def par_checker(in_str: str): """ >>> par_checker('()') True >>> par_checker(')(') False >>> par_checker('()()') True >>> par_checker('({})') True """ s = Stack() balanced = True index = 0 while index < len(in_str) and balanced: symbol = in_str[index] if symbol in OPENS: s.push(symbol) else: if s.isEmpty(): balanced = False else: top = s.pop() if OPENS.index(top) != CLOSERS.index(symbol): balanced = False index += 1 return balanced and s.isEmpty()
def symbol_checker(user_input): ''' Check {[( symbols matching has proper open and end ''' open_symbols = '{[(' close_symbols = '}])' is_valid = True stk = Stack() for symbol in user_input: if symbol in open_symbols: stk.push(symbol) else: if stk.is_empty(): is_valid = False break else: open_symbol = stk.pop() if not open_symbols.index(open_symbol) == close_symbols.index( symbol): is_valid = False break if is_valid: print("Symbols matching...") else: print("Invalid symbol pattern...")
def stack_operations(): '''USER OPERATIONS OF A STACK IMPLEMENTATION''' stack = Stack() global choice while choice > 0: stack_menu = [('MAIN MENU', -1), ('EXIT', 0), \ ('PUSH', 1), ('POP', 2), ('PEEK', 3), ('SIZE', 4)] print_menu(stack_menu) choice = int(input().strip()) if choice == 1: print('ENTER ITEM :', end=' ') stack.push(int(input().strip())) print('PUSH OPERATION SUCCESSFUL.') elif choice == 2: if stack.is_empty(): print('UNDERFLOW') else: print('POPPED VALUE :', stack.pop()) elif choice == 3: if stack.is_empty(): print('UNDERFLOW') else: print('STACK TOP :', stack.peek()) elif choice == 4: print('STACK SIZE :', stack.size())
def test_clear(): myStack = Stack() myStack.push(3) myStack.push(4) myStack.clear() assert myStack.size() == 0 assert myStack.isEmpty() assert myStack.toList() == []
def setUp(self): # Set up some Elements self.e1 = LinkedListElement(1) self.e2 = LinkedListElement(2) self.e3 = LinkedListElement(3) self.e4 = LinkedListElement(4) # Start setting up a LinkedList self.stack = Stack(self.e1) return
def revert(s: str): stack = Stack() for ch in s: stack.push(ch) res = [] while not stack.isEmpty(): res.append(stack.pop()) return ''.join(res)
def dectoBin(decimal): s = Stack() while decimal > 0: rem = decimal % 2 s.push(rem) decimal //= 2 binString = '' while not s.isEmpty(): binString += str(s.pop()) return binString
def decToBase(decimal, base): alphanumeric = "0123456789ABCDEF" remainders = Stack() baseString = '' while decimal > 0: remainder = decimal % base remainders.push(remainder) decimal //= base while not remainders.isEmpty(): baseString += alphanumeric[remainders.pop()] return baseString
def decimal_to_binary(number): remainder_stack = Stack() while (number > 0): rem = number % 2 #print rem number = number // 2 #print number remainder_stack.push(rem) s = '' for k in range(remainder_stack.size()): s += str(remainder_stack.pop()) print s
def convert_number(user_input, base): ''' Convert number into different base format 2 : Binary 8 : Octal 16 : Hex ''' stk = Stack() remainder_list = '0123456789ABCDEF' while user_input > 0: remainder = user_input % base stk.push(remainder_list[remainder]) user_input = user_input // base result = '' while not stk.is_empty(): result += str(stk.pop()) print(result)
def balanceCheck(symbols): s = Stack() balanced = True index = 0 while index < len(symbols) and balanced: symbol = symbols[index] if symbol in "({[": s.push(symbol) else: if s.isEmpty(): balanced = False else: top = s.pop() if not matched(top, symbol): balanced = False index += 1 if balanced and s.isEmpty(): return True else: return False
def insert(to_insert, stack, temp_stack): position = 0 while not stack.stack_empty(): if stack.peek() < to_insert: # Found correct position break temp_stack.push(stack.pop()) position += 1 stack.push(to_insert) for _ in xrange(position): stack.push(temp_stack.pop()) def sort_stack_recursive(stack, temp_stack): if stack.stack_empty(): return temp_stack.push(stack.pop()) sort_stack_recursive(stack, temp_stack) to_insert = temp_stack.pop() insert(to_insert, stack, temp_stack) stack = Stack() temp_stack = Stack() for i in xrange(10, 0, -1): stack.push(i) stack.traverse() sort_stack(stack, temp_stack) stack.traverse()
def test_isEmpty_non_empty_stack(): myStack = Stack() myStack.push(1) assert not myStack.isEmpty()
from datastructures.stack import Stack import time n = 26 transfers = 0 stack_i = Stack() stack_j = Stack() stack_k = Stack() for i in reversed(range(n)): # print(".") stack_i.push(i) start_time = time.time() def transfer(from_stack, to_stack): global transfers transfers += 1 to_stack.push(from_stack.pop()) def do_hanoi(n, stack_i, stack_j, stack_k): if n == 0: return do_hanoi(n - 1, stack_i, stack_k, stack_j) transfer(stack_i, stack_j) do_hanoi(n - 1, stack_k, stack_j, stack_i)
def test_pop(): myStack = Stack() myStack.push(2) assert myStack.pop() == 2
def test_peek(): myStack = Stack() myStack.push(3) myStack.push(4) assert myStack.peek() == 4
def test_size(): myStack = Stack() myStack.push(3) myStack.push(4) assert myStack.size() == 2
def test_isEmpty_empty_stack(): myStack = Stack() assert myStack.isEmpty()
def test_toList(): myStack = Stack() myStack.push(3) myStack.push(4) assert myStack.toList() == [3, 4]
def setUp(self) -> None: self.stack = Stack("plate1")