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
def test_stack_peek(): letters = Stack() assert not letters.peek() letters.push('A') assert letters.peek() == 'A' letters.push('B') assert letters.peek() == 'B'
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
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()
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 test_peek(): s = Stack() s.push("apple") s.push("banana") actual = s.peek() expected = "banana" assert actual == expected
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
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
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
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 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(): 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_empty_stack_peek(): s = Stack() assert s.peek() == 'this stack is empty buddy'
def test_stack_peek(): s = Stack() s.push('hello world') s.push('quit peeking') assert s.peek() == 'quit peeking'
def test_peek_empty(): s = Stack() with pytest.raises(InvalidOperationError) as e: s.peek() assert str(e.value) == "Not allowed on empty structure"
def test_peek_stack(): stck2 = Stack() stck2.push(9) stck2.push(8) stck2.push(10) assert stck2.peek() == 10
def test_pop_from_empty(): stck = Stack() assert stck.is_empty() == True with pytest.raises(AttributeError): stck.peek()
def test_stack_peek(): stack5 = Stack() stack5.push(5) assert stack5.peek() == 5 assert stack5.top.val == 5
def test_pop_or_peek_empty_stack(): stack = Stack() with pytest.raises(EmptyStackException): stack.peek() with pytest.raises(EmptyStackException): stack.pop()
def test_peek_empty(): s = Stack() with pytest.raises(InvalidOperationError) as e: s.peek() assert str(e.value) == "Method not allowed on empty collection"
def test_peek_on_empty_raises_exception(): new_stack = Stack() expected = 'Exception' actual = new_stack.peek() assert expected == actual
def test_can_successfully_push_to_a_stack(): new_stack = Stack() new_stack.push('blue') expected = 'blue' actual = new_stack.peek() assert expected == actual