codeparrot/xlcost-text-to-code huggingface dataset:
Given a matrix of ' O ' and ' X ' , find the largest subsquare surrounded by ' X ' | Size of given matrix is N X N ; Initialize maxside with 0 ; Fill the dp matrix horizontally . for contiguous ' X ' increment the value of x , otherwise make it 0 ; Fill the dp matrix vertically . For contiguous ' X ' increment the value of y , otherwise make it 0 ; Now check , for every value of ( i , j ) if sub - square is possible , traverse back horizontally by value val , and check if vertical contiguous ' X ' enfing at ( i , j - val + 1 ) is greater than equal to val . Similarly , check if traversing back vertically , the horizontal contiguous ' X ' ending at ( i - val + 1 , j ) is greater than equal to val . ; Store the final answer in maxval ; Return the final answe . ; Driver code ; Function call
N = 6 NEW_LINE def maximumSubSquare ( arr ) : NEW_LINE INDENT dp = [ [ [ - 1 , - 1 ] for i in range ( 51 ) ] for j in range ( 51 ) ] NEW_LINE maxside = [ [ 0 for i in range ( 51 ) ] for j in range ( 51 ) ] NEW_LINE x = 0 NEW_LINE y = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT x = 0 NEW_LINE for j in range ( N ) : NEW_LINE INDENT if ( arr [ i ] [ j ] == ' X ' ) : NEW_LINE INDENT x += 1 NEW_LINE DEDENT else : NEW_LINE INDENT x = 0 NEW_LINE DEDENT dp [ i ] [ j ] [ 0 ] = x NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT if ( arr [ j ] [ i ] == ' X ' ) : NEW_LINE INDENT y += 1 NEW_LINE DEDENT else : NEW_LINE INDENT y = 0 NEW_LINE DEDENT dp [ j ] [ i ] [ 1 ] = y NEW_LINE DEDENT DEDENT maxval = 0 NEW_LINE val = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT val = min ( dp [ i ] [ j ] [ 0 ] , dp [ i ] [ j ] [ 1 ] ) NEW_LINE if ( dp [ i ] [ j - val + 1 ] [ 1 ] >= val and dp [ i - val + 1 ] [ j ] [ 0 ] >= val ) : NEW_LINE INDENT maxside [ i ] [ j ] = val NEW_LINE DEDENT else : NEW_LINE INDENT maxside [ i ] [ j ] = 0 NEW_LINE DEDENT maxval = max ( maxval , maxside [ i ] [ j ] ) NEW_LINE DEDENT DEDENT return maxval NEW_LINE DEDENT mat = [ [ ' X ' , ' O ' , ' X ' , ' X ' , ' X ' , ' X ' ] , [ ' X ' , ' O ' , ' X ' , ' X ' , ' O ' , ' X ' ] , [ ' X ' , ' X ' , ' X ' , ' O ' , ' O ' , ' X ' ] , [ ' O ' , ' X ' , ' X ' , ' X ' , ' X ' , ' X ' ] , [ ' X ' , ' X ' , ' X ' , ' O ' , ' X ' , ' O ' ] , [ ' O ' , ' O ' , ' X ' , ' O ' , ' O ' , ' O ' ] ] NEW_LINE print ( maximumSubSquare ( mat ) ) NEW_LINE
Flood fill Algorithm | Dimentions of paint screen ; A recursive function to replace previous color ' prevC ' at ' ( x , ▁ y ) ' and all surrounding pixels of ( x , y ) with new color ' newC ' and ; Base cases ; Replace the color at ( x , y ) ; Recur for north , east , south and west ; It mainly finds the previous color on ( x , y ) and calls floodFillUtil ( ) ; Driver Code
M = 8 NEW_LINE N = 8 NEW_LINE def floodFillUtil ( screen , x , y , prevC , newC ) : NEW_LINE INDENT if ( x < 0 or x >= M or y < 0 or y >= N or screen [ x ] [ y ] != prevC or screen [ x ] [ y ] == newC ) : NEW_LINE INDENT return NEW_LINE DEDENT screen [ x ] [ y ] = newC NEW_LINE floodFillUtil ( screen , x + 1 , y , prevC , newC ) NEW_LINE floodFillUtil ( screen , x - 1 , y , prevC , newC ) NEW_LINE floodFillUtil ( screen , x , y + 1 , prevC , newC ) NEW_LINE floodFillUtil ( screen , x , y - 1 , prevC , newC ) NEW_LINE DEDENT def floodFill ( screen , x , y , newC ) : NEW_LINE INDENT prevC = screen [ x ] [ y ] NEW_LINE floodFillUtil ( screen , x , y , prevC , newC ) NEW_LINE DEDENT screen = [ [ 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ] , [ 1 , 1 , 1 , 1 , 1 , 1 , 0 , 0 ] , [ 1 , 0 , 0 , 1 , 1 , 0 , 1 , 1 ] , [ 1 , 2 , 2 , 2 , 2 , 0 , 1 , 0 ] , [ 1 , 1 , 1 , 2 , 2 , 0 , 1 , 0 ] , [ 1 , 1 , 1 , 2 , 2 , 2 , 2 , 0 ] , [ 1 , 1 , 1 , 1 , 1 , 2 , 1 , 1 ] , [ 1 , 1 , 1 , 1 , 1 , 2 , 2 , 1 ] ] NEW_LINE x = 4 NEW_LINE y = 4 NEW_LINE newC = 3 NEW_LINE floodFill ( screen , x , y , newC ) NEW_LINE print ( " Updated ▁ screen ▁ after ▁ call ▁ to ▁ floodFill : " ) NEW_LINE for i in range ( M ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT print ( screen [ i ] [ j ] , end = ' ▁ ' ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT
Collect maximum points in a grid using two traversals | A Memoization based program to find maximum collection using two traversals of a grid ; checks whether a given input is valid or not ; Driver function to collect max value ; if P1 or P2 is at an invalid cell ; if both traversals reach their destinations ; If both traversals are at last row but not at their destination ; If subproblem is already solved ; Initialize answer for this subproblem ; this variable is used to store gain of current cell ( s ) ; Recur for all possible cases , then store and return the one with max value ; This is mainly a wrapper over recursive function getMaxUtil ( ) . This function creates a table for memoization and calls getMaxUtil ( ) ; Create a memoization table and initialize all entries as - 1 ; Calculation maximum value using memoization based function getMaxUtil ( ) ; Driver program to test above functions
R = 5 NEW_LINE C = 4 NEW_LINE intmin = - 10000000 NEW_LINE intmax = 10000000 NEW_LINE def isValid ( x , y1 , y2 ) : NEW_LINE INDENT return ( ( x >= 0 and x < R and y1 >= 0 and y1 < C and y2 >= 0 and y2 < C ) ) NEW_LINE DEDENT def getMaxUtil ( arr , mem , x , y1 , y2 ) : NEW_LINE INDENT if isValid ( x , y1 , y2 ) == False : NEW_LINE INDENT return intmin NEW_LINE DEDENT if x == R - 1 and y1 == 0 and y2 == C - 1 : NEW_LINE INDENT if y1 == y2 : NEW_LINE INDENT return arr [ x ] [ y1 ] NEW_LINE DEDENT else : NEW_LINE INDENT return arr [ x ] [ y1 ] + arr [ x ] [ y2 ] NEW_LINE DEDENT DEDENT if x == R - 1 : NEW_LINE INDENT return intmin NEW_LINE DEDENT if mem [ x ] [ y1 ] [ y2 ] != - 1 : NEW_LINE INDENT return mem [ x ] [ y1 ] [ y2 ] NEW_LINE DEDENT ans = intmin NEW_LINE temp = 0 NEW_LINE if y1 == y2 : NEW_LINE INDENT temp = arr [ x ] [ y1 ] NEW_LINE DEDENT else : NEW_LINE INDENT temp = arr [ x ] [ y1 ] + arr [ x ] [ y2 ] NEW_LINE DEDENT ans = max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 , y2 - 1 ) ) NEW_LINE ans = max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 , y2 + 1 ) ) NEW_LINE ans = max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 , y2 ) ) NEW_LINE ans = max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 - 1 , y2 ) ) NEW_LINE ans = max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 - 1 , y2 - 1 ) ) NEW_LINE ans = max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 - 1 , y2 + 1 ) ) NEW_LINE ans = max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 + 1 , y2 ) ) NEW_LINE ans = max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 + 1 , y2 - 1 ) ) NEW_LINE ans = max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 + 1 , y2 + 1 ) ) NEW_LINE mem [ x ] [ y1 ] [ y2 ] = ans NEW_LINE return ans NEW_LINE DEDENT def geMaxCollection ( arr ) : NEW_LINE INDENT mem = [ [ [ - 1 for i in range ( C ) ] for i in range ( C ) ] for i in range ( R ) ] NEW_LINE return getMaxUtil ( arr , mem , 0 , 0 , C - 1 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ [ 3 , 6 , 8 , 2 ] , [ 5 , 2 , 4 , 3 ] , [ 1 , 1 , 20 , 10 ] , [ 1 , 1 , 20 , 10 ] , [ 1 , 1 , 20 , 10 ] , ] NEW_LINE print ( ' Maximum ▁ collection ▁ is ▁ ' , geMaxCollection ( arr ) ) NEW_LINE DEDENT
Collect maximum coins before hitting a dead end | A Naive Recursive Python 3 program to find maximum number of coins that can be collected before hitting a dead end ; to check whether current cell is out of the grid or not ; dir = 0 for left , dir = 1 for facing right . This function returns number of maximum coins that can be collected starting from ( i , j ) . ; If this is a invalid cell or if cell is a blocking cell ; Check if this cell contains the coin ' C ' or if its empty ' E ' . ; Get the maximum of two cases when you are facing right in this cell ; Direction is right ; Direction is left Get the maximum of two cases when you are facing left in this cell ; Driver program to test above function ; As per the question initial cell is ( 0 , 0 ) and direction is right
R = 5 NEW_LINE C = 5 NEW_LINE def isValid ( i , j ) : NEW_LINE INDENT return ( i >= 0 and i < R and j >= 0 and j < C ) NEW_LINE DEDENT def maxCoinsRec ( arr , i , j , dir ) : NEW_LINE INDENT if ( isValid ( i , j ) == False or arr [ i ] [ j ] == ' # ' ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( arr [ i ] [ j ] == ' C ' ) : NEW_LINE INDENT result = 1 NEW_LINE DEDENT else : NEW_LINE INDENT result = 0 NEW_LINE DEDENT if ( dir == 1 ) : NEW_LINE INDENT return ( result + max ( maxCoinsRec ( arr , i + 1 , j , 0 ) , maxCoinsRec ( arr , i , j + 1 , 1 ) ) ) NEW_LINE DEDENT return ( result + max ( maxCoinsRec ( arr , i + 1 , j , 1 ) , maxCoinsRec ( arr , i , j - 1 , 0 ) ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ [ ' E ' , ' C ' , ' C ' , ' C ' , ' C ' ] , [ ' C ' , ' # ' , ' C ' , ' # ' , ' E ' ] , [ ' # ' , ' C ' , ' C ' , ' # ' , ' C ' ] , [ ' C ' , ' E ' , ' E ' , ' C ' , ' E ' ] , [ ' C ' , ' E ' , ' # ' , ' C ' , ' E ' ] ] NEW_LINE print ( " Maximum ▁ number ▁ of ▁ collected ▁ coins ▁ is ▁ " , maxCoinsRec ( arr , 0 , 0 , 1 ) ) NEW_LINE DEDENT
Find length of the longest consecutive path from a given starting character | Python3 program to find the longest consecutive path ; tool matrices to recur for adjacent cells . ; dp [ i ] [ j ] Stores length of longest consecutive path starting at arr [ i ] [ j ] . ; check whether mat [ i ] [ j ] is a valid cell or not . ; Check whether current character is adjacent to previous character ( character processed in parent call ) or not . ; i , j are the indices of the current cell and prev is the character processed in the parent call . . also mat [ i ] [ j ] is our current character . ; If this cell is not valid or current character is not adjacent to previous one ( e . g . d is not adjacent to b ) or if this cell is already included in the path than return 0. ; If this subproblem is already solved , return the answer ; Initialize answer ; recur for paths with different adjacent cells and store the length of longest path . ; save the answer and return ; Returns length of the longest path with all characters consecutive to each other . This function first initializes dp array that is used to store results of subproblems , then it calls recursive DFS based function getLenUtil ( ) to find max length path ; check for each possible starting point ; recur for all eight adjacent cells ; Driver program
R = 3 NEW_LINE C = 3 NEW_LINE x = [ 0 , 1 , 1 , - 1 , 1 , 0 , - 1 , - 1 ] NEW_LINE y = [ 1 , 0 , 1 , 1 , - 1 , - 1 , 0 , - 1 ] NEW_LINE dp = [ [ 0 for i in range ( C ) ] for i in range ( R ) ] NEW_LINE def isvalid ( i , j ) : NEW_LINE INDENT if ( i < 0 or j < 0 or i >= R or j >= C ) : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE DEDENT def isadjacent ( prev , curr ) : NEW_LINE INDENT if ( ord ( curr ) - ord ( prev ) ) == 1 : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def getLenUtil ( mat , i , j , prev ) : NEW_LINE INDENT if ( isvalid ( i , j ) == False or isadjacent ( prev , mat [ i ] [ j ] ) == False ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( dp [ i ] [ j ] != - 1 ) : NEW_LINE INDENT return dp [ i ] [ j ] NEW_LINE DEDENT ans = 0 NEW_LINE for k in range ( 8 ) : NEW_LINE INDENT ans = max ( ans , 1 + getLenUtil ( mat , i + x [ k ] , j + y [ k ] , mat [ i ] [ j ] ) ) NEW_LINE DEDENT dp [ i ] [ j ] = ans NEW_LINE return dp [ i ] [ j ] NEW_LINE DEDENT def getLen ( mat , s ) : NEW_LINE INDENT for i in range ( R ) : NEW_LINE INDENT for j in range ( C ) : NEW_LINE INDENT dp [ i ] [ j ] = - 1 NEW_LINE DEDENT DEDENT ans = 0 NEW_LINE for i in range ( R ) : NEW_LINE INDENT for j in range ( C ) : NEW_LINE INDENT if ( mat [ i ] [ j ] == s ) : NEW_LINE INDENT for k in range ( 8 ) : NEW_LINE INDENT ans = max ( ans , 1 + getLenUtil ( mat , i + x [ k ] , j + y [ k ] , s ) ) ; NEW_LINE DEDENT DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT mat = [ [ ' a ' , ' c ' , ' d ' ] , [ ' h ' , ' b ' , ' a ' ] , [ ' i ' , ' g ' , ' f ' ] ] NEW_LINE print ( getLen ( mat , ' a ' ) ) NEW_LINE print ( getLen ( mat , ' e ' ) ) NEW_LINE print ( getLen ( mat , ' b ' ) ) NEW_LINE print ( getLen ( mat , ' f ' ) ) NEW_LINE
Find the longest path in a matrix with given constraints | Python3 program to find the longest path in a matrix with given constraints ; Returns length of the longest path beginning with mat [ i ] [ j ] . This function mainly uses lookup table dp [ n ] [ n ] ; Base case ; If this subproblem is already solved ; To store the path lengths in all the four directions ; Since all numbers are unique and in range from 1 to n * n , there is atmost one possible direction from any cell ; If none of the adjacent fours is one greater we will take 1 otherwise we will pick maximum from all the four directions ; Returns length of the longest path beginning with any cell ; Initialize result ; Create a lookup table and fill all entries in it as - 1 ; Compute longest path beginning from all cells ; Update result if needed ; Driver program
n = 3 NEW_LINE def findLongestFromACell ( i , j , mat , dp ) : NEW_LINE INDENT if ( i < 0 or i >= n or j < 0 or j >= n ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( dp [ i ] [ j ] != - 1 ) : NEW_LINE INDENT return dp [ i ] [ j ] NEW_LINE DEDENT x , y , z , w = - 1 , - 1 , - 1 , - 1 NEW_LINE if ( j < n - 1 and ( ( mat [ i ] [ j ] + 1 ) == mat [ i ] [ j + 1 ] ) ) : NEW_LINE INDENT x = 1 + findLongestFromACell ( i , j + 1 , mat , dp ) NEW_LINE DEDENT if ( j > 0 and ( mat [ i ] [ j ] + 1 == mat [ i ] [ j - 1 ] ) ) : NEW_LINE INDENT y = 1 + findLongestFromACell ( i , j - 1 , mat , dp ) NEW_LINE DEDENT if ( i > 0 and ( mat [ i ] [ j ] + 1 == mat [ i - 1 ] [ j ] ) ) : NEW_LINE INDENT z = 1 + findLongestFromACell ( i - 1 , j , mat , dp ) NEW_LINE DEDENT if ( i < n - 1 and ( mat [ i ] [ j ] + 1 == mat [ i + 1 ] [ j ] ) ) : NEW_LINE INDENT w = 1 + findLongestFromACell ( i + 1 , j , mat , dp ) NEW_LINE DEDENT dp [ i ] [ j ] = max ( x , max ( y , max ( z , max ( w , 1 ) ) ) ) NEW_LINE return dp [ i ] [ j ] NEW_LINE DEDENT def finLongestOverAll ( mat ) : NEW_LINE INDENT result = 1 NEW_LINE dp = [ [ - 1 for i in range ( n ) ] for i in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT if ( dp [ i ] [ j ] == - 1 ) : NEW_LINE INDENT findLongestFromACell ( i , j , mat , dp ) NEW_LINE DEDENT result = max ( result , dp [ i ] [ j ] ) ; NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT mat = [ [ 1 , 2 , 9 ] , [ 5 , 3 , 8 ] , [ 4 , 6 , 7 ] ] NEW_LINE print ( " Length ▁ of ▁ the ▁ longest ▁ path ▁ is ▁ " , finLongestOverAll ( mat ) ) NEW_LINE
Minimum Initial Points to Reach Destination | Python3 program to find minimum initial points to reach destination ; dp [ i ] [ j ] represents the minimum initial points player should have so that when starts with cell ( i , j ) successfully reaches the destination cell ( m - 1 , n - 1 ) ; Base case ; Fill last row and last column as base to fill entire table ; fill the table in bottom - up fashion ; Driver code
import math as mt NEW_LINE R = 3 NEW_LINE C = 3 NEW_LINE def minInitialPoints ( points ) : NEW_LINE INDENT dp = [ [ 0 for x in range ( C + 1 ) ] for y in range ( R + 1 ) ] NEW_LINE m , n = R , C NEW_LINE if points [ m - 1 ] [ n - 1 ] > 0 : NEW_LINE INDENT dp [ m - 1 ] [ n - 1 ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT dp [ m - 1 ] [ n - 1 ] = abs ( points [ m - 1 ] [ n - 1 ] ) + 1 NEW_LINE DEDENT for i in range ( m - 2 , - 1 , - 1 ) : NEW_LINE INDENT dp [ i ] [ n - 1 ] = max ( dp [ i + 1 ] [ n - 1 ] - points [ i ] [ n - 1 ] , 1 ) NEW_LINE DEDENT for i in range ( 2 , - 1 , - 1 ) : NEW_LINE INDENT dp [ m - 1 ] [ i ] = max ( dp [ m - 1 ] [ i + 1 ] - points [ m - 1 ] [ i ] , 1 ) NEW_LINE DEDENT for i in range ( m - 2 , - 1 , - 1 ) : NEW_LINE INDENT for j in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT min_points_on_exit = min ( dp [ i + 1 ] [ j ] , dp [ i ] [ j + 1 ] ) NEW_LINE dp [ i ] [ j ] = max ( min_points_on_exit - points [ i ] [ j ] , 1 ) NEW_LINE DEDENT DEDENT return dp [ 0 ] [ 0 ] NEW_LINE DEDENT points = [ [ - 2 , - 3 , 3 ] , [ - 5 , - 10 , 1 ] , [ 10 , 30 , - 5 ] ] NEW_LINE print ( " Minimum ▁ Initial ▁ Points ▁ Required : " , minInitialPoints ( points ) ) NEW_LINE
Queue using Stacks | Python3 program to implement Queue using two stacks with costly enQueue ( ) ; Move all elements from s1 to s2 ; Push item into self . s1 ; Push everything back to s1 ; Dequeue an item from the queue ; if first stack is empty ; Return top of self . s1 ; Driver code
class Queue : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . s1 = [ ] NEW_LINE self . s2 = [ ] NEW_LINE DEDENT def enQueue ( self , x ) : NEW_LINE INDENT while len ( self . s1 ) != 0 : NEW_LINE INDENT self . s2 . append ( self . s1 [ - 1 ] ) NEW_LINE self . s1 . pop ( ) NEW_LINE DEDENT self . s1 . append ( x ) NEW_LINE while len ( self . s2 ) != 0 : NEW_LINE INDENT self . s1 . append ( self . s2 [ - 1 ] ) NEW_LINE self . s2 . pop ( ) NEW_LINE DEDENT DEDENT def deQueue ( self ) : NEW_LINE INDENT if len ( self . s1 ) == 0 : NEW_LINE INDENT print ( " Q ▁ is ▁ Empty " ) NEW_LINE DEDENT x = self . s1 [ - 1 ] NEW_LINE self . s1 . pop ( ) NEW_LINE return x NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT q = Queue ( ) NEW_LINE q . enQueue ( 1 ) NEW_LINE q . enQueue ( 2 ) NEW_LINE q . enQueue ( 3 ) NEW_LINE print ( q . deQueue ( ) ) NEW_LINE print ( q . deQueue ( ) ) NEW_LINE print ( q . deQueue ( ) ) NEW_LINE DEDENT
Convert left | Python3 program to convert left - right to down - right representation of binary tree Helper function that allocates a new node with the given data and None left and right poers . ; Construct to create a new node ; An Iterative level order traversal based function to convert left - right to down - right representation . ; Base Case ; Recursively convert left an right subtrees ; If left child is None , make right child as left as it is the first child . ; If left child is NOT None , then make right child as right of left child ; Set root 's right as None ; A utility function to traverse a tree stored in down - right form . ; Driver Code ; 1 / \ 2 3 / \ 4 5 / / \ 6 7 8
class newNode : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . key = key NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def convert ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT convert ( root . left ) NEW_LINE convert ( root . right ) NEW_LINE if ( root . left == None ) : NEW_LINE INDENT root . left = root . right NEW_LINE DEDENT else : NEW_LINE INDENT root . left . right = root . right NEW_LINE DEDENT root . right = None NEW_LINE DEDENT def downRightTraversal ( root ) : NEW_LINE INDENT if ( root != None ) : NEW_LINE INDENT print ( root . key , end = " ▁ " ) NEW_LINE downRightTraversal ( root . right ) NEW_LINE downRightTraversal ( root . left ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 1 ) NEW_LINE root . left = newNode ( 2 ) NEW_LINE root . right = newNode ( 3 ) NEW_LINE root . right . left = newNode ( 4 ) NEW_LINE root . right . right = newNode ( 5 ) NEW_LINE root . right . left . left = newNode ( 6 ) NEW_LINE root . right . right . left = newNode ( 7 ) NEW_LINE root . right . right . right = newNode ( 8 ) NEW_LINE convert ( root ) NEW_LINE print ( " Traversal ▁ of ▁ the ▁ tree ▁ converted " , " to ▁ down - right ▁ form " ) NEW_LINE downRightTraversal ( root ) NEW_LINE DEDENT
Convert a given tree to its Sum Tree | Node defintion ; Convert a given tree to a tree where every node contains sum of values of nodes in left and right subtrees in the original tree ; Base case ; Store the old value ; Recursively call for left and right subtrees and store the sum as new value of this node ; Return the sum of values of nodes in left and right subtrees and old_value of this node ; A utility function to print inorder traversal of a Binary Tree ; Utility function to create a new Binary Tree node ; Driver Code ; Constructing tree given in the above figure ; Print inorder traversal of the converted tree to test result of toSumTree ( )
class node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . left = None NEW_LINE self . right = None NEW_LINE self . data = data NEW_LINE DEDENT DEDENT def toSumTree ( Node ) : NEW_LINE INDENT if ( Node == None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT old_val = Node . data NEW_LINE Node . data = toSumTree ( Node . left ) + toSumTree ( Node . right ) NEW_LINE return Node . data + old_val NEW_LINE DEDENT def printInorder ( Node ) : NEW_LINE INDENT if ( Node == None ) : NEW_LINE INDENT return NEW_LINE DEDENT printInorder ( Node . left ) NEW_LINE print ( Node . data , end = " ▁ " ) NEW_LINE printInorder ( Node . right ) NEW_LINE DEDENT def newNode ( data ) : NEW_LINE INDENT temp = node ( 0 ) NEW_LINE temp . data = data NEW_LINE temp . left = None NEW_LINE temp . right = None NEW_LINE return temp NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT root = None NEW_LINE x = 0 NEW_LINE root = newNode ( 10 ) NEW_LINE root . left = newNode ( - 2 ) NEW_LINE root . right = newNode ( 6 ) NEW_LINE root . left . left = newNode ( 8 ) NEW_LINE root . left . right = newNode ( - 4 ) NEW_LINE root . right . left = newNode ( 7 ) NEW_LINE root . right . right = newNode ( 5 ) NEW_LINE toSumTree ( root ) NEW_LINE print ( " Inorder ▁ Traversal ▁ of ▁ the ▁ resultant ▁ tree ▁ is : ▁ " ) NEW_LINE printInorder ( root ) NEW_LINE DEDENT
Find a peak element | Find the peak element in the array ; first or last element is peak element ; check for every other element ; check if the neighbors are smaller ; Driver code .
def findPeak ( arr , n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE return 0 NEW_LINE if ( arr [ 0 ] >= arr [ 1 ] ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( arr [ n - 1 ] >= arr [ n - 2 ] ) : NEW_LINE INDENT return n - 1 NEW_LINE DEDENT for i in range ( 1 , n - 1 ) : NEW_LINE INDENT if ( arr [ i ] >= arr [ i - 1 ] and arr [ i ] >= arr [ i + 1 ] ) : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT DEDENT arr = [ 1 , 3 , 20 , 4 , 1 , 0 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Index ▁ of ▁ a ▁ peak ▁ point ▁ is " , findPeak ( arr , n ) ) NEW_LINE
Find a peak element | A binary search based function that returns index of a peak element ; Find index of middle element ( low + high ) / 2 ; Compare middle element with its neighbours ( if neighbours exist ) ; If middle element is not peak and its left neighbour is greater than it , then left half must have a peak element ; If middle element is not peak and its right neighbour is greater than it , then right half must have a peak element ; A wrapper over recursive function findPeakUtil ( ) ; Driver code
def findPeakUtil ( arr , low , high , n ) : NEW_LINE INDENT mid = low + ( high - low ) / 2 NEW_LINE mid = int ( mid ) NEW_LINE if ( ( mid == 0 or arr [ mid - 1 ] <= arr [ mid ] ) and ( mid == n - 1 or arr [ mid + 1 ] <= arr [ mid ] ) ) : NEW_LINE INDENT return mid NEW_LINE DEDENT elif ( mid > 0 and arr [ mid - 1 ] > arr [ mid ] ) : NEW_LINE INDENT return findPeakUtil ( arr , low , ( mid - 1 ) , n ) NEW_LINE DEDENT else : NEW_LINE INDENT return findPeakUtil ( arr , ( mid + 1 ) , high , n ) NEW_LINE DEDENT DEDENT def findPeak ( arr , n ) : NEW_LINE INDENT return findPeakUtil ( arr , 0 , n - 1 , n ) NEW_LINE DEDENT arr = [ 1 , 3 , 20 , 4 , 1 , 0 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Index ▁ of ▁ a ▁ peak ▁ point ▁ is " , findPeak ( arr , n ) ) NEW_LINE
Find the two repeating elements in a given array | Print Repeating function ; Driver code
def printRepeating ( arr , size ) : NEW_LINE INDENT print ( " Repeating ▁ elements ▁ are ▁ " , end = ' ' ) NEW_LINE for i in range ( 0 , size ) : NEW_LINE INDENT for j in range ( i + 1 , size ) : NEW_LINE INDENT if arr [ i ] == arr [ j ] : NEW_LINE INDENT print ( arr [ i ] , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT DEDENT DEDENT arr = [ 4 , 2 , 4 , 5 , 2 , 3 , 1 ] NEW_LINE arr_size = len ( arr ) NEW_LINE printRepeating ( arr , arr_size ) NEW_LINE