|
| 1 | +#!/usr/bin/python3 |
| 2 | +'''Queue via Stacks: Implement a MyQueue class |
| 3 | +which implements a queue using two stacks.''' |
| 4 | +import unittest |
| 5 | + |
| 6 | + |
| 7 | +class Stack: |
| 8 | + def __init__(self): |
| 9 | + self.stack = [] |
| 10 | + self.min = None |
| 11 | + |
| 12 | + def getMin(self): |
| 13 | + return self.min |
| 14 | + |
| 15 | + def pop(self): |
| 16 | + if not self.length: |
| 17 | + print('No stack to pop') |
| 18 | + return |
| 19 | + |
| 20 | + item = self.stack.pop() |
| 21 | + |
| 22 | + if item == self.min: |
| 23 | + if self.length() > 0: |
| 24 | + self.findMin() |
| 25 | + else: |
| 26 | + self.min = None |
| 27 | + return item |
| 28 | + |
| 29 | + def findMin(self): |
| 30 | + self.min = self.stack[0] |
| 31 | + for x in self.stack: |
| 32 | + if x < self.min: |
| 33 | + self.min = x |
| 34 | + |
| 35 | + def length(self): |
| 36 | + return len(self.stack) |
| 37 | + |
| 38 | + def push(self, value): |
| 39 | + if not self.min: |
| 40 | + self.min = value |
| 41 | + elif value < self.min: |
| 42 | + self.min = value |
| 43 | + |
| 44 | + self.stack.append(value) |
| 45 | + |
| 46 | + def peek(self): |
| 47 | + if not self.length: |
| 48 | + print('No stack to peek') |
| 49 | + return |
| 50 | + return self.stack[-1] |
| 51 | + |
| 52 | + def printStack(self): |
| 53 | + if not self.length: |
| 54 | + print('No stack to peeek') |
| 55 | + return |
| 56 | + |
| 57 | + print("Top of stack\n _ ") |
| 58 | + for x in self.stack[::-1]: |
| 59 | + print('|', x, '|') |
| 60 | + print('|', '_', '|') |
| 61 | + |
| 62 | + |
| 63 | +class MyQueue: |
| 64 | + def __init__(self): |
| 65 | + self.newStack = Stack() |
| 66 | + self.oldStack = Stack() |
| 67 | + |
| 68 | + def push(self, value): |
| 69 | + self.newStack.push(value) |
| 70 | + |
| 71 | + def pop(self): |
| 72 | + self.rotate() |
| 73 | + if self.oldStack.length() == 0: |
| 74 | + print('Nothing to pop in this queue') |
| 75 | + return |
| 76 | + return self.oldStack.pop() |
| 77 | + |
| 78 | + def peek(self): |
| 79 | + self.rotate() |
| 80 | + if self.oldStack.length() == 0: |
| 81 | + print('Nothing to peek in this queue') |
| 82 | + return |
| 83 | + return self.oldStack.peek() |
| 84 | + |
| 85 | + def rotate(self): |
| 86 | + if (self.oldStack.length() == 0): |
| 87 | + while not self.newStack.length() == 0: |
| 88 | + self.oldStack.push(self.newStack.pop()) |
| 89 | + |
| 90 | + def printQueue(self): |
| 91 | + self.rotate() |
| 92 | + for x in self.oldStack.stack[::-1]: |
| 93 | + print('Item in Queue -->', x) |
| 94 | + |
| 95 | + for x in self.newStack.stack: |
| 96 | + print('Item in Queue -->', x) |
| 97 | + |
| 98 | + |
| 99 | +class Test(unittest.TestCase): |
| 100 | + @classmethod |
| 101 | + def setUpClass(cls): |
| 102 | + pass |
| 103 | + |
| 104 | + @classmethod |
| 105 | + def tearDownClass(cls): |
| 106 | + pass |
| 107 | + |
| 108 | + def test1(self): |
| 109 | + arr = [2, 5, 7, 10, 3, 10, -1, -100] |
| 110 | + print(arr) |
| 111 | + q = MyQueue() |
| 112 | + for x in arr: |
| 113 | + q.push(x) |
| 114 | + |
| 115 | + q.printQueue() |
| 116 | + print('First in Queue --> Popped', q.pop()) |
| 117 | + print('Second in Queue --> Popped', q.pop()) |
| 118 | + q.printQueue() |
| 119 | + print('Let\'s take a peek -->', q.peek()) |
| 120 | + |
| 121 | + |
| 122 | +if __name__ == '__main__': |
| 123 | + unittest.main() |
0 commit comments