def test_empty_after_multiple_pops():
    s = Stack()
    s.push('hello world')
    s.push('multiple values woah')
    s.pop()
    s.pop()
    assert s.top == None
Ejemplo n.º 2
0
def test_pop_too_many():
    letters = Stack()
    letters.push('A')
    letters.push('B')
    letters.push('C')
    assert letters.pop() == 'C'
    assert letters.pop() == 'B'
    assert letters.pop() == 'A'
    assert letters.pop() is None
Ejemplo n.º 3
0
def test_stacks_and_queues_3 ():
    stack = Stack()
    stack.push('a')
    stack.push('b')
    stack.push('c')
    stack.pop()
    actual = stack.top.value
    expected = 'b'
    assert actual == expected
Ejemplo n.º 4
0
def test_pop_all():
    letters = Stack()
    letters.push('A')
    letters.push('B')
    letters.push('C')
    assert letters.pop() == 'C'
    assert letters.pop() == 'B'
    assert letters.pop() == 'A'
    assert print_all(letters) == ''
def test_pop_some():
    s = Stack()

    s.push("apple")
    s.push("banana")
    s.push("cucumber")
    s.pop()
    actual = s.pop()
    expected = "banana"
    assert actual == expected
Ejemplo n.º 6
0
def test_stacks_and_queues_4 ():
    stack = Stack()
    stack.push('a')
    stack.push('b')
    stack.push('c')
    stack.pop()
    stack.pop()
    stack.pop()
    actual = stack.top
    expected = None
    assert actual == expected
Ejemplo n.º 7
0
def test_pop_until_empty():
    s = Stack()
    s.push("apple")
    s.push("banana")
    s.push("cucumber")
    s.pop()
    s.pop()
    s.pop()
    actual = s.is_empty()
    expected = True
    assert actual == expected
Ejemplo n.º 8
0
def test_can_successfully_pop_from_a_stack():
    new_stack = Stack()
    new_stack.push('red')
    new_stack.push('orange')
    new_stack.push('green')
    new_stack.push('blue')

    new_stack.pop()

    expected = 'green'
    actual = new_stack.peek()
    assert expected == actual
def test_pop_to_empty():
    stck2 = Stack()
    stck2.push(9)
    stck2.push(8)
    stck2.push(10)
    stck2.push(11)
    stck2.pop()
    stck2.pop()
    stck2.pop()
    stck2.pop()
    assert stck2.top == None
    assert stck2.is_empty() == True
Ejemplo n.º 10
0
class PseudoQueue(object):
    def __init__(self):
        self._stack1 = Stack()
        self._stack2 = Stack()

    def enqueue(self, val):
        self._stack1.push(val)

    def dequeue(self):
        if not self._stack2.peek():
            while self._stack1.peek():
                self._stack2.push(self._stack1.pop())
        return self._stack2.pop()
def multi_bracket_validation(input):
    stack = Stack()
    b = {')': '(', ']': '[', '}': '{'}
    for i in range(len(input) - 1):
        if input[i] in b.values():
            stack.push(input[i])
        if input[i] in b.keys():
            if stack.peek() == b[input[i]]:
                stack.pop()
            else:
                return False
    if stack.peek() == None:
        return True
    else:
        return False
Ejemplo n.º 12
0
def test_can_successfully_empty_a_stack_after_multiple_pops():
    new_stack = Stack()
    new_stack.push('red')
    new_stack.push('orange')
    new_stack.push('green')
    new_stack.push('blue')

    new_stack.pop()
    new_stack.pop()
    new_stack.pop()
    new_stack.pop()

    expected = True
    actual = new_stack.isempty()
    assert expected == actual
def test_pop_stack():
    stck2 = Stack()
    stck2.push(9)
    stck2.push(8)
    stck2.push(10)
    assert stck2.pop() == 10
    assert stck2.top.value == 8
Ejemplo n.º 14
0
def multi_bracket_validation(input):
    '''
    Input: string  Output: boolean stating True if brackets are balanced in the input string
    '''

    bracket_tower = Stack()

    for char in input:
        if char in ['(', '[', '{']:
            bracket_tower.push(char)
        elif char in [')', ']', '}']:
            if bracket_tower.is_empty():
                return False
            removed_char = bracket_tower.pop()

            if char == ')' and removed_char != '(':
                return False
            elif char == ']' and removed_char != '[':
                return False
            elif char == '}' and removed_char != '{':
                return False

    if bracket_tower.is_empty():
        return True
    return False
