codeparrot/xlcost-text-to-code huggingface dataset:
Sort a string according to the frequency of characters | Returns count of character in the string ; Check for vowel ; Function to sort the string according to the frequency ; Vector to store the frequency of characters with respective character ; Inserting frequency with respective character in the vector pair ; Sort the vector , this will sort the pair according to the number of characters ; Print the sorted vector content ; Driver code
def countFrequency ( string , ch ) : NEW_LINE INDENT count = 0 ; NEW_LINE for i in range ( len ( string ) ) : NEW_LINE INDENT if ( string [ i ] == ch ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT return count ; NEW_LINE DEDENT def sortArr ( string ) : NEW_LINE INDENT n = len ( string ) ; NEW_LINE vp = [ ] ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT vp . append ( ( countFrequency ( string , string [ i ] ) , string [ i ] ) ) ; NEW_LINE DEDENT vp . sort ( ) ; NEW_LINE for i in range ( len ( vp ) ) : NEW_LINE INDENT print ( vp [ i ] [ 1 ] , end = " " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " geeksforgeeks " ; NEW_LINE sortArr ( string ) ; NEW_LINE DEDENT
Maximum sum after rearranging the array for K queries | Function to find maximum sum after rearranging array elements ; Auxiliary array to find the count of each selected elements Initialize with 0 ; Finding count of every element to be selected ; Making it to 0 - indexing ; Prefix sum array concept is used to obtain the count array ; Iterating over the count array to get the final array ; Variable to store the maximum sum ; Sorting both the arrays ; Loop to find the maximum sum ; Driver code
def maxSumArrangement ( A , R , N , M ) : NEW_LINE INDENT count = [ 0 for i in range ( N ) ] NEW_LINE for i in range ( M ) : NEW_LINE INDENT l = R [ i ] [ 0 ] NEW_LINE r = R [ i ] [ 1 ] + 1 NEW_LINE l = l - 1 NEW_LINE r = r - 1 NEW_LINE count [ l ] = count [ l ] + 1 NEW_LINE if ( r < N ) : NEW_LINE INDENT count [ r ] = count [ r ] - 1 NEW_LINE DEDENT DEDENT for i in range ( 1 , N ) : NEW_LINE INDENT count [ i ] = count [ i ] + count [ i - 1 ] NEW_LINE DEDENT ans = 0 NEW_LINE count . sort ( ) NEW_LINE A . sort ( ) NEW_LINE for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT ans = ans + A [ i ] * count [ i ] NEW_LINE DEDENT return ans NEW_LINE DEDENT A = [ 2 , 6 , 10 , 1 , 5 , 6 ] NEW_LINE R = [ [ 1 , 3 ] , [ 4 , 6 ] , [ 3 , 4 ] ] NEW_LINE N = len ( A ) NEW_LINE M = len ( R ) NEW_LINE print ( maxSumArrangement ( A , R , N , M ) ) NEW_LINE
Sum of M maximum distinct digit sum from 1 to N that are factors of K | Python 3 implementation to find the sum of maximum distinct digit sum of at most M numbers from 1 to N that are factors of K ; Function to find the factors of K in N ; Initialise a vector ; Find out the factors of K less than N ; Find the digit sum of each factor ; Sum of digits for each element in vector ; Find the largest M distinct digit sum from the digitSum vector ; Find the sum of last M numbers . ; Find the at most M numbers from N natural numbers whose digit sum is distinct and those M numbers are factors of K ; Find out the factors of K less than N ; Sum of digits for each element in vector ; Sorting the digitSum vector ; Removing the duplicate elements ; Finding the sum and returning it ; Driver Code ; Function Call
import math NEW_LINE def findFactors ( n , k ) : NEW_LINE INDENT factors = [ ] NEW_LINE sqt = ( int ) ( math . sqrt ( k ) ) NEW_LINE for i in range ( 1 , sqt ) : NEW_LINE INDENT if ( k % i == 0 ) : NEW_LINE INDENT if ( k // i == i and i <= n ) : NEW_LINE INDENT factors . append ( i ) NEW_LINE DEDENT else : NEW_LINE INDENT if ( i <= n ) : NEW_LINE INDENT factors . append ( i ) NEW_LINE DEDENT if ( k // i <= n ) : NEW_LINE INDENT factors . append ( k // i ) NEW_LINE DEDENT DEDENT DEDENT DEDENT return factors NEW_LINE DEDENT def findDigitSum ( a ) : NEW_LINE INDENT for i in range ( len ( a ) ) : NEW_LINE INDENT c = 0 NEW_LINE while ( a [ i ] > 0 ) : NEW_LINE INDENT c += a [ i ] % 10 NEW_LINE a [ i ] = a [ i ] // 10 NEW_LINE DEDENT a [ i ] = c NEW_LINE DEDENT return a NEW_LINE DEDENT def findMMaxDistinctDigitSum ( distinctDigitSum , m ) : NEW_LINE INDENT sum = 0 NEW_LINE i = len ( distinctDigitSum ) - 1 NEW_LINE while ( i >= 0 and m > 0 ) : NEW_LINE INDENT sum += distinctDigitSum [ i ] NEW_LINE i -= 1 NEW_LINE m -= 1 NEW_LINE DEDENT return sum NEW_LINE DEDENT def findDistinctMnumbers ( n , k , m ) : NEW_LINE INDENT factors = findFactors ( n , k ) NEW_LINE digitSum = findDigitSum ( factors ) NEW_LINE digitSum . sort ( ) NEW_LINE ip = list ( set ( digitSum ) ) NEW_LINE return findMMaxDistinctDigitSum ( ip , m ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 100 NEW_LINE k = 80 NEW_LINE m = 4 NEW_LINE print ( findDistinctMnumbers ( n , k , m ) ) NEW_LINE DEDENT
Sorting objects using In | Partition function which will partition the array and into two parts ; Compare hash values of objects ; Classic quicksort algorithm ; Function to sort and print the objects ; As the sorting order is blue objects , red objects and then yellow objects ; Quick sort function ; Printing the sorted array ; Let 's represent objects as strings
objects = [ ] NEW_LINE hash = dict ( ) NEW_LINE def partition ( l , r ) : NEW_LINE INDENT global objects , hash NEW_LINE j = l - 1 NEW_LINE last_element = hash [ objects [ r ] ] NEW_LINE for i in range ( l , r ) : NEW_LINE INDENT if ( hash [ objects [ i ] ] <= last_element ) : NEW_LINE INDENT j += 1 NEW_LINE ( objects [ i ] , objects [ j ] ) = ( objects [ j ] , objects [ i ] ) NEW_LINE DEDENT DEDENT j += 1 NEW_LINE ( objects [ j ] , objects [ r ] ) = ( objects [ r ] , objects [ j ] ) NEW_LINE return j NEW_LINE DEDENT def quicksort ( l , r ) : NEW_LINE INDENT if ( l < r ) : NEW_LINE INDENT mid = partition ( l , r ) NEW_LINE quicksort ( l , mid - 1 ) NEW_LINE quicksort ( mid + 1 , r ) NEW_LINE DEDENT DEDENT def sortObj ( ) : NEW_LINE INDENT global objects , hash NEW_LINE hash [ " blue " ] = 1 NEW_LINE hash [ " red " ] = 2 NEW_LINE hash [ " yellow " ] = 3 NEW_LINE quicksort ( 0 , int ( len ( objects ) - 1 ) ) NEW_LINE for i in objects : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT objects = [ " red " , " blue " , " red " , " yellow " , " blue " ] NEW_LINE sortObj ( ) NEW_LINE
Sort the numbers according to their product of digits | Function to return the product of the digits of n ; Function to sort the array according to the product of the digits of elements ; Vector to store the digit product with respective elements ; Inserting digit product with elements in the vector pair ; Sort the vector , this will sort the pair according to the product of the digits ; Print the sorted vector content ; Driver code
def productOfDigit ( n ) : NEW_LINE INDENT product = 1 ; NEW_LINE while ( n > 0 ) : NEW_LINE INDENT product *= ( n % 10 ) ; NEW_LINE n = n // 10 ; NEW_LINE DEDENT return product ; NEW_LINE DEDENT def sortArr ( arr , n ) : NEW_LINE INDENT vp = [ ] ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT vp . append ( ( productOfDigit ( arr [ i ] ) , arr [ i ] ) ) ; NEW_LINE DEDENT vp . sort ( ) ; NEW_LINE for i in range ( len ( vp ) ) : NEW_LINE INDENT print ( vp [ i ] [ 1 ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 12 , 10 , 102 , 31 , 15 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE sortArr ( arr , n ) ; NEW_LINE DEDENT
Minimum steps required to reduce all the elements of the array to zero | Function to return the minimum steps required to reduce all the elements to 0 ; Maximum element from the array ; Driver code
def minSteps ( arr , n ) : NEW_LINE INDENT maxVal = max ( arr ) NEW_LINE return maxVal NEW_LINE DEDENT arr = [ 1 , 2 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE print ( minSteps ( arr , n ) ) NEW_LINE
Maximum water that can be stored between two buildings | Return the maximum water that can be stored ; Check all possible pairs of buildings ; Maximum so far ; Driver code
def maxWater ( height , n ) : NEW_LINE INDENT maximum = 0 ; NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT current = min ( height [ i ] , height [ j ] ) * ( j - i - 1 ) ; NEW_LINE maximum = max ( maximum , current ) ; NEW_LINE DEDENT DEDENT return maximum ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT height = [ 2 , 1 , 3 , 4 , 6 , 5 ] ; NEW_LINE n = len ( height ) ; NEW_LINE print ( maxWater ( height , n ) ) ; NEW_LINE DEDENT
Check if the given array contains all the divisors of some integer | Python 3 implementation of the approach ; Function that returns true if arr [ ] contains all the divisors of some integer ; Maximum element from the array ; Vector to store divisors of the maximum element i . e . X ; Store all the divisors of X ; If the lengths of a [ ] and b are different return false ; Sort a [ ] and b ; If divisors are not equal return false ; Driver code
from math import sqrt NEW_LINE def checkDivisors ( a , n ) : NEW_LINE INDENT X = max ( a ) NEW_LINE b = [ ] NEW_LINE for i in range ( 1 , int ( sqrt ( X ) ) + 1 ) : NEW_LINE INDENT if ( X % i == 0 ) : NEW_LINE INDENT b . append ( i ) NEW_LINE if ( X // i != i ) : NEW_LINE INDENT b . append ( X // i ) NEW_LINE DEDENT DEDENT DEDENT if ( len ( b ) != n ) : NEW_LINE INDENT return False NEW_LINE DEDENT a . sort ( reverse = False ) NEW_LINE b . sort ( reverse = False ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( b [ i ] != a [ i ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 8 , 1 , 2 , 12 , 48 , 6 , 4 , 24 , 16 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE if ( checkDivisors ( arr , N ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Kth largest node among all directly connected nodes to the given node in an undirected graph | Function to print Kth node for each node ; Vector to store nodes directly connected to ith node along with their values ; Add edges to the vector along with the values of the node ; Sort neighbors of every node and find the Kth node ; Get the kth node ; If total nodes are < k ; Driver code
def findKthNode ( u , v , n , val , V , k ) : NEW_LINE INDENT g = [ [ ] for i in range ( V ) ] NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT g [ u [ i ] ] . append ( ( val [ v [ i ] ] , v [ i ] ) ) NEW_LINE g [ v [ i ] ] . append ( ( val [ u [ i ] ] , u [ i ] ) ) NEW_LINE DEDENT for i in range ( 0 , V ) : NEW_LINE INDENT if len ( g [ i ] ) > 0 : NEW_LINE INDENT g [ i ] . sort ( ) NEW_LINE DEDENT if k <= len ( g [ i ] ) : NEW_LINE INDENT print ( g [ i ] [ - k ] [ 1 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT DEDENT return NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT V = 3 NEW_LINE val = [ 2 , 4 , 3 ] NEW_LINE u = [ 0 , 0 , 1 ] NEW_LINE v = [ 2 , 1 , 2 ] NEW_LINE n , k = len ( u ) , 2 NEW_LINE findKthNode ( u , v , n , val , V , k ) NEW_LINE DEDENT
Minimum length of square to contain at least half of the given Coordinates | Function to Calculate the Minimum value of M ; To store the minimum M for each point in array ; Sort the array ; Index at which atleast required point are inside square of length 2 * M ; Driver Code
def findSquare ( n ) : NEW_LINE INDENT points = [ [ 1 , 2 ] , [ - 3 , 4 ] , [ 1 , 78 ] , [ - 3 , - 7 ] ] NEW_LINE a = [ None ] * n NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT x = points [ i ] [ 0 ] NEW_LINE y = points [ i ] [ 1 ] NEW_LINE a [ i ] = max ( abs ( x ) , abs ( y ) ) NEW_LINE DEDENT a . sort ( ) NEW_LINE index = n // 2 - 1 NEW_LINE print ( " Minimum ▁ M ▁ required ▁ is : " , a [ index ] ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 4 NEW_LINE findSquare ( N ) NEW_LINE DEDENT
Eulerian Path in undirected graph | Function to find out the path It takes the adjacency matrix representation of the graph as input ; Find out number of edges each vertex has ; Find out how many vertex has odd number edges ; If number of vertex with odd number of edges is greater than two return " No ▁ Solution " . ; If there is a path find the path Initialize empty stack and path take the starting current as discussed ; Loop will run until there is element in the stack or current edge has some neighbour . ; If current node has not any neighbour add it to path and pop stack set new current to the popped element ; If the current vertex has at least one neighbour add the current vertex to stack , remove the edge between them and set the current to its neighbour . ; Print the path ; Driver Code ; Test case 1 ; Test case 2 ; Test case 3
def findpath ( graph , n ) : NEW_LINE INDENT numofadj = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT numofadj . append ( sum ( graph [ i ] ) ) NEW_LINE DEDENT startpoint , numofodd = 0 , 0 NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( numofadj [ i ] % 2 == 1 ) : NEW_LINE INDENT numofodd += 1 NEW_LINE startpoint = i NEW_LINE DEDENT DEDENT if ( numofodd > 2 ) : NEW_LINE INDENT print ( " No ▁ Solution " ) NEW_LINE return NEW_LINE DEDENT stack = [ ] NEW_LINE path = [ ] NEW_LINE cur = startpoint NEW_LINE while ( len ( stack ) > 0 or sum ( graph [ cur ] ) != 0 ) : NEW_LINE INDENT if ( sum ( graph [ cur ] ) == 0 ) : NEW_LINE INDENT path . append ( cur ) NEW_LINE cur = stack [ - 1 ] NEW_LINE del stack [ - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if ( graph [ cur ] [ i ] == 1 ) : NEW_LINE INDENT stack . append ( cur ) NEW_LINE graph [ cur ] [ i ] = 0 NEW_LINE graph [ i ] [ cur ] = 0 NEW_LINE cur = i NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT DEDENT for ele in path : NEW_LINE INDENT print ( ele , end = " ▁ - > ▁ " ) NEW_LINE DEDENT print ( cur ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT graph1 = [ [ 0 , 1 , 0 , 0 , 1 ] , [ 1 , 0 , 1 , 1 , 0 ] , [ 0 , 1 , 0 , 1 , 0 ] , [ 0 , 1 , 1 , 0 , 0 ] , [ 1 , 0 , 0 , 0 , 0 ] ] NEW_LINE n = len ( graph1 ) NEW_LINE findpath ( graph1 , n ) NEW_LINE graph2 = [ [ 0 , 1 , 0 , 1 , 1 ] , [ 1 , 0 , 1 , 0 , 1 ] , [ 0 , 1 , 0 , 1 , 1 ] , [ 1 , 1 , 1 , 0 , 0 ] , [ 1 , 0 , 1 , 0 , 0 ] ] NEW_LINE n = len ( graph2 ) NEW_LINE findpath ( graph2 , n ) NEW_LINE graph3 = [ [ 0 , 1 , 0 , 0 , 1 ] , [ 1 , 0 , 1 , 1 , 1 ] , [ 0 , 1 , 0 , 1 , 0 ] , [ 0 , 1 , 1 , 0 , 1 ] , [ 1 , 1 , 0 , 1 , 0 ] ] NEW_LINE n = len ( graph3 ) NEW_LINE findpath ( graph3 , n ) NEW_LINE DEDENT
Array element with minimum sum of absolute differences | Function to return the minimized sum ; Sort the array ; Median of the array ; Calculate the minimized sum ; Return the required sum ; Driver code
def minSum ( arr , n ) : NEW_LINE INDENT arr . sort ( ) ; NEW_LINE x = arr [ n // 2 ] ; NEW_LINE sum = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += abs ( arr [ i ] - x ) ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 3 , 9 , 3 , 6 ] ; NEW_LINE n = len ( arr ) NEW_LINE print ( minSum ( arr , n ) ) ; NEW_LINE DEDENT
Sort even and odd placed elements in increasing order | function to prin the odd and even indexed digits ; lists to store the odd and even positioned digits ; traverse through all the indexes in the integer ; if the digit is in odd_index position append it to odd_position list ; else append it to the even_position list ; print the elements in the list in sorted order ; Driver Code
def odd_even ( n ) : NEW_LINE INDENT odd_indexes = [ ] NEW_LINE even_indexes = [ ] NEW_LINE for i in range ( len ( n ) ) : NEW_LINE INDENT if i % 2 == 0 : odd_indexes . append ( n [ i ] ) NEW_LINE else : even_indexes . append ( n [ i ] ) NEW_LINE DEDENT for i in sorted ( odd_indexes ) : print ( i , end = " ▁ " ) NEW_LINE for i in sorted ( even_indexes ) : print ( i , end = " ▁ " ) NEW_LINE DEDENT n = [ 3 , 2 , 7 , 6 , 8 ] NEW_LINE odd_even ( n ) NEW_LINE
Number of pairs whose sum is a power of 2 | Function to return the count of valid pairs ; Storing occurrences of each element ; Sort the array in deceasing order ; Start taking largest element each time ; If element has already been paired ; Find the number which is greater than a [ i ] and power of two ; If there is a number which adds up with a [ i ] to form a power of two ; Edge case when a [ i ] and crr - a [ i ] is same and we have only one occurrence of a [ i ] then it cannot be paired ; Remove already paired elements ; Return the count ; Driver code
def countPairs ( a , n ) : NEW_LINE INDENT mp = dict . fromkeys ( a , 0 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT mp [ a [ i ] ] += 1 NEW_LINE DEDENT a . sort ( reverse = True ) NEW_LINE count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( mp [ a [ i ] ] < 1 ) : NEW_LINE INDENT continue NEW_LINE DEDENT cur = 1 NEW_LINE while ( cur <= a [ i ] ) : NEW_LINE INDENT cur = cur << 1 NEW_LINE DEDENT if ( cur - a [ i ] in mp . keys ( ) ) : NEW_LINE INDENT if ( cur - a [ i ] == a [ i ] and mp [ a [ i ] ] == 1 ) : NEW_LINE INDENT continue NEW_LINE DEDENT count += 1 NEW_LINE mp [ cur - a [ i ] ] -= 1 NEW_LINE mp [ a [ i ] ] -= 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 3 , 11 , 14 , 5 , 13 ] NEW_LINE n = len ( a ) NEW_LINE print ( countPairs ( a , n ) ) NEW_LINE DEDENT
Maximizing the elements with a [ i + 1 ] > a [ i ] | Returns the number of positions where A ( i + 1 ) is greater than A ( i ) after rearrangement of the array ; Creating a HashMap containing char as a key and occurrences as a value ; Find the maximum frequency ; Driver code
def countMaxPos ( arr ) : NEW_LINE INDENT n = len ( arr ) NEW_LINE Map = { } NEW_LINE for x in arr : NEW_LINE INDENT if x in Map : NEW_LINE INDENT Map [ x ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT Map [ x ] = 1 NEW_LINE DEDENT DEDENT max_freq = 0 NEW_LINE for entry in Map : NEW_LINE INDENT max_freq = max ( max_freq , Map [ entry ] ) NEW_LINE DEDENT return n - max_freq NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 20 , 30 , 10 , 50 , 40 ] NEW_LINE print ( countMaxPos ( arr ) ) NEW_LINE DEDENT
Permutation of an array that has smaller values from another array | Function to print required permutation ; Storing elements and indexes ; Filling the answer array ; pair element of A and B ; Fill the remaining elements of answer ; Output required permutation ; Driver Code
def anyPermutation ( A , B , n ) : NEW_LINE INDENT Ap , Bp = [ ] , [ ] NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT Ap . append ( [ A [ i ] , i ] ) NEW_LINE DEDENT for i in range ( 0 , n ) : NEW_LINE INDENT Bp . append ( [ B [ i ] , i ] ) NEW_LINE DEDENT Ap . sort ( ) NEW_LINE Bp . sort ( ) NEW_LINE i , j = 0 , 0 , NEW_LINE ans = [ 0 ] * n NEW_LINE remain = [ ] NEW_LINE while i < n and j < n : NEW_LINE INDENT if Ap [ i ] [ 0 ] > Bp [ j ] [ 0 ] : NEW_LINE INDENT ans [ Bp [ j ] [ 1 ] ] = Ap [ i ] [ 0 ] NEW_LINE i += 1 NEW_LINE j += 1 NEW_LINE DEDENT else : NEW_LINE INDENT remain . append ( i ) NEW_LINE i += 1 NEW_LINE DEDENT DEDENT j = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ans [ i ] == 0 : NEW_LINE INDENT ans [ i ] = Ap [ remain [ j ] ] [ 0 ] NEW_LINE j += 1 NEW_LINE DEDENT DEDENT for i in range ( 0 , n ) : NEW_LINE INDENT print ( ans [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 12 , 24 , 8 , 32 ] NEW_LINE B = [ 13 , 25 , 32 , 11 ] NEW_LINE n = len ( A ) NEW_LINE anyPermutation ( A , B , n ) NEW_LINE DEDENT
Rearrange all elements of array which are multiples of x in increasing order | Function to sort all the multiples of x from the array in ascending order ; Insert all multiples of 5 to a vector ; Sort the vector ; update the array elements ; Driver code ; Print the result
def sortMultiples ( arr , n , x ) : NEW_LINE INDENT v = [ ] NEW_LINE for i in range ( 0 , n , 1 ) : NEW_LINE INDENT if ( arr [ i ] % x == 0 ) : NEW_LINE INDENT v . append ( arr [ i ] ) NEW_LINE DEDENT DEDENT v . sort ( reverse = False ) NEW_LINE j = 0 NEW_LINE for i in range ( 0 , n , 1 ) : NEW_LINE INDENT if ( arr [ i ] % x == 0 ) : NEW_LINE INDENT arr [ i ] = v [ j ] NEW_LINE j += 1 NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 125 , 3 , 15 , 6 , 100 , 5 ] NEW_LINE x = 5 NEW_LINE n = len ( arr ) NEW_LINE sortMultiples ( arr , n , x ) NEW_LINE for i in range ( 0 , n , 1 ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT
Minimum increment in the sides required to get non | Function to return the minimum increase in side lengths of the triangle ; push the three sides to a array ; sort the array ; check if sum is greater than third side ; Driver code
def minimumIncrease ( a , b , c ) : NEW_LINE INDENT arr = [ a , b , c ] NEW_LINE arr . sort ( ) NEW_LINE if arr [ 0 ] + arr [ 1 ] >= arr [ 2 ] : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT return arr [ 2 ] - ( arr [ 0 ] + arr [ 1 ] ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a , b , c = 3 , 5 , 10 NEW_LINE print ( minimumIncrease ( a , b , c ) ) NEW_LINE DEDENT
Minimum sum of differences with an element in an array | function to find min sum after operation ; Sort the array ; Pick middle value ; Sum of absolute differences . ; Driver Code
def absSumDidd ( a , n ) : NEW_LINE INDENT a . sort ( ) NEW_LINE midValue = a [ ( int ) ( n // 2 ) ] NEW_LINE sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum = sum + abs ( a [ i ] - midValue ) NEW_LINE DEDENT return sum NEW_LINE DEDENT arr = [ 5 , 11 , 14 , 10 , 17 , 15 ] NEW_LINE n = len ( arr ) NEW_LINE print ( absSumDidd ( arr , n ) ) NEW_LINE
Printing frequency of each character just after its consecutive occurrences | Python 3 program to print run length encoding of a string ; Counting occurrences of s [ i ] ; Driver Code
def printRLE ( s ) : NEW_LINE INDENT i = 0 NEW_LINE while ( i < len ( s ) - 1 ) : NEW_LINE INDENT count = 1 NEW_LINE while s [ i ] == s [ i + 1 ] : NEW_LINE INDENT i += 1 NEW_LINE count += 1 NEW_LINE if i + 1 == len ( s ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT print ( str ( s [ i ] ) + str ( count ) , end = " ▁ " ) NEW_LINE i += 1 NEW_LINE DEDENT print ( ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT printRLE ( " GeeeEEKKKss " ) NEW_LINE printRLE ( " cccc0ddEEE " ) NEW_LINE DEDENT