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
def test_check_not_empty(): s = Stack() s.push("apple") s.push("banana") actual = s.is_empty() expected = False assert actual == expected
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 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_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
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_stack_is_empty(): stack6 = Stack() assert stack6.is_empty() == True stack6.push(5) assert stack6.is_empty() == False
def test_pop_from_empty(): stck = Stack() assert stck.is_empty() == True with pytest.raises(AttributeError): stck.peek()
def test_instantiate_empty_stack(): stck = Stack() assert stck.top == None assert stck.is_empty() == True