def dfs_iterative(self, root): s = Stack() s.push(root) while s.size() > 0: vertex = s.pop() self.vertices.append(vertex.get_vertex()) for neighbor in vertex.get_child(): if neighbor and neighbor.state == State.UNVISITED: s.push(neighbor) vertex.state = State.VISITED
def simplifyPath(self, path): s = Stack() skip = {"..", ".", ""} for dir in path.split("/"): if (dir == ".." and not s.isEmpty()): s.pop() elif dir not in skip: s.push(dir) res = "" siter = iter(s) for elem in siter: res = "/" + elem + res return "/" if not res else res
def __string_bs_caps(self, s): BACKSLASH = False CAPSLOCK = False st = Stack() for char in s: if char == '\\': BACKSLASH = not BACKSLASH else: if BACKSLASH and char == 'b': st.pop() BACKSLASH = not BACKSLASH elif BACKSLASH and char == 'c': CAPSLOCK = not CAPSLOCK BACKSLASH = not BACKSLASH else: st.push(char.upper() if CAPSLOCK else char) if CAPSLOCK: CAPSLOCK = not CAPSLOCK result = '' for i in range(st.size()): result = st.pop() + result return result
def test_sort(self): s1 = Stack() s1.push(1) s1.push(2) s1.push(4) s1.push(9) s1.push(12) s1.push(11) s1.push(3) expected = [1, 2, 3, 4, 9, 11, 12] for i in enumerate(iter(self.fixture.sort(s1))): self.assertEqual(expected[i[0]], i[1]) s1.push(1) s1.push(2) s1.push(4) s1.push(9) s1.push(12) s1.push(11) s1.push(3) self.fixture.sortStack(s1) for i in range(s1.size()): self.assertEqual(expected[i], s1.pop())
def setUp(self): self.fixture = Stack() self.assertTrue(self.fixture.isEmpty())
class TestStack(unittest.TestCase): def setUp(self): self.fixture = Stack() self.assertTrue(self.fixture.isEmpty()) def test_iter(self): self.fixture.push(1) self.fixture.push(2) self.fixture.push(3) stackiter = iter(self.fixture) expected = [1, 2, 3] for i in stackiter: self.assertEqual(expected.pop(), i) def test_push_various_types(self): self.fixture.push(6) self.fixture.push(True) self.fixture.push('cat') self.assertEqual('cat', self.fixture.peek()) self.assertFalse(self.fixture.isEmpty()) self.assertEqual(3, self.fixture.size()) def test_pop_items_out(self): self.fixture.push(6) self.fixture.push(True) self.fixture.push('cat') self.assertEqual('cat', self.fixture.peek()) self.fixture.pop() self.assertEqual(True, self.fixture.peek()) self.fixture.pop() self.assertEqual(6, self.fixture.peek()) self.fixture.pop() self.assertTrue((self.fixture.isEmpty())) self.assertRaises(IndexError, self.fixture.pop) self.assertRaises(Exception, self.fixture.peek) def test_sort(self): s1 = Stack() s1.push(1) s1.push(2) s1.push(4) s1.push(9) s1.push(12) s1.push(11) s1.push(3) expected = [1, 2, 3, 4, 9, 11, 12] for i in enumerate(iter(self.fixture.sort(s1))): self.assertEqual(expected[i[0]], i[1]) s1.push(1) s1.push(2) s1.push(4) s1.push(9) s1.push(12) s1.push(11) s1.push(3) self.fixture.sortStack(s1) for i in range(s1.size()): self.assertEqual(expected[i], s1.pop())
def __init__(self): from calvin.data_structure.stack import Stack self.stack1=Stack() self.stack2=Stack()
class QueueUsingStacks(): def __init__(self): from calvin.data_structure.stack import Stack self.stack1=Stack() self.stack2=Stack() def isEmpty(self): return self.stack1.isEmpty() and self.stack2.isEmpty() def enqueue(self, item): self.stack1.push(item) def dequeue(self): self.__rotateStack() return self.stack2.pop() def peek(self): self.__rotateStack() return self.stack2.peek() def __rotateStack(self): if (self.isEmpty()): raise Exception("queue is empty!") if self.stack2.isEmpty(): while not self.stack1.isEmpty(): self.stack2.push(self.stack1.pop()) def size(self): return self.stack1.size() + self.stack2.size()
def baseball_game(self, arr): if arr is None: return None sum = 0 stack = Stack() for x in arr: if x.isdigit() or (x[0] == '-' and x[1:].isdigit()): sum += int(x) stack.push(x) elif x == 'C': sum -= int(stack.pop()) elif x == 'D': sum += 2 * int(stack.peek()) stack.push(2 * int(stack.peek())) elif x == '+': curr = stack.pop() prev = 0 if not stack.isEmpty(): prev = stack.pop() sum += (int(curr) + int(prev)) stack.push(int(curr) + int(prev)) stack.push(curr) return sum