|
| 1 | +"""Queue represented by a Python list""" |
| 2 | + |
| 3 | +from collections.abc import Iterable |
| 4 | +from typing import Generic, TypeVar |
| 5 | + |
| 6 | +_T = TypeVar("_T") |
| 7 | + |
| 8 | + |
| 9 | +class QueueByList(Generic[_T]): |
| 10 | + def __init__(self, iterable: Iterable[_T] | None = None) -> None: |
| 11 | + """ |
| 12 | + >>> QueueByList() |
| 13 | + Queue(()) |
| 14 | + >>> QueueByList([10, 20, 30]) |
| 15 | + Queue((10, 20, 30)) |
| 16 | + >>> QueueByList((i**2 for i in range(1, 4))) |
| 17 | + Queue((1, 4, 9)) |
| 18 | + """ |
| 19 | + self.entries: list[_T] = list(iterable or []) |
| 20 | + |
| 21 | + def __len__(self) -> int: |
| 22 | + """ |
| 23 | + >>> len(QueueByList()) |
| 24 | + 0 |
| 25 | + >>> from string import ascii_lowercase |
| 26 | + >>> len(QueueByList(ascii_lowercase)) |
| 27 | + 26 |
| 28 | + >>> queue = QueueByList() |
| 29 | + >>> for i in range(1, 11): |
| 30 | + ... queue.put(i) |
| 31 | + >>> len(queue) |
| 32 | + 10 |
| 33 | + >>> for i in range(2): |
| 34 | + ... queue.get() |
| 35 | + 1 |
| 36 | + 2 |
| 37 | + >>> len(queue) |
| 38 | + 8 |
| 39 | + """ |
| 40 | + |
| 41 | + return len(self.entries) |
| 42 | + |
| 43 | + def __repr__(self) -> str: |
| 44 | + """ |
| 45 | + >>> queue = QueueByList() |
| 46 | + >>> queue |
| 47 | + Queue(()) |
| 48 | + >>> str(queue) |
| 49 | + 'Queue(())' |
| 50 | + >>> queue.put(10) |
| 51 | + >>> queue |
| 52 | + Queue((10,)) |
| 53 | + >>> queue.put(20) |
| 54 | + >>> queue.put(30) |
| 55 | + >>> queue |
| 56 | + Queue((10, 20, 30)) |
| 57 | + """ |
| 58 | + |
| 59 | + return f"Queue({tuple(self.entries)})" |
| 60 | + |
| 61 | + def put(self, item: _T) -> None: |
| 62 | + """Put `item` to the Queue |
| 63 | +
|
| 64 | + >>> queue = QueueByList() |
| 65 | + >>> queue.put(10) |
| 66 | + >>> queue.put(20) |
| 67 | + >>> len(queue) |
| 68 | + 2 |
| 69 | + >>> queue |
| 70 | + Queue((10, 20)) |
| 71 | + """ |
| 72 | + |
| 73 | + self.entries.append(item) |
| 74 | + |
| 75 | + def get(self) -> _T: |
| 76 | + """ |
| 77 | + Get `item` from the Queue |
| 78 | +
|
| 79 | + >>> queue = QueueByList((10, 20, 30)) |
| 80 | + >>> queue.get() |
| 81 | + 10 |
| 82 | + >>> queue.put(40) |
| 83 | + >>> queue.get() |
| 84 | + 20 |
| 85 | + >>> queue.get() |
| 86 | + 30 |
| 87 | + >>> len(queue) |
| 88 | + 1 |
| 89 | + >>> queue.get() |
| 90 | + 40 |
| 91 | + >>> queue.get() |
| 92 | + Traceback (most recent call last): |
| 93 | + ... |
| 94 | + IndexError: Queue is empty |
| 95 | + """ |
| 96 | + |
| 97 | + if not self.entries: |
| 98 | + raise IndexError("Queue is empty") |
| 99 | + return self.entries.pop(0) |
| 100 | + |
| 101 | + def rotate(self, rotation: int) -> None: |
| 102 | + """Rotate the items of the Queue `rotation` times |
| 103 | +
|
| 104 | + >>> queue = QueueByList([10, 20, 30, 40]) |
| 105 | + >>> queue |
| 106 | + Queue((10, 20, 30, 40)) |
| 107 | + >>> queue.rotate(1) |
| 108 | + >>> queue |
| 109 | + Queue((20, 30, 40, 10)) |
| 110 | + >>> queue.rotate(2) |
| 111 | + >>> queue |
| 112 | + Queue((40, 10, 20, 30)) |
| 113 | + """ |
| 114 | + |
| 115 | + put = self.entries.append |
| 116 | + get = self.entries.pop |
| 117 | + |
| 118 | + for _ in range(rotation): |
| 119 | + put(get(0)) |
| 120 | + |
| 121 | + def get_front(self) -> _T: |
| 122 | + """Get the front item from the Queue |
| 123 | +
|
| 124 | + >>> queue = QueueByList((10, 20, 30)) |
| 125 | + >>> queue.get_front() |
| 126 | + 10 |
| 127 | + >>> queue |
| 128 | + Queue((10, 20, 30)) |
| 129 | + >>> queue.get() |
| 130 | + 10 |
| 131 | + >>> queue.get_front() |
| 132 | + 20 |
| 133 | + """ |
| 134 | + |
| 135 | + return self.entries[0] |
| 136 | + |
| 137 | + |
| 138 | +if __name__ == "__main__": |
| 139 | + from doctest import testmod |
| 140 | + |
| 141 | + testmod() |
0 commit comments