codeparrot/xlcost-text-to-code huggingface dataset:
Find if an expression has duplicate parenthesis or not | Function to find duplicate parenthesis in a balanced expression ; create a stack of characters ; Iterate through the given expression ; if current character is close parenthesis ' ) ' ; pop character from the stack ; stores the number of characters between a closing and opening parenthesis if this count is less than or equal to 1 then the brackets are redundant else not ; push open parenthesis ' ( ' , operators and operands to stack ; No duplicates found ; Driver Code ; input balanced expression
def findDuplicateparenthesis ( string ) : NEW_LINE INDENT Stack = [ ] NEW_LINE for ch in string : NEW_LINE INDENT if ch == ' ) ' : NEW_LINE INDENT top = Stack . pop ( ) NEW_LINE elementsInside = 0 NEW_LINE while top != ' ( ' : NEW_LINE INDENT elementsInside += 1 NEW_LINE top = Stack . pop ( ) NEW_LINE DEDENT if elementsInside < 1 : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT Stack . append ( ch ) NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " ( ( ( a + ( b ) ) + ( c + d ) ) ) " NEW_LINE if findDuplicateparenthesis ( string ) == True : NEW_LINE INDENT print ( " Duplicate ▁ Found " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No ▁ Duplicates ▁ Found " ) NEW_LINE DEDENT DEDENT
Find next Smaller of next Greater in an array | function find Next greater element ; create empty stack ; Traverse all array elements in reverse order order == ' G ' we compute next greater elements of every element order == ' S ' we compute right smaller element of every element ; Keep removing top element from S while the top element is smaller then or equal to arr [ i ] ( if Key is G ) element is greater then or equal to arr [ i ] ( if order is S ) ; store the next greater element of current element ; If all elements in S were smaller than arr [ i ] ; Push this element ; Function to find Right smaller element of next greater element ; stores indexes of next greater elements ; stores indexes of right smaller elements ; Find next greater element Here G indicate next greater element ; Find right smaller element using same function nextGreater ( ) Here S indicate right smaller elements ; If NG [ i ] = = - 1 then there is no smaller element on right side . We can find Right smaller of next greater by arr [ RS [ NG [ i ] ] ] ; Driver program
def nextGreater ( arr , n , next , order ) : NEW_LINE INDENT S = [ ] NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT while ( S != [ ] and ( arr [ S [ len ( S ) - 1 ] ] <= arr [ i ] if ( order == ' G ' ) else arr [ S [ len ( S ) - 1 ] ] >= arr [ i ] ) ) : NEW_LINE INDENT S . pop ( ) NEW_LINE DEDENT if ( S != [ ] ) : NEW_LINE INDENT next [ i ] = S [ len ( S ) - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT next [ i ] = - 1 NEW_LINE DEDENT S . append ( i ) NEW_LINE DEDENT DEDENT def nextSmallerOfNextGreater ( arr , n ) : NEW_LINE INDENT NG = [ None ] * n NEW_LINE RS = [ None ] * n NEW_LINE nextGreater ( arr , n , NG , ' G ' ) NEW_LINE nextGreater ( arr , n , RS , ' S ' ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( NG [ i ] != - 1 and RS [ NG [ i ] ] != - 1 ) : NEW_LINE INDENT print ( arr [ RS [ NG [ i ] ] ] , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " - 1" , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 1 , 9 , 2 , 5 , 1 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE nextSmallerOfNextGreater ( arr , n ) NEW_LINE DEDENT
Convert Ternary Expression to a Binary Tree | Class to define a node structure of the tree ; Function to convert ternary expression to a Binary tree It returns the root node of the tree ; Base case ; Create a new node object for the expression at ith index ; Move ahead in str ; if current character of ternary expression is ' ? ' then we add next character as a left child of current node ; else we have to add it as a right child of current node expression [ 0 ] = = ':   ; Function to print the tree in a pre - order traversal pattern ; Driver Code
class Node : 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 convert_expression ( expression , i ) : NEW_LINE INDENT if i >= len ( expression ) : NEW_LINE INDENT return None NEW_LINE DEDENT root = Node ( expression [ i ] ) NEW_LINE i += 1 NEW_LINE if ( i < len ( expression ) and expression [ i ] == " ? " ) : NEW_LINE INDENT root . left = convert_expression ( expression , i + 1 ) NEW_LINE DEDENT elif i < len ( expression ) : NEW_LINE INDENT root . right = convert_expression ( expression , i + 1 ) NEW_LINE DEDENT return root NEW_LINE DEDENT def print_tree ( root ) : NEW_LINE INDENT if not root : NEW_LINE INDENT return NEW_LINE DEDENT print ( root . data , end = ' ▁ ' ) NEW_LINE print_tree ( root . left ) NEW_LINE print_tree ( root . right ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string_expression = " a ? b ? c : d : e " NEW_LINE root_node = convert_expression ( string_expression , 0 ) NEW_LINE print_tree ( root_node ) NEW_LINE DEDENT
Count natural numbers whose all permutation are greater than that number | Return the count of the number having all permutation greater than or equal to the number . ; Pushing 1 to 9 because all number from 1 to 9 have this property . ; take a number from stack and add a digit smaller than last digit of it . ; Driver Code
def countNumber ( n ) : NEW_LINE INDENT result = 0 NEW_LINE s = [ ] NEW_LINE for i in range ( 1 , 10 ) : NEW_LINE INDENT if ( i <= n ) : NEW_LINE INDENT s . append ( i ) NEW_LINE result += 1 NEW_LINE DEDENT while len ( s ) != 0 : NEW_LINE INDENT tp = s [ - 1 ] NEW_LINE s . pop ( ) NEW_LINE for j in range ( tp % 10 , 10 ) : NEW_LINE INDENT x = tp * 10 + j NEW_LINE if ( x <= n ) : NEW_LINE INDENT s . append ( x ) NEW_LINE result += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT return result NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 15 NEW_LINE print ( countNumber ( n ) ) NEW_LINE DEDENT
Delete consecutive same words in a sequence | Function to find the size of manipulated sequence ; Start traversing the sequence ; Compare the current string with next one Erase both if equal ; Erase function delete the element and also shifts other element that 's why   i is not updated  ; Update i , as to check from previous element again ; Reduce sequence size ; Increment i , if not equal ; Return modified size ; Driver Code
def removeConsecutiveSame ( v ) : NEW_LINE INDENT n = len ( v ) NEW_LINE i = 0 NEW_LINE while ( i < n - 1 ) : NEW_LINE INDENT if ( ( i + 1 ) < len ( v ) ) and ( v [ i ] == v [ i + 1 ] ) : NEW_LINE INDENT v = v [ : i ] NEW_LINE v = v [ : i ] NEW_LINE if ( i > 0 ) : NEW_LINE INDENT i -= 1 NEW_LINE DEDENT n = n - 2 NEW_LINE DEDENT else : NEW_LINE INDENT i += 1 NEW_LINE DEDENT DEDENT return len ( v [ : i - 1 ] ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT v = [ " tom " , " jerry " , " jerry " , " tom " ] NEW_LINE print ( removeConsecutiveSame ( v ) ) NEW_LINE DEDENT
Delete consecutive same words in a sequence | Function to find the size of manipulated sequence ; Start traversing the sequence ; Push the current string if the stack is empty ; compare the current string with stack top if equal , pop the top ; Otherwise push the current string ; Return stack size ; Driver code
def removeConsecutiveSame ( v ) : NEW_LINE INDENT st = [ ] NEW_LINE for i in range ( len ( v ) ) : NEW_LINE INDENT if ( len ( st ) == 0 ) : NEW_LINE INDENT st . append ( v [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT Str = st [ - 1 ] NEW_LINE if ( Str == v [ i ] ) : NEW_LINE INDENT st . pop ( ) NEW_LINE DEDENT else : NEW_LINE INDENT st . append ( v [ i ] ) NEW_LINE DEDENT DEDENT DEDENT return len ( st ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT V = [ " ab " , " aa " , " aa " , " bcd " , " ab " ] NEW_LINE print ( removeConsecutiveSame ( V ) ) NEW_LINE DEDENT
Decode a string recursively encoded as count followed by substring | Returns decoded string for ' str ' ; Traversing the string ; If number , convert it into number and push it into integerstack . ; If closing bracket ' ] ' , pop elemment until ' [ ' opening bracket is not found in the character stack . ; Repeating the popped string ' temo ' count number of times . ; Push it in the character stack . ; If ' [ ' opening bracket , push it into character stack . ; Pop all the elmenet , make a string and return . ; Driven code
def decode ( Str ) : NEW_LINE INDENT integerstack = [ ] NEW_LINE stringstack = [ ] NEW_LINE temp = " " NEW_LINE result = " " NEW_LINE i = 0 NEW_LINE while i < len ( Str ) : NEW_LINE INDENT count = 0 NEW_LINE if ( Str [ i ] >= '0' and Str [ i ] <= '9' ) : NEW_LINE INDENT while ( Str [ i ] >= '0' and Str [ i ] <= '9' ) : NEW_LINE INDENT count = count * 10 + ord ( Str [ i ] ) - ord ( '0' ) NEW_LINE i += 1 NEW_LINE DEDENT i -= 1 NEW_LINE integerstack . append ( count ) NEW_LINE DEDENT elif ( Str [ i ] == ' ] ' ) : NEW_LINE INDENT temp = " " NEW_LINE count = 0 NEW_LINE if ( len ( integerstack ) != 0 ) : NEW_LINE INDENT count = integerstack [ - 1 ] NEW_LINE integerstack . pop ( ) NEW_LINE DEDENT while ( len ( stringstack ) != 0 and stringstack [ - 1 ] != ' [ ' ) : NEW_LINE INDENT temp = stringstack [ - 1 ] + temp NEW_LINE stringstack . pop ( ) NEW_LINE DEDENT if ( len ( stringstack ) != 0 and stringstack [ - 1 ] == ' [ ' ) : NEW_LINE INDENT stringstack . pop ( ) NEW_LINE DEDENT for j in range ( count ) : NEW_LINE INDENT result = result + temp NEW_LINE DEDENT for j in range ( len ( result ) ) : NEW_LINE INDENT stringstack . append ( result [ j ] ) NEW_LINE DEDENT result = " " NEW_LINE DEDENT elif ( Str [ i ] == ' [ ' ) : NEW_LINE INDENT if ( Str [ i - 1 ] >= '0' and Str [ i - 1 ] <= '9' ) : NEW_LINE INDENT stringstack . append ( Str [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT stringstack . append ( Str [ i ] ) NEW_LINE integerstack . append ( 1 ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT stringstack . append ( Str [ i ] ) NEW_LINE DEDENT i += 1 NEW_LINE DEDENT while len ( stringstack ) != 0 : NEW_LINE INDENT result = stringstack [ - 1 ] + result NEW_LINE stringstack . pop ( ) NEW_LINE DEDENT return result NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT Str = "3 [ b2 [ ca ] ] " NEW_LINE print ( decode ( Str ) ) NEW_LINE DEDENT
Iterative method to find ancestors of a given binary tree | A class to create a new tree node ; Iterative Function to print all ancestors of a given key ; Create a stack to hold ancestors ; Traverse the complete tree in postorder way till we find the key ; Traverse the left side . While traversing , push the nodes into the stack so that their right subtrees can be traversed later ; push current node ; move to next node ; If the node whose ancestors are to be printed is found , then break the while loop . ; Check if right sub - tree exists for the node at top If not then pop that node because we don 't need   this node any more.  ; If the popped node is right child of top , then remove the top as well . Left child of the top must have processed before . ; if stack is not empty then simply set the root as right child of top and start traversing right sub - tree . ; If stack is not empty , print contents of stack Here assumption is that the key is there in tree ; Driver code ; Let us construct a binary tree
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 printAncestors ( root , key ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT st = [ ] NEW_LINE while ( 1 ) : NEW_LINE INDENT while ( root and root . data != key ) : NEW_LINE INDENT st . append ( root ) NEW_LINE root = root . left NEW_LINE DEDENT if ( root and root . data == key ) : NEW_LINE INDENT break NEW_LINE DEDENT if ( st [ - 1 ] . right == None ) : NEW_LINE INDENT root = st [ - 1 ] NEW_LINE st . pop ( ) NEW_LINE while ( len ( st ) != 0 and st [ - 1 ] . right == root ) : NEW_LINE INDENT root = st [ - 1 ] NEW_LINE st . pop ( ) NEW_LINE DEDENT DEDENT root = None if len ( st ) == 0 else st [ - 1 ] . right NEW_LINE DEDENT while ( len ( st ) != 0 ) : NEW_LINE INDENT print ( st [ - 1 ] . data , end = " ▁ " ) NEW_LINE st . pop ( ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 1 ) NEW_LINE root . left = newNode ( 2 ) NEW_LINE root . right = newNode ( 7 ) NEW_LINE root . left . left = newNode ( 3 ) NEW_LINE root . left . right = newNode ( 5 ) NEW_LINE root . right . left = newNode ( 8 ) NEW_LINE root . right . right = newNode ( 9 ) NEW_LINE root . left . left . left = newNode ( 4 ) NEW_LINE root . left . right . right = newNode ( 6 ) NEW_LINE root . right . right . left = newNode ( 10 ) NEW_LINE key = 6 NEW_LINE printAncestors ( root , key ) NEW_LINE DEDENT
Tracking current Maximum Element in a Stack | Python3 program to keep track of maximum element in a stack ; main stack ; tack to keep track of max element ; If current element is greater than the top element of track stack , append the current element to track stack otherwise append the element at top of track stack again into it . ; Driver Code
class StackWithMax : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . mainStack = [ ] NEW_LINE self . trackStack = [ ] NEW_LINE DEDENT def push ( self , x ) : NEW_LINE INDENT self . mainStack . append ( x ) NEW_LINE if ( len ( self . mainStack ) == 1 ) : NEW_LINE INDENT self . trackStack . append ( x ) NEW_LINE return NEW_LINE DEDENT if ( x > self . trackStack [ - 1 ] ) : NEW_LINE INDENT self . trackStack . append ( x ) NEW_LINE DEDENT else : NEW_LINE INDENT self . trackStack . append ( self . trackStack [ - 1 ] ) NEW_LINE DEDENT DEDENT def getMax ( self ) : NEW_LINE INDENT return self . trackStack [ - 1 ] NEW_LINE DEDENT def pop ( self ) : NEW_LINE INDENT self . mainStack . pop ( ) NEW_LINE self . trackStack . pop ( ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = StackWithMax ( ) NEW_LINE s . push ( 20 ) NEW_LINE print ( s . getMax ( ) ) NEW_LINE s . push ( 10 ) NEW_LINE print ( s . getMax ( ) ) NEW_LINE s . push ( 50 ) NEW_LINE print ( s . getMax ( ) ) NEW_LINE DEDENT
Reverse a number using stack | Stack to maintain order of digits ; Function to push digits into stack ; Function to reverse the number ; Function call to push number 's   digits to stack ; Popping the digits and forming the reversed number ; Return the reversed number formed ; Driver Code ; Function call to reverse number
st = [ ] ; NEW_LINE def push_digits ( number ) : NEW_LINE INDENT while ( number != 0 ) : NEW_LINE INDENT st . append ( number % 10 ) ; NEW_LINE number = int ( number / 10 ) ; NEW_LINE DEDENT DEDENT def reverse_number ( number ) : NEW_LINE INDENT push_digits ( number ) ; NEW_LINE reverse = 0 ; NEW_LINE i = 1 ; NEW_LINE while ( len ( st ) > 0 ) : NEW_LINE INDENT reverse = reverse + ( st [ len ( st ) - 1 ] * i ) ; NEW_LINE st . pop ( ) ; NEW_LINE i = i * 10 ; NEW_LINE DEDENT return reverse ; NEW_LINE DEDENT number = 39997 ; NEW_LINE print ( reverse_number ( number ) ) ; NEW_LINE
Flip Binary Tree | A binary tree node ; method to flip the binary tree ; Recursively call the same method ; Rearranging main root Node after returning from recursive call ; Iterative method to do the level order traversal line by line ; Base Case ; Create an empty queue for level order traversal ; Enqueue root and initialize height ; nodeCount ( queue size ) indicates number of nodes at current level ; Dequeue all nodes of current level and Enqueue all nodes of next level ; Driver code
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . right = None NEW_LINE self . left = None NEW_LINE DEDENT DEDENT def flipBinaryTree ( root ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return root NEW_LINE DEDENT if ( root . left is None and root . right is None ) : NEW_LINE INDENT return root NEW_LINE DEDENT flippedRoot = flipBinaryTree ( root . left ) NEW_LINE root . left . left = root . right NEW_LINE root . left . right = root NEW_LINE root . left = root . right = None NEW_LINE return flippedRoot NEW_LINE DEDENT def printLevelOrder ( root ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return NEW_LINE DEDENT from Queue import Queue NEW_LINE q = Queue ( ) NEW_LINE q . put ( root ) NEW_LINE while ( True ) : NEW_LINE INDENT nodeCount = q . qsize ( ) NEW_LINE if nodeCount == 0 : NEW_LINE INDENT break NEW_LINE DEDENT while nodeCount > 0 : NEW_LINE INDENT node = q . get ( ) NEW_LINE print node . data , NEW_LINE if node . left is not None : NEW_LINE INDENT q . put ( node . left ) NEW_LINE DEDENT if node . right is not None : NEW_LINE INDENT q . put ( node . right ) NEW_LINE DEDENT nodeCount -= 1 NEW_LINE DEDENT print NEW_LINE DEDENT DEDENT root = Node ( 1 ) NEW_LINE root . left = Node ( 2 ) NEW_LINE root . right = Node ( 3 ) NEW_LINE root . right . left = Node ( 4 ) NEW_LINE root . right . right = Node ( 5 ) NEW_LINE print " Level ▁ order ▁ traversal ▁ of ▁ given ▁ tree " NEW_LINE printLevelOrder ( root ) NEW_LINE root = flipBinaryTree ( root ) NEW_LINE print   " NEW_LINE Level order traversal of the flipped tree " NEW_LINE printLevelOrder ( root ) NEW_LINE
Check if stack elements are pairwise consecutive | Function to check if elements are pairwise consecutive in stack ; Transfer elements of s to aux . ; Traverse aux and see if elements are pairwise consecutive or not . We also need to make sure that original content is retained . ; Fetch current top two elements of aux and check if they are consecutive . ; append the elements to original stack . ; Driver Code
def pairWiseConsecutive ( s ) : NEW_LINE INDENT aux = [ ] NEW_LINE while ( len ( s ) != 0 ) : NEW_LINE INDENT aux . append ( s [ - 1 ] ) NEW_LINE s . pop ( ) NEW_LINE DEDENT result = True NEW_LINE while ( len ( aux ) > 1 ) : NEW_LINE INDENT x = aux [ - 1 ] NEW_LINE aux . pop ( ) NEW_LINE y = aux [ - 1 ] NEW_LINE aux . pop ( ) NEW_LINE if ( abs ( x - y ) != 1 ) : NEW_LINE INDENT result = False NEW_LINE DEDENT s . append ( x ) NEW_LINE s . append ( y ) NEW_LINE DEDENT if ( len ( aux ) == 1 ) : NEW_LINE INDENT s . append ( aux [ - 1 ] ) NEW_LINE DEDENT return result NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = [ ] NEW_LINE s . append ( 4 ) NEW_LINE s . append ( 5 ) NEW_LINE s . append ( - 2 ) NEW_LINE s . append ( - 3 ) NEW_LINE s . append ( 11 ) NEW_LINE s . append ( 10 ) NEW_LINE s . append ( 5 ) NEW_LINE s . append ( 6 ) NEW_LINE s . append ( 20 ) NEW_LINE if ( pairWiseConsecutive ( s ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT print ( " Stack ▁ content ▁ ( from ▁ top ) " , " after ▁ function ▁ call " ) NEW_LINE while ( len ( s ) != 0 ) : NEW_LINE INDENT print ( s [ - 1 ] , end = " ▁ " ) NEW_LINE s . pop ( ) NEW_LINE DEDENT DEDENT
Remove brackets from an algebraic string containing + and | Function to simplify the String ; resultant String of max Length equal to Length of input String ; create empty stack ; If top is 1 , flip the operator ; If top is 0 , append the same operator ; x is opposite to the top of stack ; append value equal to top of the stack ; If closing parentheses pop the stack once ; copy the character to the result ; Driver Code
def simplify ( Str ) : NEW_LINE INDENT Len = len ( Str ) NEW_LINE res = [ None ] * Len NEW_LINE index = 0 NEW_LINE i = 0 NEW_LINE s = [ ] NEW_LINE s . append ( 0 ) NEW_LINE while ( i < Len ) : NEW_LINE INDENT if ( Str [ i ] == ' + ' ) : NEW_LINE INDENT if ( s [ - 1 ] == 1 ) : NEW_LINE INDENT res [ index ] = ' - ' NEW_LINE index += 1 NEW_LINE DEDENT if ( s [ - 1 ] == 0 ) : NEW_LINE INDENT res [ index ] = ' + ' NEW_LINE index += 1 NEW_LINE DEDENT DEDENT elif ( Str [ i ] == ' - ' ) : NEW_LINE INDENT if ( s [ - 1 ] == 1 ) : NEW_LINE INDENT res [ index ] = ' + ' NEW_LINE index += 1 NEW_LINE DEDENT elif ( s [ - 1 ] == 0 ) : NEW_LINE INDENT res [ index ] = ' - ' NEW_LINE index += 1 NEW_LINE DEDENT DEDENT elif ( Str [ i ] == ' ( ' and i > 0 ) : NEW_LINE INDENT if ( Str [ i - 1 ] == ' - ' ) : NEW_LINE INDENT x = 0 if ( s [ - 1 ] == 1 ) else 1 NEW_LINE s . append ( x ) NEW_LINE DEDENT elif ( Str [ i - 1 ] == ' + ' ) : NEW_LINE INDENT s . append ( s [ - 1 ] ) NEW_LINE DEDENT DEDENT elif ( Str [ i ] == ' ) ' ) : NEW_LINE INDENT s . pop ( ) NEW_LINE DEDENT else : NEW_LINE INDENT res [ index ] = Str [ i ] NEW_LINE index += 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s1 = " a - ( b + c ) " NEW_LINE s2 = " a - ( b - c - ( d + e ) ) - f " NEW_LINE r1 = simplify ( s1 ) NEW_LINE for i in r1 : NEW_LINE INDENT if i != None : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT print ( ) NEW_LINE r2 = simplify ( s2 ) NEW_LINE for i in r2 : NEW_LINE INDENT if i != None : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT
Growable array based stack | constant amount at which stack is increased ; top of the stack ; length of stack ; function to create new stack ; allocate memory for new stack ; copying the content of old stack ; re - sizing the length ; function to push new element ; if stack is full , create new one ; insert element at top of the stack ; function to pop an element ; function to display ; if top is - 1 , that means stack is empty ; Driver Code ; creating initial stack ; pushing element to top of stack ; pushing more element when stack is full ; pushing more element so that stack can grow
BOUND = 4 NEW_LINE top = - 1 ; NEW_LINE a = [ ] NEW_LINE length = 0 ; NEW_LINE def create_new ( ) : NEW_LINE INDENT global length ; NEW_LINE new_a = [ 0 for i in range ( length + BOUND ) ] ; NEW_LINE for i in range ( length ) : NEW_LINE INDENT new_a [ i ] = a [ i ] ; NEW_LINE DEDENT length += BOUND ; NEW_LINE return new_a NEW_LINE DEDENT def push ( element ) : NEW_LINE INDENT global top , a NEW_LINE if ( top == length - 1 ) : NEW_LINE INDENT a = create_new ( ) ; NEW_LINE DEDENT top += 1 NEW_LINE a [ top ] = element ; NEW_LINE return a ; NEW_LINE DEDENT def pop ( ) : NEW_LINE INDENT global top NEW_LINE top -= 1 ; NEW_LINE DEDENT def display ( ) : NEW_LINE INDENT global top NEW_LINE if ( top == - 1 ) : NEW_LINE INDENT print ( " Stack ▁ is ▁ Empty " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Stack : ▁ " , end = ' ' ) NEW_LINE for i in range ( top + 1 ) : NEW_LINE INDENT print ( a [ i ] , end = ' ▁ ' ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = create_new ( ) ; NEW_LINE push ( 1 ) ; NEW_LINE push ( 2 ) ; NEW_LINE push ( 3 ) ; NEW_LINE push ( 4 ) ; NEW_LINE display ( ) ; NEW_LINE push ( 5 ) ; NEW_LINE push ( 6 ) ; NEW_LINE display ( ) ; NEW_LINE push ( 7 ) ; NEW_LINE push ( 8 ) ; NEW_LINE display ( ) ; NEW_LINE push ( 9 ) ; NEW_LINE display ( ) ; NEW_LINE DEDENT