def test_stack_push_pop(): el = 1 stack = Stack(5) stack.push(el) assert stack.size() == 1 assert stack.pop() == el assert stack.size() == 0
class Queue: def __init__(self): self.s1 = Stack() self.s2 = Stack() def enqueue(self, val): while self.s1.size() > 0: self.s2.push(self.s1.pop()) self.s2.push(val) while self.s2.size() > 0: self.s1.push(self.s2.pop()) def dequeue(self): return self.s1.pop()
def stack_reverse_string(input): stk = Stack() for elem in input: stk.push(elem) res = "" while stk.size() > 0: elem = stk.pop() res += elem return res
def matching_parens(input): stk = Stack() for elem in input: if elem in paren_map.keys(): stk.push(elem) else: if stk.size() == 0: return False match = stk.pop() if not paren_map[match] == elem: return False return True
def reverseString(mystr): """ Reverses the input string using Stack data structure INPUT ------- mystr : apple RETURN ------- elppa """ revStr = '' temp = Stack() for token in mystr: temp.push(token) for i in range(temp.size()): revStr += temp.pop() return revStr
class TestStack(TestCase): def setUp(self): super().setUp() self.stack = Stack() def test_pop(self): self.stack.push(1) self.stack.push(2) self.assertEqual(self.stack.pop(), 2) self.assertEqual(self.stack.pop(), 1) def test_pop_empty(self): self.assertIsNone(self.stack.pop()) def test_peek(self): self.stack.push(1) self.stack.push(2) self.assertEqual(self.stack.peek(), 2) self.stack.pop() self.assertEqual(self.stack.peek(), 1) def test_size(self): self.stack.push(1) self.stack.push(2) self.assertEqual(self.stack.size(), 2) def test_is_empty(self): self.assertTrue(self.stack.is_empty()) self.stack.push(1) self.assertFalse(self.stack.is_empty()) self.stack.pop()