def test_push2_pop1_size(self): '''Pop an element from the stack. Test that it matches the 2nd pushed value. Check that the size of the stack is 1. ''' stack = StackClass.Stack() stack.push(1) stack.push(2) self.assertEqual(2, stack.pop()) self.assertEqual(1, stack.size())
def decimalToBinary(decNum): binStack = stack.Stack() binNumStr = '' while decNum > 0: binStack.push(decNum % 2) decNum = decNum // 2 while not binStack.isEmpty(): binNumStr += str(binStack.pop()) return binNumStr
def test_pop_stack(self): test_stack = stack.Stack() test_stack.push(5) test_stack.push("hello") test_stack.push(True) self.assertEqual(test_stack.pop(), True) self.assertEqual(test_stack.pop(), "hello") self.assertEqual(test_stack.pop(), 5) self.assertEqual(test_stack.is_empty(), True)
def main(): user_string = input('Enter a string to reverse:') string_stack = stack.Stack() for i in range(len(user_string)): string_stack.push(user_string[i]) reversed_string = '' for i in range(len(user_string)): reversed_string += string_stack.pop() print(reversed_string)
def test_full_no_capacity(): s = stack.Stack() assert s.capacity() == False s.push("a") assert s.capacity() == False s.push("b") assert s.capacity() == False s.push("c") assert s.capacity() == False
def test_full_yes_capacity(): s = stack.Stack(2) assert s.capacity() == False s.push("a") assert s.capacity() == False s.push("b") assert s.capacity() == True s.pop() assert s.capacity() == False
def test_peek(): s = stack.Stack() assert s.peek() == None s.push("hello") assert s.peek() == "hello" s.push("world") assert s.peek() == "world"
def outStack(inputList): tempList = inputList tempStack = stack.Stack() while len(tempList) != 0: tempStack.push(tempList.pop()) return tempStack.stack
def decToBase(num, base, chars): s = stack.Stack() while (num > 0): remainder = num % base s.push(chars[remainder]) num = num // base baseString = '' while (not s.isEmpty()): baseString += s.pop() return baseString
def populated_stack(): """Fixture generates a populated stack.""" populated = st.Stack() populated.push('first') populated.push('second') populated.push('third') populated.push('fourth') populated.push(5) populated.push(6) return populated
def test_push(): """Unit test for stack.push() method """ mystack = stack.Stack() for i in range(16): mystack.push(i) mystack.print_stack() assert mystack.get_size() == 16
def __init__(self): Gtk.Window.__init__(self, title=cn.App.application_name) hbar = hb.Headerbar() self.set_titlebar(hbar) self.stack = sk.Stack(self) hbar.func_parameters = self.stack.new_search.func_parameters hbar.algo_parameters = self.stack.new_search.algo_parameters self.add(self.stack)
def test_isFull(self): try: s = stack.Stack(3) s.push(1) s.push(2) s.push(3) self.assertTrue(s.isFull()) self.fp.write('isFull passed\n') except Exception as e: self.fp.write('isFull failed\n')
def infix_to_rpn(a_string): import stack my_stack = stack.Stack(40) temp_stack = stack.Stack(40) n = 0 while n < len(a_string): temp = "" while n < len(a_string) and a_string[n] == " ": n += 1 while n < len(a_string) and (temp == "" or find_type(a_string[n]) == item_type): temp += a_string[n] n += 1 item_type = find_type(temp) while n < len(a_string) and a_string[n] == " ": n += 1 n -= 1 if item_type == 0: my_stack.push(temp) if item_type == 1: if temp_stack.is_empty(): temp_stack.push(temp) elif (temp_stack.peek() == "+" or temp_stack.peek() == "-") and (temp == "*" or temp == "/"): temp_stack.push(temp) else: while (not temp_stack.is_empty() and (temp_stack.peek() == "*" or temp_stack.peek() == "/")): my_stack.push(temp_stack.pop()) temp_stack.push(temp) if item_type == 2: print("An item has invalid type") n += 1 while not temp_stack.is_empty(): my_stack.push(temp_stack.pop()) assert not my_stack.is_empty( ), "Couldn't evaluate expression. Please enter valid infix expressions (numbers interposed with operators)" my_stack = reverse_stack(my_stack) return my_stack
def test_double_push(): """ :return: """ s = stack.Stack() s.push(1) s.push(2) assert s.pop() == 2 assert s.pop() == 1
def decToBin(num): s = stack.Stack() while (num > 0): remainder = num % 2 s.push(remainder) num = num // 2 binString = '' while (not s.isEmpty()): binString += str(s.pop()) return binString
def string_reverse(str): s = stack.Stack() print s revStr = '' for c in str: s.push(c) while not s.isEmpty(): revStr += s.pop() return revStr
def left_back_iter(B): s = stack.Stack() length = -1 while B != None: s.push(B.key) B = B.left while not (s.isempty()): print(s.pop(), " ", end='') length += 1 print("\nlength = ", length)
def isBalanced(expr): s = stack.Stack() for paren in expr: if (paren == '('): s.push(paren) elif (paren == ')'): if (s.isEmpty()): return False s.pop() return True if s.isEmpty() else False
def preorder_iter(B): s = stack.Stack() if B != None: s.push(B) while not (s.isempty()): B = s.pop() print(B.key, " ", end='') if B.right: s.push(B.right) if B.left: s.push(B.left)
def _validate(self, code): valid = stack.Stack() for char in code: if char == Token.EXP_OPEN: valid.push(char) elif char == Token.EXP_CLOSE: valid.pop() if valid.is_empty(): return True else: return False
def __init__(self): self.symbolTable = [SymbolEntry() for i in range(SCompiler.RAMSIZE)] self.lineFlags = [-1] * SCompiler.RAMSIZE # notes which fail, only because of spec else I'd append to a list or dict self.smlData = [0] * SCompiler.RAMSIZE # floods ram later self.instructionCounter = -1 self.dataCounter = SCompiler.RAMSIZE self.currSym = -1 # index in symbol table of latest self.lastLine = -1 # for checkLineNumIncreasing self.verbose = False self.loops = stack.Stack()
def dfs_traverse(self): lifo = stack.Stack() lifo.push(self.root) while not lifo.isEmpty(): node = lifo.pop() if node != None: print(node.getData()) lifo.push(node.getLeft()) lifo.push(node.getRight())
def test_five_push(): """ :return: """ s = stack.Stack() for i in range(5): s.push(i) for i in range(4): s.pop() assert s.pop() == 0
def delete_last_10(): blog_posts = BlogPost.query.all() st = stack.Stack() for post in blog_posts: st.push(post) for _ in range(10): post_to_delete = st.pop() db.session.delete(post_to_delete.data) db.session.commit() return jsonify({"message": "success"}), 200
def test_empty(self): try: s = stack.Stack(5) for i in ['a', 'b', 'c']: s.push(i) # 测试清空栈操作是否工作正常 s.empty() self.assertTrue(s.isEmpty()) self.fp.write('empty passed\n') except Exception as e: self.fp.write('empty failed\n')
def accepts(self, strm): def matchTop(aStackTop, stack): return stack[-1] == aStackTop def getTransitions(stateId, stack): transitionList = [] for aStateId, anInputSym, aStackTop in self.delta.keys(): if stateId == aStateId and matchTop(aStackTop, stack): transitionList.append((anInputSym, aStackTop)) return transitionList def popPush(aStackTop, pushOnStack, stack): newstack = stack[:] for x in aStackTop: newstack.pop() for x in pushOnStack[::-1]: newstack.append(x) return newstack stateId = self.startStateId pdaStack = [self.stackStartSym] # This is the instantaneous description stack. It starts with the start state # instantaneous description. ID = stack.Stack() ID.push((stateId, strm, pdaStack)) while not ID.isEmpty(): stateId, strm, pdaStack = ID.pop() print((stateId, strm, pdaStack)) c = strm.readChar() if strm.eof() and stateId in self.finalStates: return True strm.unreadChar(c) for anInputSym, aStackTop in getTransitions(stateId, pdaStack): for toStateId, pushOnStack in self.delta[(stateId, anInputSym, aStackTop)]: if anInputSym == epsilon: ID.push((toStateId, copy.deepcopy(strm), popPush(aStackTop, pushOnStack, pdaStack))) else: # not an epsilon transition c = strm.readChar() if c == anInputSym: ID.push((toStateId, copy.deepcopy(strm), popPush(aStackTop, pushOnStack, pdaStack))) strm.unreadChar(c) return False
def main(): my_stack = stack.Stack() test(my_stack.is_empty(), True) my_stack.push(1) test(my_stack.peek(), 1) my_stack.push('Second Item') my_stack.push(True) test(my_stack.size(), 3) test(my_stack.pop(), True) test(my_stack.size(), 2)
def test_pushpop(self): try: s = stack.Stack() s.push(3) # 确保入栈后立刻出栈得到原来的元素 self.assertEqual(s.pop(), 3) s.push('a') self.assertEqual(s.pop(), 'a') self.fp.write('push and pop passed\n') except Exception as e: self.fp.write('push or pop failed\n')
def balancedParents(parents): closedParentStack = stack.Stack() for parent in parents: if parent=='(': closedParentStack.push(')') else: if closedParentStack.isEmpty(): return False else: closedParentStack.pop() return closedParentStack.isEmpty()