codeparrot/xlcost-text-to-code huggingface dataset:
Print root to leaf paths without using recursion | Helper function that allocates a new node with the given data and None left and right pointers . ; Function to print root to leaf path for a leaf using parent nodes stored in map ; start from leaf node and keep on appending nodes into stack till root node is reached ; Start popping nodes from stack and print them ; An iterative function to do preorder traversal of binary tree and print root to leaf path without using recursion ; Corner Case ; Create an empty stack and append root to it ; Create a map to store parent pointers of binary tree nodes ; parent of root is None ; Pop all items one by one . Do following for every popped item a ) append its right child and set its parent pointer b ) append its left child and set its parent pointer Note that right child is appended first so that left is processed first ; Pop the top item from stack ; If leaf node encountered , print Top To Bottom path ; append right & left children of the popped node to stack . Also set their parent pointer in the map ; Driver Code ; Constructed binary tree is 10 / \ 8 2 / \ / 3 5 2
class newNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = self . right = None NEW_LINE DEDENT DEDENT def printTopToBottomPath ( curr , parent ) : NEW_LINE INDENT stk = [ ] NEW_LINE while ( curr ) : NEW_LINE INDENT stk . append ( curr ) NEW_LINE curr = parent [ curr ] NEW_LINE DEDENT while len ( stk ) != 0 : NEW_LINE INDENT curr = stk [ - 1 ] NEW_LINE stk . pop ( - 1 ) NEW_LINE print ( curr . data , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT def printRootToLeaf ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT nodeStack = [ ] NEW_LINE nodeStack . append ( root ) NEW_LINE parent = { } NEW_LINE parent [ root ] = None NEW_LINE while len ( nodeStack ) != 0 : NEW_LINE INDENT current = nodeStack [ - 1 ] NEW_LINE nodeStack . pop ( - 1 ) NEW_LINE if ( not ( current . left ) and not ( current . right ) ) : NEW_LINE INDENT printTopToBottomPath ( current , parent ) NEW_LINE DEDENT if ( current . right ) : NEW_LINE INDENT parent [ current . right ] = current NEW_LINE nodeStack . append ( current . right ) NEW_LINE DEDENT if ( current . left ) : NEW_LINE INDENT parent [ current . left ] = current NEW_LINE nodeStack . append ( current . left ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 10 ) NEW_LINE root . left = newNode ( 8 ) NEW_LINE root . right = newNode ( 2 ) NEW_LINE root . left . left = newNode ( 3 ) NEW_LINE root . left . right = newNode ( 5 ) NEW_LINE root . right . left = newNode ( 2 ) NEW_LINE printRootToLeaf ( root ) NEW_LINE DEDENT
Game of replacing array elements | Function return which player win the game ; Create hash that will stores all distinct element ; Traverse an array element ; Driver code
def playGame ( arr , n ) : NEW_LINE INDENT s = set ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT s . add ( arr [ i ] ) NEW_LINE DEDENT return 1 if len ( s ) % 2 == 0 else 2 NEW_LINE DEDENT arr = [ 1 , 1 , 2 , 2 , 2 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Player " , playGame ( arr , n ) , " Wins " ) NEW_LINE
Length of longest strict bitonic subsequence | function to find length of longest strict bitonic subsequence ; hash table to map the array element with the length of the longest subsequence of which it is a part of and is the last / first element of that subsequence ; arrays to store the length of increasing and decreasing subsequences which end at them or start from them ; to store the length of longest strict bitonic subsequence ; traverse the array elements from left to right ; initialize current length for element arr [ i ] as 0 ; if ' arr [ i ] -1' is in 'inc   ; update arr [ i ] subsequence length in ' inc ' and in len_inc [ ] ; traverse the array elements from right to left ; initialize current length for element arr [ i ] as 0 ; if ' arr [ i ] -1' is in 'dcr   ; update arr [ i ] subsequence length in ' dcr ' and in len_dcr [ ] ; calculating the length of all the strict bitonic subsequence ; required longest length strict bitonic subsequence ; Driver Code
def longLenStrictBitonicSub ( arr , n ) : NEW_LINE INDENT inc , dcr = dict ( ) , dict ( ) NEW_LINE len_inc , len_dcr = [ 0 ] * n , [ 0 ] * n NEW_LINE longLen = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT len = 0 NEW_LINE if inc . get ( arr [ i ] - 1 ) in inc . values ( ) : NEW_LINE INDENT len = inc . get ( arr [ i ] - 1 ) NEW_LINE DEDENT inc [ arr [ i ] ] = len_inc [ i ] = len + 1 NEW_LINE DEDENT for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT len = 0 NEW_LINE if dcr . get ( arr [ i ] - 1 ) in dcr . values ( ) : NEW_LINE INDENT len = dcr . get ( arr [ i ] - 1 ) NEW_LINE DEDENT dcr [ arr [ i ] ] = len_dcr [ i ] = len + 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if longLen < ( len_inc [ i ] + len_dcr [ i ] - 1 ) : NEW_LINE INDENT longLen = len_inc [ i ] + len_dcr [ i ] - 1 NEW_LINE DEDENT DEDENT return longLen NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 5 , 2 , 3 , 4 , 5 , 3 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Longest ▁ length ▁ strict ▁ bitonic ▁ subsequence ▁ = " , longLenStrictBitonicSub ( arr , n ) ) NEW_LINE DEDENT
Selection Sort VS Bubble Sort |  ; Driver Code
def Selection_Sort ( arr , n ) : NEW_LINE INDENT for i in range ( n - 1 ) : NEW_LINE INDENT min_index = i NEW_LINE for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( arr [ j ] < arr [ min_index ] ) : NEW_LINE INDENT min_index = j NEW_LINE DEDENT DEDENT arr [ i ] , arr [ min_index ] = arr [ min_index ] , arr [ i ] NEW_LINE DEDENT DEDENT n = 5 NEW_LINE arr = [ 2 , 0 , 1 , 4 , 3 ] NEW_LINE Selection_Sort ( arr , n ) NEW_LINE print ( " The ▁ Sorted ▁ Array ▁ by ▁ using ▁ "   \ " Selection ▁ Sort ▁ is ▁ : ▁ " , end = ' ' ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT
Last seen array element ( last appearance is earliest ) | Python3 program to find last seen element in an array . ; Returns last seen element in arr [ ] ; Store last occurrence index of every element ; Find an element in hash with minimum index value ; Driver code
import sys ; NEW_LINE def lastSeenElement ( a , n ) : NEW_LINE INDENT hash = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT hash [ a [ i ] ] = i NEW_LINE DEDENT res_ind = sys . maxsize NEW_LINE res = 0 NEW_LINE for x , y in hash . items ( ) : NEW_LINE INDENT if y < res_ind : NEW_LINE INDENT res_ind = y NEW_LINE res = x NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 2 , 1 , 2 , 2 , 4 , 1 ] NEW_LINE n = len ( a ) NEW_LINE print ( lastSeenElement ( a , n ) ) NEW_LINE DEDENT
Selection Sort VS Bubble Sort |  ; Driver Code
def Bubble_Sort ( arr , n ) : NEW_LINE INDENT for i in range ( 1 , n ) : NEW_LINE INDENT for j in range ( 0 , n - i ) : NEW_LINE INDENT if ( arr [ j ] > arr [ j + 1 ] ) : NEW_LINE INDENT arr [ j ] , arr [ j + 1 ] = arr [ j + 1 ] , arr [ j ] NEW_LINE DEDENT DEDENT DEDENT return arr NEW_LINE DEDENT n = 5 NEW_LINE arr = [ 2 , 0 , 1 , 4 , 3 ] NEW_LINE arr = Bubble_Sort ( arr , n ) NEW_LINE print ( " The ▁ Sorted ▁ Array ▁ by ▁ using ▁ Bubble ▁ Sort ▁ is ▁ : ▁ " , end = ' ' ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT
Minimum cost required to connect all houses in a city | Python3 program for the above approach ; Utility function to find set of an element v using path compression technique ; If v is the parent ; Otherwise , recursively find its parent ; Function to perform union of the sets a and b ; Find parent of a and b ; If parent are different ; Swap Operation ; Update parent of b as a ; Otherwise , return 0 ; Function to create a Minimum Cost Spanning tree for given houses ; Stores adjacency list of graph ; Traverse each coordinate ; Find the Manhattan distance ; Add the edges ; Sort all the edges ; Initialize parent [ ] and size [ ] ; Stores the minimum cost ; Finding the minimum cost ; Perform the unioun operation ; Print the minimum cost ; Driver Code ; Given houses ; Function Call
parent = [ 0 ] * 100 NEW_LINE size = [ 0 ] * 100 NEW_LINE def find_set ( v ) : NEW_LINE INDENT if ( v == parent [ v ] ) : NEW_LINE INDENT return v NEW_LINE DEDENT parent [ v ] = find_set ( parent [ v ] ) NEW_LINE return parent [ v ] NEW_LINE DEDENT def union_sets ( a , b ) : NEW_LINE INDENT a = find_set ( a ) NEW_LINE b = find_set ( b ) NEW_LINE if ( a != b ) : NEW_LINE INDENT if ( size [ a ] < size [ b ] ) : NEW_LINE INDENT a , b = b , a NEW_LINE DEDENT parent [ b ] = a NEW_LINE size [ a ] += size [ b ] NEW_LINE return 1 NEW_LINE DEDENT return 0 NEW_LINE DEDENT def MST ( houses , n ) : NEW_LINE INDENT v = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT p = abs ( houses [ i ] [ 0 ] - houses [ j ] [ 0 ] ) NEW_LINE p += abs ( houses [ i ] [ 1 ] - houses [ j ] [ 1 ] ) NEW_LINE v . append ( [ p , i , j ] ) NEW_LINE DEDENT DEDENT v = sorted ( v ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT parent [ i ] = i NEW_LINE size [ i ] = 1 NEW_LINE DEDENT ans = 0 NEW_LINE for x in v : NEW_LINE INDENT if ( union_sets ( x [ 1 ] , x [ 2 ] ) ) : NEW_LINE INDENT ans += x [ 0 ] NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT houses = [ [ 0 , 0 ] , [ 2 , 2 ] , [ 3 , 10 ] , [ 5 , 2 ] , [ 7 , 0 ] ] NEW_LINE N = len ( houses ) NEW_LINE MST ( houses , N ) NEW_LINE DEDENT
Lexicographically smallest subsequence possible by removing a character from given string | Function to find the lexicographically smallest subsequence of length N - 1 ; Store index of character to be deleted ; Traverse the String ; If ith character > ( i + 1 ) th character then store it ; If any character found in non alphabetical order then remove it ; Otherwise remove last character ; Print the resultant subsequence ; Driver Code ; Given String S ; Function Call
def firstSubsequence ( s ) : NEW_LINE INDENT isMax = - 1 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( s [ i ] > s [ i + 1 ] ) : NEW_LINE INDENT isMax = i NEW_LINE break NEW_LINE DEDENT DEDENT if ( isMax >= 0 ) : NEW_LINE INDENT s = s [ 0 : isMax ] + s [ isMax + 1 : len ( s ) ] NEW_LINE DEDENT s . rerase ( isMax , 1 ) ; NEW_LINE else : NEW_LINE INDENT s . erase ( s . length ( ) - 1 , 1 ) ; NEW_LINE s = s [ 0 : s . length ( ) - 1 ] NEW_LINE DEDENT print ( s ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " geeksforgeeks " NEW_LINE firstSubsequence ( S ) NEW_LINE DEDENT
Maximize product of array by replacing array elements with its sum or product with element from another array | Function to find the largest product of array A [ ] ; Base Case ; Store all the elements of the array A [ ] ; Sort the Array B [ ] ; Traverse the array B [ ] ; Pop minimum element ; Check which operation is producing maximum element ; Insert resultant element into the priority queue ; Evaluate the product of the elements of A [ ] ; Return the maximum product ; Given arrays ; Function Call
def largeProduct ( A , B , N ) : NEW_LINE INDENT if ( N == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT pq = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT pq . append ( A [ i ] ) NEW_LINE DEDENT B . sort ( ) NEW_LINE pq . sort ( reverse = True ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT minn = pq . pop ( ) NEW_LINE maximized_element = max ( minn * B [ i ] , minn + B [ i ] ) NEW_LINE pq . append ( maximized_element ) NEW_LINE pq . sort ( reverse = True ) NEW_LINE DEDENT max_product = 1 NEW_LINE while ( len ( pq ) > 0 ) : NEW_LINE INDENT max_product *= pq . pop ( ) ; NEW_LINE DEDENT return max_product NEW_LINE DEDENT A = [ 1 , 1 , 10 ] NEW_LINE B = [ 1 , 1 , 1 ] NEW_LINE N = 3 NEW_LINE print ( largeProduct ( A , B , N ) ) NEW_LINE
Print all root to leaf paths with there relative positions | Python3 program to print the longest leaf to leaf path ; Tree node structure used in the program ; Prints given root to leafAllpaths with underscores ; Find the minimum horizontal distance value in current root to leafAllpaths ; Find minimum horizontal distance ; Print the root to leafAllpaths with " _ " that indicate the related position ; Current tree node ; Print underscore ; Print current key ; A utility function prall path from root to leaf working of this function is similar to function of " Print _ vertical _ order " : Prpaths of binary tree in vertical order https : www . geeksforgeeks . org / print - binary - tree - vertical - order - set - 2 / ; Base case ; Leaf node ; Add leaf node and then prpath ; Store current path information ; Call left sub_tree ; Call left sub_tree ; Base case ; Driver code
MAX_PATH_SIZE = 1000 NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , x ) : NEW_LINE INDENT self . data = x NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def printPath ( size ) : NEW_LINE INDENT minimum_Hd = 10 ** 19 NEW_LINE p = [ ] NEW_LINE global Allpaths NEW_LINE for it in range ( size ) : NEW_LINE INDENT p = Allpaths [ it ] NEW_LINE minimum_Hd = min ( minimum_Hd , p [ 0 ] ) NEW_LINE DEDENT for it in range ( size ) : NEW_LINE INDENT p = Allpaths [ it ] NEW_LINE noOfUnderScores = abs ( p [ 0 ] - minimum_Hd ) NEW_LINE for i in range ( noOfUnderScores ) : NEW_LINE INDENT print ( end = " _ ▁ " ) NEW_LINE DEDENT print ( p [ 1 ] ) NEW_LINE DEDENT print ( " = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = " ) NEW_LINE DEDENT def printAllPathsUtil ( root , HD , order ) : NEW_LINE INDENT global Allpaths NEW_LINE if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( root . left == None and root . right == None ) : NEW_LINE INDENT Allpaths [ order ] = [ HD , root . data ] NEW_LINE printPath ( order + 1 ) NEW_LINE return NEW_LINE DEDENT Allpaths [ order ] = [ HD , root . data ] NEW_LINE printAllPathsUtil ( root . left , HD - 1 , order + 1 ) NEW_LINE printAllPathsUtil ( root . right , HD + 1 , order + 1 ) NEW_LINE DEDENT def printAllPaths ( root ) : NEW_LINE INDENT global Allpaths NEW_LINE if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT printAllPathsUtil ( root , 0 , 0 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT Allpaths = [ [ 0 , 0 ] for i in range ( MAX_PATH_SIZE ) ] NEW_LINE root = Node ( ' A ' ) NEW_LINE root . left = Node ( ' B ' ) NEW_LINE root . right = Node ( ' C ' ) NEW_LINE root . left . left = Node ( ' D ' ) NEW_LINE root . left . right = Node ( ' E ' ) NEW_LINE root . right . left = Node ( ' F ' ) NEW_LINE root . right . right = Node ( ' G ' ) NEW_LINE printAllPaths ( root ) NEW_LINE DEDENT
Minimum steps required to rearrange given array to a power sequence of 2 | Function to calculate the minimum steps required to convert given array into a power sequence of 2 ; Sort the array in ascending order ; Calculate the absolute difference between arr [ i ] and 2 ^ i for each index ; Return the answer ; Driver Code
def minsteps ( arr , n ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans += abs ( arr [ i ] - pow ( 2 , i ) ) NEW_LINE DEDENT return ans NEW_LINE DEDENT arr = [ 1 , 8 , 2 , 10 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE print ( minsteps ( arr , n ) ) NEW_LINE
Block swap algorithm for array rotation | Wrapper over the recursive function leftRotateRec ( ) It left rotates arr by d . ; * Return If number of elements to be rotated is zero or equal to array size ; * If number of elements to be rotated is exactly half of array size ; If A is shorter ; If B is shorter ; function to pran array ; * This function swaps d elements starting at * index fi with d elements starting at index si ; Driver Code
def leftRotate ( arr , d , n ) : NEW_LINE INDENT leftRotateRec ( arr , 0 , d , n ) ; NEW_LINE DEDENT def leftRotateRec ( arr , i , d , n ) : NEW_LINE INDENT if ( d == 0 or d == n ) : NEW_LINE INDENT return ; NEW_LINE DEDENT if ( n - d == d ) : NEW_LINE INDENT swap ( arr , i , n - d + i , d ) ; NEW_LINE return ; NEW_LINE DEDENT if ( d < n - d ) : NEW_LINE INDENT swap ( arr , i , n - d + i , d ) ; NEW_LINE leftRotateRec ( arr , i , d , n - d ) ; NEW_LINE DEDENT else : NEW_LINE INDENT swap ( arr , i , d , n - d ) ; NEW_LINE leftRotateRec ( arr , n - d + i , 2 * d - n , d ) ; NEW_LINE DEDENT DEDENT def printArray ( arr , size ) : NEW_LINE INDENT for i in range ( size ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT print ( ) ; NEW_LINE DEDENT def swap ( arr , fi , si , d ) : NEW_LINE INDENT for i in range ( d ) : NEW_LINE INDENT temp = arr [ fi + i ] ; NEW_LINE arr [ fi + i ] = arr [ si + i ] ; NEW_LINE arr [ si + i ] = temp ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] ; NEW_LINE leftRotate ( arr , 2 , 7 ) ; NEW_LINE printArray ( arr , 7 ) ; NEW_LINE DEDENT
Cost required to empty a given array by repeated removal of maximum obtained by given operations | Function to find the total cost of removing all array elements ; Sort the array in descending order ; Stores the total cost ; Contribution of i - th greatest element to the cost ; Remove the element ; If negative ; Add to the final cost ; Return the cost ; Given array arr [ ] ; Function call
def findCost ( a , n ) : NEW_LINE INDENT a . sort ( reverse = True ) NEW_LINE count = 0 NEW_LINE for j in range ( n ) : NEW_LINE INDENT p = a [ j ] - j NEW_LINE a [ j ] = 0 NEW_LINE if ( p < 0 ) : NEW_LINE INDENT p = 0 NEW_LINE continue NEW_LINE DEDENT count += p NEW_LINE DEDENT return count NEW_LINE DEDENT arr = [ 1 , 6 , 7 , 4 , 2 , 5 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE print ( findCost ( arr , N ) ) NEW_LINE
Block swap algorithm for array rotation | Python3 code for above implementation ; A is shorter ; B is shorter ; Finally , block swap A and B
def leftRotate ( arr , d , n ) : NEW_LINE INDENT if ( d == 0 or d == n ) : NEW_LINE INDENT return ; NEW_LINE DEDENT i = d NEW_LINE j = n - d NEW_LINE while ( i != j ) : NEW_LINE INDENT if ( i < j ) : NEW_LINE INDENT swap ( arr , d - i , d + j - i , i ) NEW_LINE j -= i NEW_LINE DEDENT else : NEW_LINE INDENT swap ( arr , d - i , d , j ) NEW_LINE i -= j NEW_LINE DEDENT DEDENT swap ( arr , d - i , d , i ) NEW_LINE DEDENT
Count of index pairs with equal elements in an array | Set 2 | Function that count the pairs having same elements in the array arr [ ] ; Hash map to keep track of occurences of elements ; Traverse the array arr [ ] ; Check if occurence of arr [ i ] > 0 add count [ arr [ i ] ] to answer ; Return the result ; Given array arr [ ] ; Function call
def countPairs ( arr , n ) : NEW_LINE INDENT ans = 0 NEW_LINE count = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT if arr [ i ] in count : NEW_LINE INDENT ans += count [ arr [ i ] ] NEW_LINE DEDENT if arr [ i ] in count : NEW_LINE INDENT count [ arr [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count [ arr [ i ] ] = 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ 1 , 2 , 1 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE print ( countPairs ( arr , N ) ) NEW_LINE
Maximize shortest path between given vertices by adding a single edge | Function that performs BFS Traversal ; Fill initially each distance as INF fill ( dist , dist + N , INF ) ; Perform BFS ; Traverse the current edges ; Update the distance ; Insert in queue ; Function that maximizes the shortest path between source and destination vertex by adding a single edge between given selected nodes ; To update the shortest distance between node 1 to other vertices ; To update the shortest distance between node N to other vertices ; Store the values x [ i ] - y [ i ] ; Sort all the vectors of pairs ; Traverse data [ ] ; Maximize x [ a ] - y [ b ] ; Prminimum cost ; Driver Code ; Given nodes and edges ; Sort the selected nodes ; Given edges ; Function Call
def bfs ( x , s ) : NEW_LINE INDENT global edges , dist NEW_LINE q = [ 0 for i in range ( 200000 ) ] NEW_LINE qh , qt = 0 , 0 NEW_LINE q [ qh ] = s NEW_LINE qh += 1 NEW_LINE dist [ x ] [ s ] = 0 NEW_LINE while ( qt < qh ) : NEW_LINE INDENT xx = q [ qt ] NEW_LINE qt += 1 NEW_LINE for y in edges [ xx ] : NEW_LINE INDENT if ( dist [ x ] [ y ] == 10 ** 18 ) : NEW_LINE INDENT dist [ x ] [ y ] = dist [ x ] [ xx ] + 1 NEW_LINE q [ qh ] = y NEW_LINE qh += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT def shortestPathCost ( selected , K ) : NEW_LINE INDENT global dist , edges NEW_LINE data = [ ] NEW_LINE bfs ( 0 , 0 ) NEW_LINE bfs ( 1 , N - 1 ) NEW_LINE for i in range ( K ) : NEW_LINE INDENT data . append ( [ dist [ 0 ] [ selected [ i ] ] - dist [ 1 ] [ selected [ i ] ] , selected [ i ] ] ) NEW_LINE DEDENT data = sorted ( data ) NEW_LINE best = 0 NEW_LINE MAX = - 10 ** 18 NEW_LINE for it in data : NEW_LINE INDENT a = it [ 1 ] NEW_LINE best = max ( best , MAX + dist [ 1 ] [ a ] ) NEW_LINE MAX = max ( MAX , dist [ 0 ] [ a ] ) NEW_LINE DEDENT print ( min ( dist [ 0 ] [ N - 1 ] , best + 1 ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT edges = [ [ ] for i in range ( 5 ) ] NEW_LINE dist = [ [ 10 ** 18 for i in range ( 1000005 ) ] for i in range ( 2 ) ] NEW_LINE N , M = 5 , 4 NEW_LINE K = 2 NEW_LINE selected = [ 1 , 3 ] NEW_LINE selected = sorted ( selected ) NEW_LINE edges [ 0 ] . append ( 1 ) NEW_LINE edges [ 1 ] . append ( 0 ) NEW_LINE edges [ 1 ] . append ( 2 ) NEW_LINE edges [ 2 ] . append ( 1 ) NEW_LINE edges [ 2 ] . append ( 3 ) NEW_LINE edges [ 3 ] . append ( 2 ) NEW_LINE edges [ 3 ] . append ( 4 ) NEW_LINE edges [ 4 ] . append ( 3 ) NEW_LINE shortestPathCost ( selected , K ) NEW_LINE DEDENT
Program to cyclically rotate an array by one | i and j pointing to first and last element respectively ; Driver function
def rotate ( arr , n ) : NEW_LINE INDENT i = 0 NEW_LINE j = n - 1 NEW_LINE while i != j : NEW_LINE arr [ i ] , arr [ j ] = arr [ j ] , arr [ i ] NEW_LINE i = i + 1 NEW_LINE pass NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Given ▁ array ▁ is " ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT print ( arr [ i ] , end = ' ▁ ' ) NEW_LINE DEDENT rotate ( arr , n ) NEW_LINE print ( " Rotated array is " ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT print ( arr [ i ] , end = ' ▁ ' ) NEW_LINE DEDENT
Minimum cost to empty Array where cost of removing an element is 2 ^ ( removed_count ) * arr [ i ] | Function to find the minimum cost of removing elements from the array ; Sorting in Increasing order ; Loop to find the minimum cost of removing elements ; Driver Code ; Function call
def removeElements ( arr , n ) : NEW_LINE INDENT arr . sort ( reverse = True ) NEW_LINE ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans += arr [ i ] * pow ( 2 , i ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 4 NEW_LINE arr = [ 3 , 1 , 2 , 3 ] NEW_LINE print ( removeElements ( arr , n ) ) NEW_LINE DEDENT
Sorting boundary elements of a matrix | Python program for the above approach ; Appending border elements ; Sorting the list ; Printing first row with first N elements from A ; Printing N - 2 rows ; Print elements from last ; Print middle elements from original matrix ; Print elements from front ; Printing last row ; Dimensions of a Matrix ; Given Matrix ; Function Call
def printMatrix ( grid , m , n ) : NEW_LINE INDENT A = [ ] NEW_LINE for i in range ( m ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT if j == n - 1 or ( i == m - 1 ) or j == 0 or i == 0 : NEW_LINE INDENT A . append ( grid [ i ] [ j ] ) NEW_LINE DEDENT DEDENT DEDENT A . sort ( ) NEW_LINE print ( * A [ : n ] ) NEW_LINE for i in range ( m - 2 ) : NEW_LINE INDENT print ( A [ len ( A ) - i - 1 ] , end = " ▁ " ) NEW_LINE for j in range ( 1 , n - 1 ) : NEW_LINE INDENT print ( grid [ i + 1 ] [ j ] , end = " ▁ " ) NEW_LINE DEDENT print ( A [ n + i ] ) NEW_LINE DEDENT print ( * reversed ( A [ n + m - 2 : n + m - 2 + n ] ) ) NEW_LINE DEDENT m , n = 4 , 5 NEW_LINE grid = [ [ 1 , 2 , 3 , 4 , 0 ] , [ 1 , 1 , 1 , 1 , 2 ] , [ 1 , 2 , 2 , 2 , 4 ] , [ 1 , 9 , 3 , 1 , 7 ] ] NEW_LINE printMatrix ( grid , m , n ) NEW_LINE