def reverse_list(data): """Reverse the elements of list using stack""" S = Stack.ArrayStack() for i in range(len(data)): S.push(data[i]) for i in range(len(data)): data[i] = S.pop()
def reverse_file(filename): """Overwrite a a given file with its contents line-by-line reversed.""" S = Stack.ArrayStack() original = open(filename) for line in original: S.push(line.rstrip('\n')) original.close() # Now we overwrite the file output = open(filename, 'w') while not S.is_empty(): output.write(S.pop() + '\n') output.close()
def is_matched(expr): """Returns True if all the delimiters are matched properly;False otherwise.""" S = Stack.ArrayStack() left = '({[' right = ')}]' for c in expr: if c in left: S.push(c) elif c in right: if S.is_empty(): return False if right.index(c) != left.index(S.pop()): return False return S.is_empty()
def is_matched_html(raw): """Return True if all HTML tags are properly matched;False otherwise""" S = Stack.ArrayStack() j = raw.find('<') while j != -1: k = raw.find('>', j + 1) if k == -1: # invalid tag return False tag = raw[j + 1:k] # strip away the tag if not tag.startswith('/'): # opening tag S.push(tag) else: if S.is_empty(): return False if tag[1:] != S.pop(): return False j = raw.find('<', k + 1) return S.is_empty()
def postfix(expr): """Returns the postfix expression of the given infix expr""" precedence = ['(', ')', '^', '/', '*', '+', '-'] postfix_expr = [] stack = Stack.ArrayStack() for char in expr: if char in precedence: if precedence.index(char) == 0: # opening paranthesis stack.push(char) elif precedence.index(char) == 1: # closing paranthesis while not stack.is_empty(): res = stack.pop() if res == '(': break postfix_expr.append(res) else: # other operators if len(stack) > 0 and precedence.index( char) > precedence.index(stack.top()): while len(stack) > 0 and precedence.index( char) > precedence.index(stack.top()): if stack.top() != '(': postfix_expr.append(stack.pop()) else: break stack.push(char) else: stack.push(char) else: postfix_expr.append(char) while not stack.is_empty(): postfix_expr.append(stack.pop()) return ''.join(postfix_expr)
def postfix_evaluate(expr): """Evaluate the given postfix operation""" stack = Stack.ArrayStack() operators = ['*', '+', '-', '/', '^'] for char in expr: if char not in operators: stack.push(int(char)) else: num1 = stack.pop() num2 = stack.pop() if char == '+': stack.push(num1 + num2) elif char == '-': stack.push(num2 - num1) elif char == '*': stack.push(num1 * num2) elif char == '/': stack.push(num2 // num1) return stack.top()
import stackarray as Stack def transfer(S, T): """Transfer the contents of stack of S to stack T""" while not S.is_empty(): T.push(S.pop()) # Testing if __name__ == "__main__": S = Stack.ArrayStack() T = Stack.ArrayStack() U = Stack.ArrayStack() S.push(12) S.push(10) S.push(25) print('S initial : ', S) transfer(S, T) print(T) transfer(T, U) print(U) transfer(U, S) print('S after : ', S)
import stackarray as Stack def empty_stack(S): """Empty the stack""" if S.is_empty(): return else: print('Item popped: ', S.pop()) empty_stack(S) # Testing if __name__ == "__main__": S = Stack.ArrayStack() S.push(12) S.push(10) S.push(25) print(S) empty_stack(S) print("Length of stack S: ", len(S))