Ejemplo n.º 15
0
class Queue():
    def __init__(self):
        self.input_stack = Stack()
        self.output = Stack()

    def enqueue(self, value):
        self.input_stack.push(value)

    def dequeue(self):
        if self.output.peek() is not None:
            return output.pop()
        if self.input_stack.peek() is not None:
            while self.input_stack.peek() is not None:
                self.output.push(self.input_stack.pop())
            return self.output.pop()
        return None
 def depth_first(self, start):
     visited = []
     s = Stack()
     s.push(start)
     visited.append(start)
     while s.is_empty() == False:
         curr = s.peek()
         flag = False
         for neighbor in self.get_neighbors(curr):
             if neighbor[0] not in visited and flag == False:
                 s.push(neighbor[0])
                 visited.append(neighbor[0])
                 flag = True
         if flag == False:
             s.pop()
     return visited
class PseudoQueue:
    '''
    This PseudoQueue class definition can be used to create an instance of a queue data strucsture.  It will be composed of nodes and has the methods of enqueue, dequeue, is_empty, and peek.  It has attritubes of rear and front.  As an additional challenge... this class of PseudoQueues uses two stacks at its implementation layer.
    '''
    def __init__(self):
        self.front_stack = Stack()
        self.rear_stack = Stack()
        self.front = self.rear = None

    def enqueue(self, value=None):
        node = Node(value)
        if self.rear_stack.top:
            self.rear_stack.top.next_node = node
        self.rear_stack.top = node
        if not self.front_stack.top:
            self.front_stack.top = node
            self.front = self.front_stack.top

    def is_empty(self):
        return self.front_stack.is_empty()

    def dequeue(self, ):
        if self.front_stack.top == self.rear_stack.top:
            self.rear_stack.top = None
        value = self.front_stack.pop()
        print("front stack top is:")
        return value

    def peek(self):
        return self.front_stack.peek()
def bracket_validation(input):
    letters= []
    s = Stack()
    for x in input:
        if x is '{' or x is '(' or x is '[':
            s.push(x)
        if x is '}':
            temp=s.peek()
            if temp is '{':
                s.pop()
            else:
                return False    
        if x is ']':
            # return True
            temp=s.peek()
            if temp is '[':
                s.pop()
            else:
                return False
        if x is ')':
            temp=s.peek()
            if temp is '(':
                s.pop()
            else:
                return False
    if s.peek() is None:
        return True
    else:
        return False
class PseudoQueue():
  def __init__(self):
    self.stack1 = Stack()
    self.stack2 = Stack()

  def enqueue(self, value):
    self.stack1.push(value)

  def dequeue(self):
    while self.stack1.peek():
      if self.stack1.top.next == None:
        return self.stack1.top.value
      
      else:
        temp = self.stack1.pop()
        self.stack2.push(temp)
        continue
def test_stack_pop():
    s = Stack()
    s.push('hello world')
    s.push('multiple values woah')
    s.pop()
    assert s.top.value == 'hello world'
Ejemplo n.º 21
0
def test_pop_empty():
    s = Stack()
    with pytest.raises(InvalidOperationError) as e:
        s.pop()

    assert str(e.value) == "Not allowed on empty structure"
Ejemplo n.º 22
0
def test_pop_single():
    s = Stack()
    s.push("apple")
    actual = s.pop()
    expected = "apple"
    assert actual == expected
Ejemplo n.º 23
0
def test_stacks_and_queues_7 ():
    stack = Stack()
    actual = stack.pop()
    expected = None
    assert actual == expected
Ejemplo n.º 24
0
def test_pop():
    letters = Stack()
    letters.push('A')
    letters.push('B')
    letters.push('C')
    assert letters.pop() == 'C'
def test_stack_pop():
    stack3 = Stack()
    stack3.push(0)
    assert stack3.pop() == 0
    assert stack3.top == None
def test_pop_or_peek_empty_stack():
    stack = Stack()
    with pytest.raises(EmptyStackException):
        stack.peek()
    with pytest.raises(EmptyStackException):
        stack.pop()
def test_pop_empty():
    s = Stack()
    with pytest.raises(InvalidOperationError) as e:
        s.pop()

    assert str(e.value) == "Method not allowed on empty collection"
def test_stack_pop_exception():
    stack4 = Stack()
    with pytest.raises(Exception) as excep:
        stack4.pop()
        assert "EmptyStack" in excep
def test_empty_stack_pop():
    s = Stack()
    assert s.pop() == 'this stack is empty buddy'
Ejemplo n.º 30
0
def test_pop_on_empty_raises_exception():
    new_stack = Stack()
    expected = 'Exception'
    actual = new_stack.pop()
    assert expected == actual