| 4 | |
| 5 | |
| 6 | class Queue: |
| 7 | def __init__(self): |
| 8 | self.stack = [] |
| 9 | self.length = 0 |
| 10 | |
| 11 | def __str__(self): |
| 12 | printed = "<" + str(self.stack)[1:-1] + ">" |
| 13 | return printed |
| 14 | |
| 15 | """Enqueues {@code item} |
| 16 | @param item |
| 17 | item to enqueue""" |
| 18 | |
| 19 | def put(self, item: Any) -> None: |
| 20 | self.stack.append(item) |
| 21 | self.length = self.length + 1 |
| 22 | |
| 23 | """Dequeues {@code item} |
| 24 | @requirement: |self.length| > 0 |
| 25 | @return dequeued |
| 26 | item that was dequeued""" |
| 27 | |
| 28 | def get(self) -> Any: |
| 29 | self.rotate(1) |
| 30 | dequeued = self.stack[self.length - 1] |
| 31 | self.stack = self.stack[:-1] |
| 32 | self.rotate(self.length - 1) |
| 33 | self.length = self.length - 1 |
| 34 | return dequeued |
| 35 | |
| 36 | """Rotates the queue {@code rotation} times |
| 37 | @param rotation |
| 38 | number of times to rotate queue""" |
| 39 | |
| 40 | def rotate(self, rotation: int) -> None: |
| 41 | for _ in range(rotation): |
| 42 | temp = self.stack[0] |
| 43 | self.stack = self.stack[1:] |
| 44 | self.put(temp) |
| 45 | self.length = self.length - 1 |
| 46 | |
| 47 | """Reports item at the front of self |
| 48 | @return item at front of self.stack""" |
| 49 | |
| 50 | def front(self) -> Any: |
| 51 | front = self.get() |
| 52 | self.put(front) |
| 53 | self.rotate(self.length - 1) |
| 54 | return front |
| 55 | |
| 56 | """Returns the length of this.stack""" |
| 57 | |
| 58 | def size(self) -> int: |
| 59 | return self.length |
no outgoing calls
no test coverage detected