codeparrot/xlcost-text-to-code huggingface dataset:
Delete middle element of a stack | Deletes middle of stack of size n . Curr is current item number ; If stack is empty or all items are traversed ; Remove current item ; Remove other items ; Put all items back except middle ; Driver function to test above functions ; push elements into the stack ; Printing stack after deletion of middle .
class Stack : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . items = [ ] NEW_LINE DEDENT def isEmpty ( self ) : NEW_LINE INDENT return self . items == [ ] NEW_LINE DEDENT def push ( self , item ) : NEW_LINE INDENT self . items . append ( item ) NEW_LINE DEDENT def pop ( self ) : NEW_LINE INDENT return self . items . pop ( ) NEW_LINE DEDENT def peek ( self ) : NEW_LINE INDENT return self . items [ len ( self . items ) - 1 ] NEW_LINE DEDENT def size ( self ) : NEW_LINE INDENT return len ( self . items ) NEW_LINE DEDENT DEDENT def deleteMid ( st , n , curr ) : NEW_LINE INDENT if ( st . isEmpty ( ) or curr == n ) : NEW_LINE INDENT return NEW_LINE DEDENT x = st . peek ( ) NEW_LINE st . pop ( ) NEW_LINE deleteMid ( st , n , curr + 1 ) NEW_LINE if ( curr != int ( n / 2 ) ) : NEW_LINE INDENT st . push ( x ) NEW_LINE DEDENT DEDENT st = Stack ( ) NEW_LINE st . push ( '1' ) NEW_LINE st . push ( '2' ) NEW_LINE st . push ( '3' ) NEW_LINE st . push ( '4' ) NEW_LINE st . push ( '5' ) NEW_LINE st . push ( '6' ) NEW_LINE st . push ( '7' ) NEW_LINE deleteMid ( st , st . size ( ) , 0 ) NEW_LINE while ( st . isEmpty ( ) == False ) : NEW_LINE INDENT p = st . peek ( ) NEW_LINE st . pop ( ) NEW_LINE print ( str ( p ) + " ▁ " , end = " " ) NEW_LINE DEDENT
Sorting array using Stacks | This function return the sorted stack ; pop out the first element ; while temporary stack is not empty and top of stack is smaller than temp ; pop from temporary stack and append it to the input stack ; append temp in tempory of stack ; append array elements to stack ; Sort the temporary stack ; Put stack elements in arrp [ ] ; Driver code
def sortStack ( input ) : NEW_LINE INDENT tmpStack = [ ] NEW_LINE while ( len ( input ) > 0 ) : NEW_LINE INDENT tmp = input [ - 1 ] NEW_LINE input . pop ( ) NEW_LINE while ( len ( tmpStack ) > 0 and tmpStack [ - 1 ] < tmp ) : NEW_LINE INDENT input . append ( tmpStack [ - 1 ] ) NEW_LINE tmpStack . pop ( ) NEW_LINE DEDENT tmpStack . append ( tmp ) NEW_LINE DEDENT return tmpStack NEW_LINE DEDENT def sortArrayUsingStacks ( arr , n ) : NEW_LINE INDENT input = [ ] NEW_LINE i = 0 NEW_LINE while ( i < n ) : NEW_LINE INDENT input . append ( arr [ i ] ) NEW_LINE i = i + 1 NEW_LINE DEDENT tmpStack = sortStack ( input ) NEW_LINE i = 0 NEW_LINE while ( i < n ) : NEW_LINE INDENT arr [ i ] = tmpStack [ - 1 ] NEW_LINE tmpStack . pop ( ) NEW_LINE i = i + 1 NEW_LINE DEDENT return arr NEW_LINE DEDENT arr = [ 10 , 5 , 15 , 45 ] NEW_LINE n = len ( arr ) NEW_LINE arr = sortArrayUsingStacks ( arr , n ) NEW_LINE i = 0 NEW_LINE while ( i < n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE i = i + 1 NEW_LINE DEDENT
Reverse individual words | reverses individual words of a string ; Traverse given string and push all characters to stack until we see a space . ; When we see a space , we print contents of stack . ; Since there may not be space after last word . ; Driver Code
def reverserWords ( string ) : NEW_LINE INDENT st = list ( ) NEW_LINE for i in range ( len ( string ) ) : NEW_LINE INDENT if string [ i ] != " ▁ " : NEW_LINE INDENT st . append ( string [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT while len ( st ) > 0 : NEW_LINE INDENT print ( st [ - 1 ] , end = " " ) NEW_LINE st . pop ( ) NEW_LINE DEDENT print ( end = " ▁ " ) NEW_LINE DEDENT DEDENT while len ( st ) > 0 : NEW_LINE INDENT print ( st [ - 1 ] , end = " " ) NEW_LINE st . pop ( ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " Geeks ▁ for ▁ Geeks " NEW_LINE reverserWords ( string ) NEW_LINE DEDENT
Count subarrays where second highest lie before highest | Python3 program to count number of distinct instance where second highest number lie before highest number in all subarrays . ; Finding the next greater element of the array . ; Finding the previous greater element of the array . ; Wrapper Function ; Finding previous largest element ; Finding next largest element ; Driver Code
from typing import List NEW_LINE MAXN = 100005 NEW_LINE def makeNext ( arr : List [ int ] , n : int , nextBig : List [ int ] ) -> None : NEW_LINE INDENT s = [ ] NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT nextBig [ i ] = i NEW_LINE while len ( s ) and s [ - 1 ] [ 0 ] < arr [ i ] : NEW_LINE INDENT s . pop ( ) NEW_LINE DEDENT if len ( s ) : NEW_LINE INDENT nextBig [ i ] = s [ - 1 ] [ 1 ] NEW_LINE DEDENT s . append ( ( arr [ i ] , i ) ) NEW_LINE DEDENT DEDENT def makePrev ( arr : List [ int ] , n : int , prevBig : List [ int ] ) -> None : NEW_LINE INDENT s = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT prevBig [ i ] = - 1 NEW_LINE while ( len ( s ) and s [ - 1 ] [ 0 ] < arr [ i ] ) : NEW_LINE INDENT s . pop ( ) NEW_LINE DEDENT if ( len ( s ) ) : NEW_LINE INDENT prevBig [ i ] = s [ - 1 ] [ 1 ] NEW_LINE DEDENT s . append ( ( arr [ i ] , i ) ) NEW_LINE DEDENT DEDENT def wrapper ( arr : List [ int ] , n : int ) -> int : NEW_LINE INDENT nextBig = [ 0 ] * MAXN NEW_LINE prevBig = [ 0 ] * MAXN NEW_LINE maxi = [ 0 ] * MAXN NEW_LINE ans = 0 NEW_LINE makePrev ( arr , n , prevBig ) NEW_LINE makeNext ( arr , n , nextBig ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( nextBig [ i ] != i ) : NEW_LINE INDENT maxi [ nextBig [ i ] - i ] = max ( maxi [ nextBig [ i ] - i ] , i - prevBig [ i ] ) NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT ans += maxi [ i ] NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 3 , 2 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE print ( wrapper ( arr , n ) ) NEW_LINE DEDENT
Program for Tower of Hanoi | Recursive Python function to solve tower of hanoi ; Number of disks ; A , C , B are the name of rods
def TowerOfHanoi ( n , from_rod , to_rod , aux_rod ) : NEW_LINE INDENT if n == 1 : NEW_LINE INDENT print ( " Move ▁ disk ▁ 1 ▁ from ▁ rod " , from_rod , " to ▁ rod " , to_rod ) NEW_LINE return NEW_LINE DEDENT TowerOfHanoi ( n - 1 , from_rod , aux_rod , to_rod ) NEW_LINE print ( " Move ▁ disk " , n , " from ▁ rod " , from_rod , " to ▁ rod " , to_rod ) NEW_LINE TowerOfHanoi ( n - 1 , aux_rod , to_rod , from_rod ) NEW_LINE DEDENT n = 4 NEW_LINE TowerOfHanoi ( n , ' A ' , ' C ' , ' B ' ) NEW_LINE
Find maximum of minimum for every window size in a given array | A naive method to find maximum of minimum of all windows of different sizes ; Consider all windows of different sizes starting from size 1 ; Initialize max of min for current window size k ; Traverse through all windows of current size k ; Find minimum of current window ; Update maxOfMin if required ; Print max of min for current window size ; Driver Code
INT_MIN = - 1000000 NEW_LINE def printMaxOfMin ( arr , n ) : NEW_LINE INDENT for k in range ( 1 , n + 1 ) : NEW_LINE INDENT maxOfMin = INT_MIN ; NEW_LINE for i in range ( n - k + 1 ) : NEW_LINE INDENT min = arr [ i ] NEW_LINE for j in range ( k ) : NEW_LINE INDENT if ( arr [ i + j ] < min ) : NEW_LINE INDENT min = arr [ i + j ] NEW_LINE DEDENT DEDENT if ( min > maxOfMin ) : NEW_LINE INDENT maxOfMin = min NEW_LINE DEDENT DEDENT print ( maxOfMin , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 10 , 20 , 30 , 50 , 10 , 70 , 30 ] NEW_LINE n = len ( arr ) NEW_LINE printMaxOfMin ( arr , n ) NEW_LINE
Find maximum of minimum for every window size in a given array | An efficient Python3 program to find maximum of all minimums of windows of different sizes ; Used to find previous and next smaller ; Initialize elements of left [ ] and right [ ] ; Fill elements of left [ ] using logic discussed on www . geeksforgeeks . org / next - greater - element https : ; Empty the stack as stack is going to be used for right [ ] ; Fill elements of right [ ] using same logic ; Create and initialize answer array ; Fill answer array by comparing minimums of all . Lengths computed using left [ ] and right [ ] ; Length of the interval ; arr [ i ] is a possible answer for this Length ' Len ' interval , check if arr [ i ] is more than max for 'Len   ; Some entries in ans [ ] may not be filled yet . Fill them by taking values from right side of ans [ ] ; Print the result ; Driver Code
def printMaxOfMin ( arr , n ) : NEW_LINE INDENT s = [ ] NEW_LINE left = [ - 1 ] * ( n + 1 ) NEW_LINE right = [ n ] * ( n + 1 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT while ( len ( s ) != 0 and arr [ s [ - 1 ] ] >= arr [ i ] ) : NEW_LINE INDENT s . pop ( ) NEW_LINE DEDENT if ( len ( s ) != 0 ) : NEW_LINE INDENT left [ i ] = s [ - 1 ] NEW_LINE DEDENT s . append ( i ) NEW_LINE DEDENT while ( len ( s ) != 0 ) : NEW_LINE INDENT s . pop ( ) NEW_LINE DEDENT for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT while ( len ( s ) != 0 and arr [ s [ - 1 ] ] >= arr [ i ] ) : NEW_LINE INDENT s . pop ( ) NEW_LINE DEDENT if ( len ( s ) != 0 ) : NEW_LINE INDENT right [ i ] = s [ - 1 ] NEW_LINE DEDENT s . append ( i ) NEW_LINE DEDENT ans = [ 0 ] * ( n + 1 ) NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT ans [ i ] = 0 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT Len = right [ i ] - left [ i ] - 1 NEW_LINE ans [ Len ] = max ( ans [ Len ] , arr [ i ] ) NEW_LINE DEDENT for i in range ( n - 1 , 0 , - 1 ) : NEW_LINE INDENT ans [ i ] = max ( ans [ i ] , ans [ i + 1 ] ) NEW_LINE DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT print ( ans [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 10 , 20 , 30 , 50 , 10 , 70 , 30 ] NEW_LINE n = len ( arr ) NEW_LINE printMaxOfMin ( arr , n ) NEW_LINE DEDENT
Length of the longest valid substring | method to get length of the longest valid ; Create a stack and push - 1 as initial index to it . ; Initialize result ; Traverse all characters of given string ; If opening bracket , push index of it ; If closing bracket ; Pop the previous opening bracket 's index ; Check if this length formed with base of current valid substring is more than max so far ; If stack is empty . push current index as base for next valid substring ( if any ) ; Driver code ; Function call ; Function call
def findMaxLen ( string ) : NEW_LINE INDENT n = len ( string ) NEW_LINE stk = [ ] NEW_LINE stk . append ( - 1 ) NEW_LINE result = 0 NEW_LINE for i in xrange ( n ) : NEW_LINE INDENT if string [ i ] == ' ( ' : NEW_LINE INDENT stk . append ( i ) NEW_LINE DEDENT else : NEW_LINE INDENT if len ( stk ) != 0 : NEW_LINE stk . pop ( ) NEW_LINE if len ( stk ) != 0 : NEW_LINE INDENT result = max ( result , i - stk [ len ( stk ) - 1 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT stk . append ( i ) NEW_LINE DEDENT DEDENT DEDENT return result NEW_LINE DEDENT string = " ( ( ( ) ( ) " NEW_LINE print findMaxLen ( string ) NEW_LINE string = " ( ) ( ( ) ) ) ) ) " NEW_LINE print findMaxLen ( string ) NEW_LINE
Convert a tree to forest of even nodes | Return the number of nodes of subtree having node as a root . ; Mark node as visited . ; Traverse the adjacency list to find non - visited node . ; Finding number of nodes of the subtree of a subtree . ; If nodes are even , increment number of edges to removed . Else leave the node as child of subtree . ; Return the maximum number of edge to remove to make forest . ; Driver Code
def dfs ( tree , visit , ans , node ) : NEW_LINE INDENT num = 0 NEW_LINE temp = 0 NEW_LINE visit [ node ] = 1 NEW_LINE for i in range ( len ( tree [ node ] ) ) : NEW_LINE INDENT if ( visit [ tree [ node ] [ i ] ] == 0 ) : NEW_LINE INDENT temp = dfs ( tree , visit , ans , tree [ node ] [ i ] ) NEW_LINE if ( temp % 2 ) : NEW_LINE INDENT num += temp NEW_LINE DEDENT else : NEW_LINE INDENT ans [ 0 ] += 1 NEW_LINE DEDENT DEDENT DEDENT return num + 1 NEW_LINE DEDENT def minEdge ( tree , n ) : NEW_LINE INDENT visit = [ 0 ] * ( n + 2 ) NEW_LINE ans = [ 0 ] NEW_LINE dfs ( tree , visit , ans , 1 ) NEW_LINE return ans [ 0 ] NEW_LINE DEDENT N = 12 NEW_LINE n = 10 NEW_LINE tree = [ [ ] for i in range ( n + 2 ) ] NEW_LINE tree [ 1 ] . append ( 3 ) NEW_LINE tree [ 3 ] . append ( 1 ) NEW_LINE tree [ 1 ] . append ( 6 ) NEW_LINE tree [ 6 ] . append ( 1 ) NEW_LINE tree [ 1 ] . append ( 2 ) NEW_LINE tree [ 2 ] . append ( 1 ) NEW_LINE tree [ 3 ] . append ( 4 ) NEW_LINE tree [ 4 ] . append ( 3 ) NEW_LINE tree [ 6 ] . append ( 8 ) NEW_LINE tree [ 8 ] . append ( 6 ) NEW_LINE tree [ 2 ] . append ( 7 ) NEW_LINE tree [ 7 ] . append ( 2 ) NEW_LINE tree [ 2 ] . append ( 5 ) NEW_LINE tree [ 5 ] . append ( 2 ) NEW_LINE tree [ 4 ] . append ( 9 ) NEW_LINE tree [ 9 ] . append ( 4 ) NEW_LINE tree [ 4 ] . append ( 10 ) NEW_LINE tree [ 10 ] . append ( 4 ) NEW_LINE print ( minEdge ( tree , n ) ) NEW_LINE
Length of the longest valid substring | Python3 program to find length of the longest valid substring ; Initialize curMax to zero ; Iterate over the string starting from second index ; Driver Code ; Function call ; Function call
def findMaxLen ( s ) : NEW_LINE INDENT if ( len ( s ) <= 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT curMax = 0 NEW_LINE longest = [ 0 ] * ( len ( s ) ) NEW_LINE for i in range ( 1 , len ( s ) ) : NEW_LINE INDENT if ( ( s [ i ] == ' ) ' and i - longest [ i - 1 ] - 1 >= 0 and s [ i - longest [ i - 1 ] - 1 ] == ' ( ' ) ) : NEW_LINE INDENT longest [ i ] = longest [ i - 1 ] + 2 NEW_LINE if ( i - longest [ i - 1 ] - 2 >= 0 ) : NEW_LINE INDENT longest [ i ] += ( longest [ i - longest [ i - 1 ] - 2 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT longest [ i ] += 0 NEW_LINE DEDENT curMax = max ( longest [ i ] , curMax ) NEW_LINE DEDENT DEDENT return curMax NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT Str = " ( ( ( ) ( ) " NEW_LINE print ( findMaxLen ( Str ) ) NEW_LINE Str = " ( ) ( ( ) ) ) ) ) " NEW_LINE print ( findMaxLen ( Str ) ) NEW_LINE DEDENT
Length of the longest valid substring | Function to return the length of the longest valid substring ; Variables for left and right counter . maxlength to store the maximum length found so far ; Iterating the string from left to right ; If " ( " is encountered , then left counter is incremented else right counter is incremented ; Whenever left is equal to right , it signifies that the subsequence is valid and ; Reseting the counters when the subsequence becomes invalid ; Iterating the string from right to left ; If " ( " is encountered , then left counter is incremented else right counter is incremented ; Whenever left is equal to right , it signifies that the subsequence is valid and ; Reseting the counters when the subsequence becomes invalid ; Function call
def solve ( s , n ) : NEW_LINE INDENT left = 0 NEW_LINE right = 0 NEW_LINE maxlength = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( s [ i ] == ' ( ' ) : NEW_LINE INDENT left += 1 NEW_LINE DEDENT else : NEW_LINE INDENT right += 1 NEW_LINE DEDENT if ( left == right ) : NEW_LINE INDENT maxlength = max ( maxlength , 2 * right ) NEW_LINE DEDENT elif ( right > left ) : NEW_LINE INDENT left = right = 0 NEW_LINE DEDENT DEDENT left = right = 0 NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( s [ i ] == ' ( ' ) : NEW_LINE INDENT left += 1 NEW_LINE DEDENT else : NEW_LINE INDENT right += 1 NEW_LINE DEDENT if ( left == right ) : NEW_LINE INDENT maxlength = max ( maxlength , 2 * left ) NEW_LINE DEDENT elif ( left > right ) : NEW_LINE INDENT left = right = 0 NEW_LINE DEDENT DEDENT return maxlength NEW_LINE DEDENT print ( solve ( " ( ( ( ) ( ) ( ) ( ) ( ( ( ( ) ) " , 16 ) ) NEW_LINE
Expression contains redundant bracket or not | Function to check redundant brackets in a balanced expression ; create a stack of characters ; Iterate through the given expression ; if current character is close parenthesis ')   ; If immediate pop have open parenthesis ' ( ' duplicate brackets found ; Check for operators in expression ; Fetch top element of stack ; If operators not found ; append open parenthesis ' ( ' , ; operators and operands to stack ; Function to check redundant brackets ; Driver code
def checkRedundancy ( Str ) : NEW_LINE INDENT st = [ ] NEW_LINE for ch in Str : NEW_LINE INDENT if ( ch == ' ) ' ) : NEW_LINE INDENT top = st [ - 1 ] NEW_LINE st . pop ( ) NEW_LINE flag = True NEW_LINE while ( top != ' ( ' ) : NEW_LINE INDENT if ( top == ' + ' or top == ' - ' or top == ' * ' or top == ' / ' ) : NEW_LINE INDENT flag = False NEW_LINE DEDENT top = st [ - 1 ] NEW_LINE st . pop ( ) NEW_LINE DEDENT if ( flag == True ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT st . append ( ch ) NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def findRedundant ( Str ) : NEW_LINE INDENT ans = checkRedundancy ( Str ) NEW_LINE if ( ans == True ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT Str = " ( ( a + b ) ) " NEW_LINE findRedundant ( Str ) NEW_LINE Str = " ( a + ( b ) / c ) " NEW_LINE findRedundant ( Str ) NEW_LINE Str = " ( a + b * ( c - d ) ) " NEW_LINE findRedundant ( Str ) NEW_LINE DEDENT
Check if two expressions with brackets are same | Python3 program to check if two expressions evaluate to same . ; Return local sign of the operand . For example , in the expr a - b - ( c ) , local signs of the operands are + a , - b , + c ; Evaluate expressions into the count vector of the 26 alphabets . If add is True , then add count to the count vector of the alphabets , else remove count from the count vector . ; stack stores the global sign for operands . ; + means True global sign is positive initially ; global sign for the bracket is pushed to the stack ; global sign is popped out which was pushed in for the last bracket ; global sign is positive ( we use different values in two calls of functions so that we finally check if all vector elements are 0. ; global sign is negative here ; Returns True if expr1 and expr2 represent same expressions ; Create a vector for all operands and initialize the vector as 0. ; Put signs of all operands in expr1 ; Subtract signs of operands in expr2 ; If expressions are same , vector must be 0. ; Driver Code
MAX_CHAR = 26 ; NEW_LINE def adjSign ( s , i ) : NEW_LINE INDENT if ( i == 0 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT if ( s [ i - 1 ] == ' - ' ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT return True ; NEW_LINE DEDENT def eval ( s , v , add ) : NEW_LINE INDENT stk = [ ] NEW_LINE stk . append ( True ) ; NEW_LINE i = 0 ; NEW_LINE while ( i < len ( s ) ) : NEW_LINE INDENT if ( s [ i ] == ' + ' or s [ i ] == ' - ' ) : NEW_LINE i += 1 NEW_LINE continue ; NEW_LINE if ( s [ i ] == ' ( ' ) : NEW_LINE if ( adjSign ( s , i ) ) : NEW_LINE INDENT stk . append ( stk [ - 1 ] ) ; NEW_LINE DEDENT else : NEW_LINE INDENT stk . append ( not stk [ - 1 ] ) ; NEW_LINE DEDENT elif ( s [ i ] == ' ) ' ) : NEW_LINE stk . pop ( ) ; NEW_LINE else : NEW_LINE if ( stk [ - 1 ] ) : NEW_LINE INDENT v [ ord ( s [ i ] ) - ord ( ' a ' ) ] += ( 1 if add else - 1 ) if adjSign ( s , i ) else ( - 1 if add else 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT v [ ord ( s [ i ] ) - ord ( ' a ' ) ] += ( - 1 if add else 1 ) if adjSign ( s , i ) else ( 1 if add else - 1 ) NEW_LINE DEDENT i += 1 NEW_LINE DEDENT DEDENT def areSame ( expr1 , expr2 ) : NEW_LINE INDENT v = [ 0 for i in range ( MAX_CHAR ) ] ; NEW_LINE eval ( expr1 , v , True ) ; NEW_LINE eval ( expr2 , v , False ) ; NEW_LINE for i in range ( MAX_CHAR ) : NEW_LINE INDENT if ( v [ i ] != 0 ) : NEW_LINE return False ; NEW_LINE DEDENT return True ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT expr1 = " - ( a + b + c ) " NEW_LINE expr2 = " - a - b - c " ; NEW_LINE if ( areSame ( expr1 , expr2 ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT
Find index of closing bracket for a given opening bracket in an expression | Python program to find index of closing bracket for a given opening bracket . ; Function to find index of closing bracket for given opening bracket . ; If input is invalid . ; Create a deque to use it as a stack . ; Traverse through all elements starting from i . ; Push all starting brackets ; Pop a starting bracket for every closing bracket ; If deque becomes empty ; test function ; Driver code to test above method . ; should be 8 ; should be 7 ; should be 12 ; No matching bracket ; execution
from collections import deque NEW_LINE def getIndex ( s , i ) : NEW_LINE INDENT if s [ i ] != ' [ ' : NEW_LINE INDENT return - 1 NEW_LINE DEDENT d = deque ( ) NEW_LINE for k in range ( i , len ( s ) ) : NEW_LINE INDENT if s [ k ] == ' [ ' : NEW_LINE INDENT d . append ( s [ i ] ) NEW_LINE DEDENT elif s [ k ] == ' ] ' : NEW_LINE INDENT d . popleft ( ) NEW_LINE DEDENT if not d : NEW_LINE INDENT return k NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT def test ( s , i ) : NEW_LINE INDENT matching_index = getIndex ( s , i ) NEW_LINE print ( s + " , ▁ " + str ( i ) + " : ▁ " + str ( matching_index ) ) NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT test ( " [ ABC [ 23 ] ] [89 ] " , 0 ) NEW_LINE test ( " [ ABC [ 23 ] ] [89 ] " , 4 ) NEW_LINE test ( " [ ABC [ 23 ] ] [89 ] " , 9 ) NEW_LINE test ( " [ ABC [ 23 ] ] [89 ] " , 1 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT main ( ) NEW_LINE DEDENT
Convert a given Binary tree to a tree that holds Logical AND property | Program to convert an aribitary binary tree to a tree that holds children sum property Helper function that allocates a new node with the given data and None left and right poers . ; Construct to create a new node ; Convert the given tree to a tree where each node is logical AND of its children The main idea is to do Postorder traversal ; first recur on left child ; then recur on right child ; first recur on left child ; then print the data of node ; now recur on right child ; Driver Code ; Create following Binary Tree 1 / \ 1 0 / \ / \ 0 1 1 1
class newNode : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . data = key NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def convertTree ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT convertTree ( root . left ) NEW_LINE convertTree ( root . right ) NEW_LINE if ( root . left != None and root . right != None ) : NEW_LINE INDENT root . data = ( ( root . left . data ) & ( root . right . data ) ) NEW_LINE DEDENT DEDENT def printInorder ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT printInorder ( root . left ) NEW_LINE print ( root . data , end = " ▁ " ) NEW_LINE printInorder ( root . right ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 0 ) NEW_LINE root . left = newNode ( 1 ) NEW_LINE root . right = newNode ( 0 ) NEW_LINE root . left . left = newNode ( 0 ) NEW_LINE root . left . right = newNode ( 1 ) NEW_LINE root . right . left = newNode ( 1 ) NEW_LINE root . right . right = newNode ( 1 ) NEW_LINE print ( " Inorder ▁ traversal ▁ before ▁ conversion " , end = " ▁ " ) NEW_LINE printInorder ( root ) NEW_LINE convertTree ( root ) NEW_LINE print ( " Inorder traversal after conversion " , ▁ end ▁ = ▁ "   " ) NEW_LINE printInorder ( root ) NEW_LINE DEDENT