def test_peep_from_non_empty_stack_(self): stack = Stack() stack.push(5) self.assertEqual(stack.peek(), 5) stack.push(10) self.assertEqual(stack.peek(), 10) stack.push(19) self.assertEqual(stack.peek(), 19) self.assertEqual(stack.items.walk(), [5, 10, 19])
class TestStack(unittest.TestCase): def setUp(self): self.data = ["d1", "d2"] self.s1 = Stack() self.s2 = Stack(StackNode(self.data[0])) self.s3 = Stack() for datum in self.data: self.s3.push(datum) def test_Stack__init__(self): self.assertEqual(self.s1.current, None) self.assertEqual(self.s1.len, 0) self.assertEqual(repr(self.s2.current), self.data[0]) self.assertEqual(self.s2.len, 1) def test_Stack__iter__(self): for n in self.s1: self.assertTrue(False) for s in [self.s2]: #, self.s3]: for n in s: self.assertTrue(repr(n) in self.data) def test_Stack_push(self): len1 = len(self.s1) self.s1.push("foo") self.assertTrue(len(self.s1) - len1 == 1) self.assertTrue(self.s1.peek() == "foo") len2 = len(self.s2) self.s2.push("bar") self.assertTrue(len(self.s2) - len2 == 1) self.assertTrue(self.s2.peek() == "bar") def test_Stack_pop(self): self.assertTrue(self.s3.pop() == self.data[1]) self.assertTrue(self.s3.pop() == self.data[0]) def test_Stack_peek(self): self.assertTrue(self.s3.peek() == self.data[1]) self.assertTrue(self.s3.peek() == self.data[1]) def test_Stack_isEmpty(self): self.assertTrue(self.s1.isEmpty()) self.assertFalse(self.s2.isEmpty()) def test_Stack__len__(self): self.assertEquals(len(self.s1), 0) self.assertEquals(len(self.s2), 1) self.assertEquals(len(self.s3), 2) def test_Stack__repr__(self): self.assertEquals(repr(self.s1), '') self.assertEquals(repr(self.s2), 'd1') self.assertEquals(repr(self.s3), 'd2 <- d1')
class StackTests(unittest.TestCase): def setUp(self): self.stack = Stack() def tearDown(self): self.stack = None def test_push(self): self.stack.push(10) self.assertEqual(self.stack.peek(), 10) self.stack.push(100) self.assertEqual(self.stack.peek(), 100) def test_pop(self): self.stack.push(1) self.stack.push(2) self.stack.push(3) self.stack.push(4) self.assertEquals(self.stack.pop(), 4) self.assertEquals(self.stack.pop(), 3) self.assertEquals(self.stack.pop(), 2) self.assertEquals(self.stack.pop(), 1) with self.assertRaises(IndexError): self.stack.pop() def test_peek(self): self.stack.push(1) self.stack.push(2) self.stack.push(3) self.stack.push(4) self.assertEquals(self.stack.peek(), 4) self.stack.pop() self.assertEquals(self.stack.peek(), 3) self.stack.pop() self.assertEquals(self.stack.peek(), 2) self.stack.pop() self.assertEquals(self.stack.peek(), 1) self.stack.pop() with self.assertRaises(IndexError): self.stack.peek() def test_reverse(self): self.stack.push(1) self.stack.push(2) self.stack.push(3) self.stack.push(4) self.stack.push(5) self.stack.reverse() self.assertEqual(self.stack.pop(), 1) self.assertEqual(self.stack.pop(), 2) self.assertEqual(self.stack.pop(), 3) self.assertEqual(self.stack.pop(), 4) self.assertEqual(self.stack.pop(), 5)
def dfs_stack(self): result = [] stack = Stack() stack.push(self) current_node = stack.peek() index = 0 while current_node and index < 8: for node in reversed(current_node.nodes): stack.push(node) current_node = stack.peek() if len(current_node.nodes) == 0: result.append(current_node.value) stack.pop() current_node = stack.peek() print(result) index += 1
def dfs_in_order_stack(self): result = [] stack = Stack() stack.push(self) while stack.peek(): current_node = stack.pop() result.append(current_node.value) for node in current_node.nodes: stack.push(node) return result
def test_stack(self): elements = [5, 1, 6, 9, 72, 42, 51] expected_result = elements.copy() expected_result.reverse() stack = Stack() for element in elements: stack.push(element) actual_result = [] while stack.peek(): actual_result.append(stack.pop()) self.assertEqual(expected_result, actual_result)
def is_balanced_symbols(string: str): stack = Stack() for char in string: if char in "([{": stack.push(char) elif char in "}])": if stack.isempty(): return False elif match(stack.peek(), char): stack.pop() else: return False if stack.isempty(): return True else: return False
class PostfixCalculator(object): def __init__(self, tokens_string=""): self._tokens_string = tokens_string self._stack = Stack() self._result = 0 @property def tokens(self): return self._tokens_string def _tokenize(self): return self._tokens_string.split() def _do_operation(self, operator): # """ This method mimics a switch statement """ rhs = self._stack.pop() # right hand side operand lhs = self._stack.pop() # left hand side operand # switch return { "+": int(lhs) + int(rhs), "-": int(lhs) - int(rhs), "*": int(lhs) * int(rhs), "/": int(lhs) / int(rhs), }.get(operator, "unknown operator {}".format(operator)) def calculate(self): """ Execute postfix algorithm here """ tokens = self._tokenize() for token in tokens: try: self._stack.push(int(token)) except ValueError: self._stack.push(self._do_operation(token)) # Peek at the top of the stack to get the result self._result = self._stack.peek() @property def result(self): return self._result
def infix2postfix(infix): s = Stack() l = [] linfix = infix.split() priority = {"*": 3, "/": 3, "+": 2, "-": 2, "(": 1} for i in linfix: if i == "(": s.push(i) elif i == ")": t = s.pop() while not s.is_empty() and t != "(": l.append(t) t = s.pop() elif i in "+-*/": while not s.is_empty() and priority[s.peek()] >= priority[i]: l.append(s.pop()) s.push(i) else: l.append(i) while not s.is_empty(): l.append(s.pop()) return " ".join(l)
from stack.stack import Stack from stack.stack import foo stk = Stack() stk.push(1) stk.push(2) stk.pop() stk.push(3) print stk.peek() print stk.size() print foo()
def test_peek_leaves_element_on_stack(list_arg_non_empty): st = Stack(list_arg_non_empty) st.peek() assert st.peek() == list_arg_non_empty[-1]
def test_peep_from_empty_stack(self): stack = Stack() self.assertEqual(stack.items.walk(), []) self.assertIsNone(stack.peek())