def test_push_pop_1(self): test_object_stack = stack() test_object_stack.push(1) test_object_stack.push(2) test_object_stack.push(3) test_object_stack.push(4) test_object = [] while not test_object_stack.is_empty(): test_object.append(test_object_stack.pop()) self.assertEqual([4, 3, 2, 1], test_object)
def div_by_2(dec_num): s = stack() # 's' instance is created for class 'stack()' while dec_num>0: r = dec_num % 2 s.push(r) dec_num = dec_num//2 bin_num = "" while not s.is_empty(): bin_num += str(s.pop()) return bin_num
def div(dec): s = stack() while dec > 0: remainder = dec % 2 s.push(remainder) dec = dec // 2 binary = "" while not s.is_empty(): binary += str(s.pop()) return binary
def isBalanced(symbols): list = stack() balanced = True for symbol in symbols: if symbol in "{[(<": list.push(symbol) elif symbol in ")]}>": if list.isEmpty(): balaned = False break else: opening = list.peek() closing = symbol if match(opening, closing): list.pop() else: balanced = False break if not list.isEmpty(): balanced = False return balanced
def __init__(self): super().__init__() self._main = QtWidgets.QWidget() self.setCentralWidget(self._main) layout = QtWidgets.QVBoxLayout(self._main) self.setting = Settings() self.stack = stack() #spec_canvas = FigureCanvas(Figure(figsize=None)) #layout.addWidget(dynamic_canvas) dynamic_canvas = FigureCanvas(Figure(figsize=(8, 5))) layout.addWidget(dynamic_canvas) self.addToolBar(QtCore.Qt.BottomToolBarArea, NavigationToolbar(dynamic_canvas, self)) self._dynamic_ax = dynamic_canvas.figure.subplots() self._timer = dynamic_canvas.new_timer(100, [(self._update_canvas, (), {})]) self._timer.start()
def balanced(strin): s = stack() is_balanced = True index = 0 while index < len(strin) and is_balanced: paren = strin[index] if paren in "([{": s.push(paren) else: if s.is_empty(): is_balanced = False else: top = s.pop() if not match(top, paren): is_balanced = False index += 1 if s.is_empty() and is_balanced: return True else: return False
def is_paren_balanced(paren_string): s = stack() is_balanced = True ## initial variables la default nav dile index = 0 # initial variables la default nav dile while index < len(paren_string) and is_balanced: paren = paren_string[index] if paren in "{[(": s.push(paren) else: if s.is_empty(): is_balanced = False else: top = s.pop() if not is_match(top, paren): is_balanced = False index += 1 if s.is_empty() and not is_balanced: return False else: return True
from Stack import stack nums = stack() operators = stack() userinput = input("계산식 입력 : ") a = userinput.split(' ') check = 0 for i in a: print(i, end='') if (check % 2) == 0: nums.push(i) elif (check % 2) == 1: if (not operators.isEmpty()): if (i == '*') or (i == '/'): operators.push(i) else: while 1: n2 = nums.pop() n1 = nums.pop() o = operators.pop() if (o == '*'): result = int(n1) * int(n2) elif (o == '/'): result = int(n1) / int(n2) elif (o == '+'): result = int(n1) + int(n2) elif (o == '-'): result = int(n1) - int(n2) nums.push(result)
def __init__(self, capacity): self.__capacity = capacity self.__Stack1 = stack(capacity) self.__Stack2 = stack(capacity) self.__activeStack = self.__Stack1 self.__passiveStack = self.__Stack2
def push(self, val): if (len(self.__stacks) == 0) or (len(self.__stacks[-1]) == self.__capacity): self.__stacks.append(stack(self.__capacity)) self.__stacks[-1].push(val)
def __init__(self, capacity): self.__stacks = [stack(capacity)] self.__capacity = capacity
"""Write a program to sort stack""" from Stack import stack from random import randint main = stack(20) temp = stack(20) for i in range(15): main.push(randint(1,20)) main.display() def sortStackHelper(stk, temp): while(not stk.isEmpty()): min = stk.pop() while sortStackHelper(main, temp)
def test_stack(self): s = stack() for i in range(225): s.push(i)
# -*- coding: utf-8 -*- """ Created on Tue Jan 05 12:29:05 2016 @author: snaganan """ from Stack import stack from queue import queue s = stack() s.push(5) s.push(6) s.push(7) s.push(4) s.push(3) s.push(2) s.push(1) s.push(0) s.push(-1) print "stack output" print s.pop() print s.pop() print s.stackData print "end of stack output" #print s.pop() #print s.stackData q = queue()
def __init__(self,player_name): self.player = player_name self.stack = stack() self.cost = 20
def test_isEmpty_Fail(self): test_object_stack = stack() test_object_stack.push(1) test_object = test_object_stack.is_empty() self.assertEqual(False, test_object)
def test_isEmpty_Success(self): test_object_stack = stack() test_object = test_object_stack.is_empty() self.assertEqual(True, test_object)
# Problem Statement : Reverse the given string (i.e. word) by using stack Data Structure # Example : If 'Hello' is the given word , its reverse string form will be 'olleH' from Stack import stack def reverse_string(stack, input_str): # Loop through strings and push all characters in 'items' list created in Stack class # Then add character by character into the another string until items list get empty for i in range(len(input_str)): stack.push(input_str[i]) rev_str = [] while not stack.is_empty(): rev_str += stack.pop() return rev_str stack = stack() input_str = "Kedar" print(reverse_string(stack, input_str)) # NOTE : we can do it directly by using string indexing or slicing , But in given problem statement we must use stack data struture type. #input_str = "Hello" #print(input_str[::-1])