Exemplo n.º 1
0
 def test_pop(self):
     stack = Stack()
     stack.push(1)
     stack.push(2)
     self.assertEqual(stack.pop(), 2)
     stack.push(3)
     self.assertEqual(stack.pop(), 3)
     self.assertEqual(stack.pop(), 1)
     stack.pop()
     self.assertRaises(IndexError)
def parentheses_check(string):
    parentheses = Stack()
    pair = {'{': '}', '[': ']', '(': ')'}

    for char in string:
        if char in pair.keys():
            parentheses.push(char)
        elif char in pair.values():
            if not parentheses.isEmpty() and char == pair[parentheses.peek()]:
                parentheses.pop()
            else:
                return False

    return parentheses.isEmpty()
Exemplo n.º 3
0
 def test_push(self):
     stack = Stack()
     stack.push(3)
     self.assertEqual(stack.items[0], 3)
     self.assertEqual(len(stack), 1)
     for i in range(10):
         stack.push(i)
     self.assertEqual(stack.items[0], 3)
     self.assertEqual(stack.items[-1], 9)
Exemplo n.º 4
0
 def test_is_instance(self):
     stack = Stack()
     self.assertIsInstance(stack, Stack)
Exemplo n.º 5
0
 def test_len(self):
     stack = Stack()
     for i in range(10):
         stack.push(i)
         self.assertEqual(len(stack), i + 1)
Exemplo n.º 6
0
 def test_is_empty(self):
     stack = Stack()
     self.assertTrue(stack.isEmpty())
     stack.push(3)
     self.assertFalse(stack.isEmpty())
 def __init__(self):
     self.enqueue_stack = Stack()
     self.dequeue_stack = Stack()
class TwoStackQueue:
    def __init__(self):
        self.enqueue_stack = Stack()
        self.dequeue_stack = Stack()

    def enqueue(self, item):
        if self.enqueue_stack.isEmpty():
            while not self.dequeue_stack.isEmpty():
                self.enqueue_stack.push(self.dequeue_stack.pop())
        self.enqueue_stack.push(item)

    def dequeue(self):
        if self.dequeue_stack.isEmpty():
            if self.enqueue_stack.isEmpty():
                IndexError
            while not self.enqueue_stack.isEmpty():
                self.dequeue_stack.push(self.enqueue_stack.pop())
        return self.dequeue_stack.pop()

    def peek(self):
        if self.dequeue_stack.isEmpty():
            if self.enqueue_stack.isEmpty():
                IndexError
            while not self.enqueue_stack.isEmpty():
                self.dequeue_stack.push(self.enqueue_stack.pop())
        return self.dequeue_stack.peek()

    def isEmpty(self):
        return self.enqueue_stack.isEmpty() and self.dequeue_stack.isEmpty()

    def __len__(self):
        return max(len(self.enqueue_stack), len(self.dequeue_stack))