Esempio n. 1
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
Esempio n. 2
0
def test_check_not_empty():
    s = Stack()
    s.push("apple")
    s.push("banana")
    actual = s.is_empty()
    expected = False
    assert actual == expected
Esempio n. 3
0
def test_peek():
    s = Stack()
    s.push("apple")
    s.push("banana")
    actual = s.peek()
    expected = "banana"
    assert actual == expected
def test_pop_stack():
    stck2 = Stack()
    stck2.push(9)
    stck2.push(8)
    stck2.push(10)
    assert stck2.pop() == 10
    assert stck2.top.value == 8
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
def stack_test():
    stack = Stack()
    stack.push(4)
    stack.push(3)
    stack.push(2)
    stack.push(1)
    return stack
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
Esempio n. 8
0
def test_can_successfully_peek_the_stack():
    new_stack = Stack()
    new_stack.push('green')
    new_stack.push('blue')

    expected = 'blue'
    actual = new_stack.peek()
    assert expected == actual
Esempio n. 9
0
def test_stacks_and_queues_2 ():
    stack = Stack()
    stack.push('a')
    stack.push('b')
    stack.push('c')
    actual = stack.top.value
    expected = 'c'
    assert actual == expected
Esempio n. 10
0
def test_stacks_and_queues_5 ():
    stack = Stack()
    stack.push('a')
    stack.push('b')
    stack.push('c')
    actual = stack.peek()
    expected = 'c'
    assert actual == expected
Esempio n. 11
0
def test_stack_peek():
    letters = Stack()
    assert not letters.peek()

    letters.push('A')
    assert letters.peek() == 'A'

    letters.push('B')
    assert letters.peek() == 'B'
Esempio n. 12
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
Esempio n. 13
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) == ''
Esempio n. 14
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
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
Esempio n. 16
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
Esempio n. 18
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
def test_push_multiple():
    stck2 = Stack()
    stck2.push(9)
    stck2.push(8)
    stck2.push(10)
    stck2.push(7)
    stck2.push(11)
    assert stck2.top.value == 11
    assert stck2.top.next.value == 7
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
Esempio n. 22
0
def test_can_successfully_push_multiple_values_onto_a_stack():
    new_stack = Stack()
    new_stack.push('blue')
    new_stack.push('red')
    new_stack.push('orange')
    new_stack.push('green')

    expected = 'green'
    actual = new_stack.peek()
    assert expected == actual
Esempio n. 23
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
Esempio n. 24
0
def test_push_onto_full():
    s = Stack()
    s.push("apple")
    s.push("banana")
    s.push("cucumber")
    actual = s.top.value
    expected = "cucumber"
    assert actual == expected
Esempio n. 25
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_some():
    s = Stack()

    s.push("apple")
    s.push("banana")
    s.push("cucumber")
    s.pop()
    actual = s.pop()
    expected = "banana"
    assert actual == expected
Esempio n. 27
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
def test_stack_peek():
    s = Stack()
    s.push('hello world')
    s.push('quit peeking')
    assert s.peek() == 'quit peeking'
def test_stack_pop():
    s = Stack()
    s.push('hello world')
    s.push('multiple values woah')
    s.pop()
    assert s.top.value == 'hello world'
def test_stack_push():
    s = Stack()
    s.push('hello world')
    assert s.top.value == 'hello world'