codeparrot/xlcost-text-to-code huggingface dataset:
Remove all subtrees consisting only of even valued nodes from a Binary Tree | Node of the tree ; Function to print tree level wise ; Base Case ; Create an empty queue for level order traversal ; Enqueue Root ; Print front of queue and remove it from queue ; If left child is present ; Otherwise ; If right child is present ; Otherwise ; Function to remove subtrees ; Base Case ; Search for required condition in left and right half ; If the node is even and leaf node ; Driver Code ; Function Call ; Print answer
class node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def printLevelOrder ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT q = [ ] NEW_LINE q . append ( root ) NEW_LINE while ( len ( q ) > 0 ) : NEW_LINE INDENT temp = q [ 0 ] NEW_LINE print ( temp . data , end = " ▁ " ) NEW_LINE q = q [ 1 : ] NEW_LINE if ( temp . left != None ) : NEW_LINE INDENT q . append ( temp . left ) NEW_LINE DEDENT elif ( temp . right != None ) : NEW_LINE INDENT print ( " NULL " , end = " ▁ " ) NEW_LINE DEDENT if ( temp . right != None ) : NEW_LINE INDENT q . append ( temp . right ) NEW_LINE DEDENT elif ( temp . left != None ) : NEW_LINE INDENT print ( " NULL " , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT def pruneTree ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return None NEW_LINE DEDENT root . left = pruneTree ( root . left ) NEW_LINE root . right = pruneTree ( root . right ) NEW_LINE if ( root . data % 2 == 0 and root . right == None and root . left == None ) : NEW_LINE INDENT return None NEW_LINE DEDENT return root NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = node ( 1 ) NEW_LINE root . left = node ( 2 ) NEW_LINE root . left . left = node ( 8 ) NEW_LINE root . left . right = node ( 10 ) NEW_LINE root . right = node ( 7 ) NEW_LINE root . right . left = node ( 12 ) NEW_LINE root . right . right = node ( 5 ) NEW_LINE newRoot = pruneTree ( root ) NEW_LINE printLevelOrder ( newRoot ) NEW_LINE DEDENT
Count total ways to reach destination from source in an undirected Graph | Utility Function to count total ways ; Base condition When reach to the destination ; Make vertex visited ; Recursive function , for count ways ; Backtracking Make vertex unvisited ; Return total ways ; Function to count total ways to reach destination ; Loop to make all vertex unvisited , Initially ; Make source visited ; Print total ways
def countWays ( mtrx , vrtx , i , dest , visited ) : NEW_LINE INDENT if ( i == dest ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT total = 0 NEW_LINE for j in range ( vrtx ) : NEW_LINE INDENT if ( mtrx [ i ] [ j ] == 1 and not visited [ j ] ) : NEW_LINE INDENT visited [ j ] = True ; NEW_LINE total += countWays ( mtrx , vrtx , j , dest , visited ) ; NEW_LINE visited [ j ] = False ; NEW_LINE DEDENT DEDENT return total NEW_LINE DEDENT def totalWays ( mtrx , vrtx , src , dest ) : NEW_LINE INDENT visited = [ False ] * vrtx NEW_LINE for i in range ( vrtx ) : NEW_LINE INDENT visited [ i ] = False NEW_LINE DEDENT visited [ src ] = True ; NEW_LINE return countWays ( mtrx , vrtx , src , dest , visited ) NEW_LINE DEDENT print ( totalWays ( mtrx , vrtx , src - 1 , dest - 1 ) ) NEW_LINE
Print path from given Source to Destination in 2 | Function to print the path ; Base condition ; Pop stores elements ; Recursive call for printing stack In reverse order ; Function to store the path into The stack , if path exist ; Base condition ; Push current elements ; Condition to check whether reach to the Destination or not ; Increment ' x ' ordinate of source by ( 2 * x + y ) Keeping ' y ' constant ; Increment ' y ' ordinate of source by ( 2 * y + x ) Keeping ' x ' constant ; Pop current elements form stack ; If no path exists ; Utility function to check whether path exist or not ; To store x co - ordinate ; To store y co - ordinate ; Function to find the path ; Print - 1 , if path doesn 't exist ; Driver code ; Function call
def printExistPath ( sx , sy , last ) : NEW_LINE INDENT if ( len ( sx ) == 0 or len ( sy ) == 0 ) : NEW_LINE INDENT return NEW_LINE DEDENT x = sx [ - 1 ] ; NEW_LINE y = sy [ - 1 ] NEW_LINE sx . pop ( ) ; NEW_LINE sy . pop ( ) ; NEW_LINE printExistPath ( sx , sy , last ) ; NEW_LINE if ( len ( sx ) == last - 1 ) : NEW_LINE INDENT print ( " ( " + str ( x ) + " , ▁ " + str ( y ) + " ) " , end = ' ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " ( " + str ( x ) + " , ▁ " + str ( y ) + " ) ▁ - > ▁ " , end = ' ' ) NEW_LINE DEDENT DEDENT def storePath ( srcX , srcY , destX , destY , sx , sy ) : NEW_LINE INDENT if ( srcX > destX or srcY > destY ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT sx . append ( srcX ) ; NEW_LINE sy . append ( srcY ) ; NEW_LINE if ( srcX == destX and srcY == destY ) : NEW_LINE INDENT printExistPath ( sx , sy , len ( sx ) ) ; NEW_LINE return True ; NEW_LINE DEDENT if ( storePath ( ( 2 * srcX ) + srcY , srcY , destX , destY , sx , sy ) ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT if ( storePath ( srcX , ( 2 * srcY ) + srcX , destX , destY , sx , sy ) ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT sx . pop ( ) ; NEW_LINE sy . pop ( ) ; NEW_LINE return False ; NEW_LINE DEDENT def isPathExist ( srcX , srcY , destX , destY ) : NEW_LINE INDENT sx = [ ] NEW_LINE sy = [ ] NEW_LINE return storePath ( srcX , srcY , destX , destY , sx , sy ) ; NEW_LINE DEDENT def printPath ( srcX , srcY , destX , destY ) : NEW_LINE INDENT if ( not isPathExist ( srcX , srcY , destX , destY ) ) : NEW_LINE INDENT print ( " - 1" ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT srcX = 5 NEW_LINE srcY = 8 ; NEW_LINE destX = 83 NEW_LINE destY = 21 ; NEW_LINE printPath ( srcX , srcY , destX , destY ) ; NEW_LINE DEDENT
Count even paths in Binary Tree | A Tree node ; Utility function to count the even path in a given Binary tree ; Base Condition , when node pointer becomes null or node value is odd ; Increment count when encounter leaf node with all node value even ; Left recursive call , and save the value of count ; Right recursive call , and return value of count ; Function to count the even paths in a given Binary tree ; Function call with count = 0 ; Driver Code ; Tree ; Function call
class Node : NEW_LINE INDENT def __init__ ( self , x ) : NEW_LINE INDENT self . key = x NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def evenPaths ( node , count ) : NEW_LINE INDENT if ( node == None or ( node . key % 2 != 0 ) ) : NEW_LINE INDENT return count NEW_LINE DEDENT if ( not node . left and not node . right ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT count = evenPaths ( node . left , count ) NEW_LINE return evenPaths ( node . right , count ) NEW_LINE DEDENT def countEvenPaths ( node ) : NEW_LINE INDENT return evenPaths ( node , 0 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = Node ( 12 ) NEW_LINE root . left = Node ( 13 ) NEW_LINE root . right = Node ( 12 ) NEW_LINE root . right . left = Node ( 14 ) NEW_LINE root . right . right = Node ( 16 ) NEW_LINE root . right . left . left = Node ( 21 ) NEW_LINE root . right . left . right = Node ( 22 ) NEW_LINE root . right . right . left = Node ( 22 ) NEW_LINE root . right . right . right = Node ( 24 ) NEW_LINE root . right . right . right . left = Node ( 8 ) NEW_LINE print ( countEvenPaths ( root ) ) NEW_LINE DEDENT
Sum of subsets of all the subsets of an array | O ( 3 ^ N ) |  ; Function to sum of all subsets of a given array ; Base case ; Recursively calling subsetSum ; Function to generate the subsets ; Base - case ; Finding the sum of all the subsets of the generated subset ; Recursively accepting and rejecting the current number ; Driver code
/ * To store the final ans * / NEW_LINE c = [ ] NEW_LINE ans = 0 NEW_LINE def subsetSum ( i , curr ) : NEW_LINE INDENT global ans , c NEW_LINE if ( i == len ( c ) ) : NEW_LINE INDENT ans += curr NEW_LINE return NEW_LINE DEDENT subsetSum ( i + 1 , curr + c [ i ] ) NEW_LINE subsetSum ( i + 1 , curr ) NEW_LINE DEDENT def subsetGen ( arr , i , n ) : NEW_LINE INDENT global ans , c NEW_LINE if ( i == n ) : NEW_LINE INDENT subsetSum ( 0 , 0 ) NEW_LINE return NEW_LINE DEDENT subsetGen ( arr , i + 1 , n ) NEW_LINE c . append ( arr [ i ] ) NEW_LINE subsetGen ( arr , i + 1 , n ) NEW_LINE del c [ - 1 ] NEW_LINE DEDENT arr = [ 1 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE subsetGen ( arr , 0 , n ) NEW_LINE print ( ans ) NEW_LINE
Print the DFS traversal step | Python3 program to print the complete DFS - traversal of graph using back - tracking ; Function to print the complete DFS - traversal ; Check if all th node is visited or not and count unvisited nodes ; If all the node is visited return ; Mark not visited node as visited ; Track the current edge ; Print the node ; Check for not visited node and proceed with it . ; Call the DFs function if not visited ; Backtrack through the last visited nodes ; Function to call the DFS function which prints the DFS - traversal stepwise ; Create a array of visited node ; Vector to track last visited road ; Initialize all the node with false ; Call the function ; Function to insert edges in Graph ; Driver Code ; Number of nodes and edges in the graph ; Function call to create the graph ; Call the function to print
N = 1000 NEW_LINE adj = [ [ ] for i in range ( N ) ] NEW_LINE def dfsUtil ( u , node , visited , road_used , parent , it ) : NEW_LINE INDENT c = 0 NEW_LINE for i in range ( node ) : NEW_LINE INDENT if ( visited [ i ] ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT DEDENT if ( c == node ) : NEW_LINE INDENT return NEW_LINE DEDENT visited [ u ] = True NEW_LINE road_used . append ( [ parent , u ] ) NEW_LINE print ( u , end = " ▁ " ) NEW_LINE for x in adj [ u ] : NEW_LINE INDENT if ( not visited [ x ] ) : NEW_LINE INDENT dfsUtil ( x , node , visited , road_used , u , it + 1 ) NEW_LINE DEDENT DEDENT for y in road_used : NEW_LINE INDENT if ( y [ 1 ] == u ) : NEW_LINE INDENT dfsUtil ( y [ 0 ] , node , visited , road_used , u , it + 1 ) NEW_LINE DEDENT DEDENT DEDENT def dfs ( node ) : NEW_LINE INDENT visited = [ False for i in range ( node ) ] NEW_LINE road_used = [ ] NEW_LINE for i in range ( node ) : NEW_LINE INDENT visited [ i ] = False NEW_LINE DEDENT dfsUtil ( 0 , node , visited , road_used , - 1 , 0 ) NEW_LINE DEDENT def insertEdge ( u , v ) : NEW_LINE INDENT adj [ u ] . append ( v ) NEW_LINE adj [ v ] . append ( u ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT node = 11 NEW_LINE edge = 13 NEW_LINE insertEdge ( 0 , 1 ) NEW_LINE insertEdge ( 0 , 2 ) NEW_LINE insertEdge ( 1 , 5 ) NEW_LINE insertEdge ( 1 , 6 ) NEW_LINE insertEdge ( 2 , 4 ) NEW_LINE insertEdge ( 2 , 9 ) NEW_LINE insertEdge ( 6 , 7 ) NEW_LINE insertEdge ( 6 , 8 ) NEW_LINE insertEdge ( 7 , 8 ) NEW_LINE insertEdge ( 2 , 3 ) NEW_LINE insertEdge ( 3 , 9 ) NEW_LINE insertEdge ( 3 , 10 ) NEW_LINE insertEdge ( 9 , 10 ) NEW_LINE dfs ( node ) NEW_LINE DEDENT
Find Maximum number possible by doing at | Python3 program to find maximum integer possible by doing at - most K swap operations on its digits . ; function to find maximum integer possible by doing at - most K swap operations on its digits ; return if no swaps left ; consider every digit ; and compare it with all digits after it ; if digit at position i is less than digit at position j , swap it and check for maximum number so far and recurse for remaining swaps ; swap string [ i ] with string [ j ] ; If current num is more than maximum so far ; recurse of the other k - 1 swaps ; backtrack ; Driver Code
def swap ( string , i , j ) : NEW_LINE INDENT return ( string [ : i ] + string [ j ] + string [ i + 1 : j ] + string [ i ] + string [ j + 1 : ] ) NEW_LINE DEDENT def findMaximumNum ( string , k , maxm ) : NEW_LINE INDENT if k == 0 : NEW_LINE INDENT return NEW_LINE DEDENT n = len ( string ) NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if string [ i ] < string [ j ] : NEW_LINE INDENT string = swap ( string , i , j ) NEW_LINE if string > maxm [ 0 ] : NEW_LINE INDENT maxm [ 0 ] = string NEW_LINE DEDENT findMaximumNum ( string , k - 1 , maxm ) NEW_LINE string = swap ( string , i , j ) NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = "129814999" NEW_LINE k = 4 NEW_LINE maxm = [ string ] NEW_LINE findMaximumNum ( string , k , maxm ) NEW_LINE print ( maxm [ 0 ] ) NEW_LINE DEDENT
Print all possible paths from top left to bottom right of a mXn matrix | Python3 program to Print all possible paths from top left to bottom right of a mXn matrix ; if we reach the bottom of maze , we can only move right ; path . append ( maze [ i ] [ k ] ) ; if we hit this block , it means one path is completed . Add it to paths list and print ; if we reach to the right most corner , we can only move down ; path . append ( maze [ j ] [ k ] ) if we hit this block , it means one path is completed . Add it to paths list and print ; add current element to the path list path . append ( maze [ i ] [ j ] ) ; move down in y direction and call findPathsUtil recursively ; move down in y direction and call findPathsUtil recursively ; Driver code
allPaths = [ ] NEW_LINE def findPaths ( maze , m , n ) : NEW_LINE INDENT path = [ 0 for d in range ( m + n - 1 ) ] NEW_LINE findPathsUtil ( maze , m , n , 0 , 0 , path , 0 ) NEW_LINE DEDENT def findPathsUtil ( maze , m , n , i , j , path , indx ) : NEW_LINE INDENT global allPaths NEW_LINE if i == m - 1 : NEW_LINE INDENT for k in range ( j , n ) : NEW_LINE INDENT path [ indx + k - j ] = maze [ i ] [ k ] NEW_LINE DEDENT print ( path ) NEW_LINE allPaths . append ( path ) NEW_LINE return NEW_LINE DEDENT if j == n - 1 : NEW_LINE INDENT for k in range ( i , m ) : NEW_LINE INDENT path [ indx + k - i ] = maze [ k ] [ j ] NEW_LINE DEDENT print ( path ) NEW_LINE allPaths . append ( path ) NEW_LINE return NEW_LINE DEDENT path [ indx ] = maze [ i ] [ j ] NEW_LINE findPathsUtil ( maze , m , n , i + 1 , j , path , indx + 1 ) NEW_LINE findPathsUtil ( maze , m , n , i , j + 1 , path , indx + 1 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT maze = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] NEW_LINE findPaths ( maze , 3 , 3 ) NEW_LINE DEDENT
Given an array A [ ] and a number x , check for pair in A [ ] with sum as x | Function to find and print pair ; Driver code
def chkPair ( A , size , x ) : NEW_LINE INDENT for i in range ( 0 , size - 1 ) : NEW_LINE INDENT for j in range ( i + 1 , size ) : NEW_LINE INDENT if ( A [ i ] + A [ j ] == x ) : NEW_LINE INDENT print ( " Pair ▁ with ▁ a ▁ given ▁ sum ▁ " , x , " ▁ is ▁ ( " , A [ i ] , " , ▁ " , A [ j ] , " ) " ) NEW_LINE return True NEW_LINE DEDENT DEDENT DEDENT return False NEW_LINE DEDENT A = [ 0 , - 1 , 2 , - 3 , 1 ] NEW_LINE x = - 2 NEW_LINE size = len ( A ) NEW_LINE if ( chkPair ( A , size , x ) ) : NEW_LINE INDENT print ( " Valid ▁ pair ▁ exists " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No ▁ valid ▁ pair ▁ exists ▁ for ▁ " , x ) NEW_LINE DEDENT
Significant Inversions in an Array | Function that sorts the input array and returns the number of inversions in the array ; Recursive function that sorts the input array and returns the number of inversions in the array ; Divide the array into two parts and call _mergeSortAndCountInv ( ) for each of the parts ; Inversion count will be sum of the inversions in the left - part , the right - part and the number of inversions in merging ; Merge the two parts ; Function that merges the two sorted arrays and returns the inversion count in the arrays ; i is the index for the left subarray ; j is the index for the right subarray ; k is the index for the resultant merged subarray ; First pass to count number of significant inversions ; i is the index for the left subarray ; j is the index for the right subarray ; k is the index for the resultant merged subarray ; Second pass to merge the two sorted arrays ; Copy the remaining elements of the left subarray ( if there are any ) to temp ; Copy the remaining elements of the right subarray ( if there are any ) to temp ; Copy back the merged elements to the original array ; Driver code
def mergeSort ( arr , array_size ) : NEW_LINE INDENT temp = [ 0 for i in range ( array_size ) ] NEW_LINE return _mergeSort ( arr , temp , 0 , array_size - 1 ) NEW_LINE DEDENT def _mergeSort ( arr , temp , left , right ) : NEW_LINE INDENT mid , inv_count = 0 , 0 NEW_LINE if ( right > left ) : NEW_LINE INDENT mid = ( right + left ) // 2 NEW_LINE inv_count = _mergeSort ( arr , temp , left , mid ) NEW_LINE inv_count += _mergeSort ( arr , temp , mid + 1 , right ) NEW_LINE inv_count += merge ( arr , temp , left , mid + 1 , right ) NEW_LINE DEDENT return inv_count NEW_LINE DEDENT def merge ( arr , temp , left , mid , right ) : NEW_LINE INDENT inv_count = 0 NEW_LINE i = left NEW_LINE j = mid NEW_LINE k = left NEW_LINE while ( ( i <= mid - 1 ) and ( j <= right ) ) : NEW_LINE INDENT if ( arr [ i ] > 2 * arr [ j ] ) : NEW_LINE INDENT inv_count += ( mid - i ) NEW_LINE j += 1 NEW_LINE DEDENT else : NEW_LINE INDENT i += 1 NEW_LINE DEDENT DEDENT i = left NEW_LINE j = mid NEW_LINE k = left NEW_LINE while ( ( i <= mid - 1 ) and ( j <= right ) ) : NEW_LINE INDENT if ( arr [ i ] <= arr [ j ] ) : NEW_LINE INDENT temp [ k ] = arr [ i ] NEW_LINE i , k = i + 1 , k + 1 NEW_LINE DEDENT else : NEW_LINE INDENT temp [ k ] = arr [ j ] NEW_LINE k , j = k + 1 , j + 1 NEW_LINE DEDENT DEDENT while ( i <= mid - 1 ) : NEW_LINE INDENT temp [ k ] = arr [ i ] NEW_LINE i , k = i + 1 , k + 1 NEW_LINE DEDENT while ( j <= right ) : NEW_LINE INDENT temp [ k ] = arr [ j ] NEW_LINE j , k = j + 1 , k + 1 NEW_LINE DEDENT for i in range ( left , right + 1 ) : NEW_LINE INDENT arr [ i ] = temp [ i ] NEW_LINE DEDENT return inv_count NEW_LINE DEDENT arr = [ 1 , 20 , 6 , 4 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( mergeSort ( arr , n ) ) NEW_LINE
Find the number of different numbers in the array after applying the given operation q times | Python3 implementation for above approach ; To store the tree in lazy propagation ; To store the different numbers ; Function to update in the range [ x , y ) with given value ; check out of bound ; check for complete overlap ; find the mid number ; check for pending updates ; make lazy [ id ] = 0 , so that it has no pending updates ; call for two child nodes ; Function to find non - zero integersin the range [ l , r ) ; if id contains positive number ; There is no need to see the children , because all the interval have same number ; check for out of bound ; find the middle number ; call for two child nodes ; Driver code ; size of the array and number of queries ; Update operation for l , r , x , id , 0 , n ; Query operation to get answer in the range [ 0 , n - 1 ] ; Print the count of non - zero elements
N = 100005 NEW_LINE lazy = [ 0 ] * ( 4 * N ) ; NEW_LINE se = set ( ) NEW_LINE def update ( x , y , value , id , l , r ) : NEW_LINE INDENT if ( x >= r or l >= y ) : NEW_LINE INDENT return ; NEW_LINE DEDENT if ( x <= l and r <= y ) : NEW_LINE INDENT lazy [ id ] = value ; NEW_LINE return ; NEW_LINE DEDENT mid = ( l + r ) // 2 ; NEW_LINE if ( lazy [ id ] ) : NEW_LINE INDENT lazy [ 2 * id ] = lazy [ 2 * id + 1 ] = lazy [ id ] ; NEW_LINE DEDENT lazy [ id ] = 0 ; NEW_LINE update ( x , y , value , 2 * id , l , mid ) ; NEW_LINE update ( x , y , value , 2 * id + 1 , mid , r ) ; NEW_LINE DEDENT def query ( id , l , r ) : NEW_LINE INDENT if ( lazy [ id ] ) : NEW_LINE INDENT se . add ( lazy [ id ] ) ; NEW_LINE return ; NEW_LINE DEDENT if ( r - l < 2 ) : NEW_LINE INDENT return ; NEW_LINE DEDENT mid = ( l + r ) // 2 ; NEW_LINE query ( 2 * id , l , mid ) ; NEW_LINE query ( 2 * id + 1 , mid , r ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 ; q = 3 ; NEW_LINE update ( 1 , 4 , 1 , 1 , 0 , n ) ; NEW_LINE update ( 0 , 2 , 2 , 1 , 0 , n ) ; NEW_LINE update ( 3 , 4 , 3 , 1 , 0 , n ) ; NEW_LINE query ( 1 , 0 , n ) ; NEW_LINE print ( len ( se ) ) ; NEW_LINE DEDENT
Find Nth term ( A matrix exponentiation example ) | Python3 program to find n - th term of a recursive function using matrix exponentiation . ; This power function returns first row of { Transformation Matrix } ^ n - 1 * Initial Vector ; This is an identity matrix . ; this is Transformation matrix . ; Matrix exponentiation to calculate power of { tMat } ^ n - 1 store res in " res " matrix . ; res store { Transformation matrix } ^ n - 1 hence will be first row of res * Initial Vector . ; Driver code
MOD = 1000000009 ; NEW_LINE def power ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT n -= 1 ; NEW_LINE res = [ [ 1 , 0 ] , [ 0 , 1 ] ] ; NEW_LINE tMat = [ [ 2 , 3 ] , [ 1 , 0 ] ] ; NEW_LINE while ( n ) : NEW_LINE INDENT if ( n & 1 ) : NEW_LINE INDENT tmp = [ [ 0 for x in range ( 2 ) ] for y in range ( 2 ) ] ; NEW_LINE tmp [ 0 ] [ 0 ] = ( res [ 0 ] [ 0 ] * tMat [ 0 ] [ 0 ] + res [ 0 ] [ 1 ] * tMat [ 1 ] [ 0 ] ) % MOD ; NEW_LINE tmp [ 0 ] [ 1 ] = ( res [ 0 ] [ 0 ] * tMat [ 0 ] [ 1 ] + res [ 0 ] [ 1 ] * tMat [ 1 ] [ 1 ] ) % MOD ; NEW_LINE tmp [ 1 ] [ 0 ] = ( res [ 1 ] [ 0 ] * tMat [ 0 ] [ 0 ] + res [ 1 ] [ 1 ] * tMat [ 1 ] [ 0 ] ) % MOD ; NEW_LINE tmp [ 1 ] [ 1 ] = ( res [ 1 ] [ 0 ] * tMat [ 0 ] [ 1 ] + res [ 1 ] [ 1 ] * tMat [ 1 ] [ 1 ] ) % MOD ; NEW_LINE res [ 0 ] [ 0 ] = tmp [ 0 ] [ 0 ] ; NEW_LINE res [ 0 ] [ 1 ] = tmp [ 0 ] [ 1 ] ; NEW_LINE res [ 1 ] [ 0 ] = tmp [ 1 ] [ 0 ] ; NEW_LINE res [ 1 ] [ 1 ] = tmp [ 1 ] [ 1 ] ; NEW_LINE DEDENT n = n // 2 ; NEW_LINE tmp = [ [ 0 for x in range ( 2 ) ] for y in range ( 2 ) ] ; NEW_LINE tmp [ 0 ] [ 0 ] = ( tMat [ 0 ] [ 0 ] * tMat [ 0 ] [ 0 ] + tMat [ 0 ] [ 1 ] * tMat [ 1 ] [ 0 ] ) % MOD ; NEW_LINE tmp [ 0 ] [ 1 ] = ( tMat [ 0 ] [ 0 ] * tMat [ 0 ] [ 1 ] + tMat [ 0 ] [ 1 ] * tMat [ 1 ] [ 1 ] ) % MOD ; NEW_LINE tmp [ 1 ] [ 0 ] = ( tMat [ 1 ] [ 0 ] * tMat [ 0 ] [ 0 ] + tMat [ 1 ] [ 1 ] * tMat [ 1 ] [ 0 ] ) % MOD ; NEW_LINE tmp [ 1 ] [ 1 ] = ( tMat [ 1 ] [ 0 ] * tMat [ 0 ] [ 1 ] + tMat [ 1 ] [ 1 ] * tMat [ 1 ] [ 1 ] ) % MOD ; NEW_LINE tMat [ 0 ] [ 0 ] = tmp [ 0 ] [ 0 ] ; NEW_LINE tMat [ 0 ] [ 1 ] = tmp [ 0 ] [ 1 ] ; NEW_LINE tMat [ 1 ] [ 0 ] = tmp [ 1 ] [ 0 ] ; NEW_LINE tMat [ 1 ] [ 1 ] = tmp [ 1 ] [ 1 ] ; NEW_LINE DEDENT return ( res [ 0 ] [ 0 ] * 1 + res [ 0 ] [ 1 ] * 1 ) % MOD ; NEW_LINE DEDENT n = 3 ; NEW_LINE print ( power ( n ) ) ; NEW_LINE
Shuffle 2 n integers in format { a1 , b1 , a2 , b2 , a3 , b3 , ... ... , an , bn } without using extra space | Function to shuffle an array of size 2 n ; Rotate the element to the left ; swap a [ j - 1 ] , a [ j ] ; Driver Code
def shuffleArray ( a , n ) : NEW_LINE INDENT i , q , k = 0 , 1 , n NEW_LINE while ( i < n ) : NEW_LINE INDENT j = k NEW_LINE while ( j > i + q ) : NEW_LINE INDENT a [ j - 1 ] , a [ j ] = a [ j ] , a [ j - 1 ] NEW_LINE j -= 1 NEW_LINE DEDENT i += 1 NEW_LINE k += 1 NEW_LINE q += 1 NEW_LINE DEDENT DEDENT a = [ 1 , 3 , 5 , 7 , 2 , 4 , 6 , 8 ] NEW_LINE n = len ( a ) NEW_LINE shuffleArray ( a , int ( n / 2 ) ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT print ( a [ i ] , end = " ▁ " ) NEW_LINE DEDENT