From 97bf01d55e23f353619d98535e8b7e0675a25a42 Mon Sep 17 00:00:00 2001 From: Amirsoroush Date: Sun, 2 Apr 2023 20:31:37 +0300 Subject: [PATCH 1/3] enhance the implementation of queue using list --- data_structures/queue/queue_on_list.py | 156 +++++++++++++++++++------ 1 file changed, 118 insertions(+), 38 deletions(-) diff --git a/data_structures/queue/queue_on_list.py b/data_structures/queue/queue_on_list.py index 71fca6b2f5f4..a013ccbaf60e 100644 --- a/data_structures/queue/queue_on_list.py +++ b/data_structures/queue/queue_on_list.py @@ -1,52 +1,132 @@ """Queue represented by a Python list""" +from typing import Any + class Queue: - def __init__(self): - self.entries = [] - self.length = 0 - self.front = 0 + def __init__(self) -> None: + self.entries: list[Any] = [] + + def __str__(self) -> str: + """ + >>> queue = Queue() + >>> str(queue) + '<>' + >>> queue.put(10) + >>> queue.put(20) + >>> queue.put(30) + >>> str(queue) + '<10, 20, 30>' + """ + + return "<" + str(self.entries)[1:-1] + ">" + + def __len__(self) -> int: + """ + >>> queue = Queue() + >>> queue.put(10) + >>> queue.put(20) + >>> queue.put(30) + >>> len(queue) + 3 + """ + + return len(self.entries) - def __str__(self): - printed = "<" + str(self.entries)[1:-1] + ">" - return printed + def put(self, item: Any) -> None: + """Put `item` to the Queue - """Enqueues {@code item} - @param item - item to enqueue""" + >>> queue = Queue() + >>> queue.put(10) + >>> str(queue) + '<10>' + >>> queue.put(20) + >>> str(queue) + '<10, 20>' + >>> len(queue) + 2 + """ - def put(self, item): self.entries.append(item) - self.length = self.length + 1 - - """Dequeues {@code item} - @requirement: |self.length| > 0 - @return dequeued - item that was dequeued""" - - def get(self): - self.length = self.length - 1 - dequeued = self.entries[self.front] - # self.front-=1 - # self.entries = self.entries[self.front:] - self.entries = self.entries[1:] - return dequeued - - """Rotates the queue {@code rotation} times - @param rotation - number of times to rotate queue""" - - def rotate(self, rotation): + + def get(self) -> Any: + """Get `item` from the Queue + + >>> queue = Queue() + >>> queue.put(10) + >>> queue.get() == 10 + True + >>> len(queue) == 0 + True + >>> queue.get() + Traceback (most recent call last): + ... + IndexError: Queue is empty + """ + + if not self.entries: + raise IndexError("Queue is empty") + return self.entries.pop(0) + + def rotate(self, rotation: int) -> None: + """Rotate the items of the Queue `rotation` times + + >>> queue = Queue() + >>> for i in (10, 20, 30, 40): + ... queue.put(i) + ... + >>> str(queue) + '<10, 20, 30, 40>' + >>> queue.rotate(1) + >>> str(queue) + '<20, 30, 40, 10>' + >>> queue.rotate(2) + >>> str(queue) + '<40, 10, 20, 30>' + """ + + # An optimization to reduce the number of attribute look-ups in the for-loop. + put = self.entries.append + get = self.entries.pop + for _ in range(rotation): - self.put(self.get()) + put(get(0)) + + def get_front(self) -> Any: + """Get the front item from the Queue - """Enqueues {@code item} - @return item at front of self.entries""" + >>> queue = Queue() + >>> for i in (10, 20, 30): + ... queue.put(i) + ... + >>> queue.get_front() + 10 + >>> len(queue) == 3 + True + """ - def get_front(self): return self.entries[0] - """Returns the length of this.entries""" + def size(self) -> int: + """Returns the length of the Queue + + >>> queue = Queue() + >>> queue.put(10) + >>> queue.size() + 1 + >>> queue.put(20) + >>> queue.size() + 2 + >>> queue.get() + 10 + >>> queue.size() == 1 + True + """ + + return len(self.entries) + + +if __name__ == "__main__": + from doctest import testmod - def size(self): - return self.length + testmod() From 986292650ec51bbc70eeb4b9e4cd517493b43184 Mon Sep 17 00:00:00 2001 From: Amirsoroush Date: Sat, 8 Apr 2023 17:56:23 +0300 Subject: [PATCH 2/3] enhance readability of queue_on_list.py --- data_structures/queue/queue_on_list.py | 147 +++++++++++++------------ 1 file changed, 78 insertions(+), 69 deletions(-) diff --git a/data_structures/queue/queue_on_list.py b/data_structures/queue/queue_on_list.py index a013ccbaf60e..6bbfcaf56559 100644 --- a/data_structures/queue/queue_on_list.py +++ b/data_structures/queue/queue_on_list.py @@ -1,66 +1,96 @@ """Queue represented by a Python list""" -from typing import Any +from collections.abc import Iterable +from typing import Generic, TypeVar +_T = TypeVar("_T") -class Queue: - def __init__(self) -> None: - self.entries: list[Any] = [] - def __str__(self) -> str: +class QueueByList(Generic[_T]): + def __init__(self, iterable: Iterable[_T] | None = None) -> None: """ - >>> queue = Queue() - >>> str(queue) - '<>' - >>> queue.put(10) - >>> queue.put(20) - >>> queue.put(30) - >>> str(queue) - '<10, 20, 30>' + >>> QueueByList() + Queue(()) + >>> QueueByList([10, 20, 30]) + Queue((10, 20, 30)) + >>> QueueByList((i**2 for i in range(1, 4))) + Queue((1, 4, 9)) """ - - return "<" + str(self.entries)[1:-1] + ">" + self.entries: list[_T] = list(iterable or []) def __len__(self) -> int: """ - >>> queue = Queue() + >>> len(QueueByList()) + 0 + >>> from string import ascii_lowercase + >>> len(QueueByList(ascii_lowercase)) + 26 + >>> queue = QueueByList() + >>> for i in range(1, 11): + ... queue.put(i) + >>> len(queue) + 10 + >>> for i in range(2): + ... queue.get() + 1 + 2 + >>> len(queue) + 8 + """ + + return len(self.entries) + + def __repr__(self) -> str: + """ + >>> queue = QueueByList() + >>> queue + Queue(()) + >>> str(queue) + 'Queue(())' >>> queue.put(10) + >>> queue + Queue((10,)) >>> queue.put(20) >>> queue.put(30) - >>> len(queue) - 3 + >>> queue + Queue((10, 20, 30)) """ - return len(self.entries) + return f"Queue({tuple(self.entries)})" - def put(self, item: Any) -> None: + def put(self, item: _T) -> None: """Put `item` to the Queue - >>> queue = Queue() + >>> queue = QueueByList() >>> queue.put(10) - >>> str(queue) - '<10>' >>> queue.put(20) - >>> str(queue) - '<10, 20>' >>> len(queue) 2 + >>> queue + Queue((10, 20)) """ self.entries.append(item) - def get(self) -> Any: - """Get `item` from the Queue + def get(self) -> _T: + """ + Get `item` from the Queue - >>> queue = Queue() - >>> queue.put(10) - >>> queue.get() == 10 - True - >>> len(queue) == 0 - True + >>> queue = QueueByList((10, 20, 30)) + >>> queue.get() + 10 + >>> queue.put(40) + >>> queue.get() + 20 + >>> queue.get() + 30 + >>> len(queue) + 1 + >>> queue.get() + 40 >>> queue.get() Traceback (most recent call last): - ... + ... IndexError: Queue is empty """ @@ -71,59 +101,38 @@ def get(self) -> Any: def rotate(self, rotation: int) -> None: """Rotate the items of the Queue `rotation` times - >>> queue = Queue() - >>> for i in (10, 20, 30, 40): - ... queue.put(i) - ... - >>> str(queue) - '<10, 20, 30, 40>' + >>> queue = QueueByList([10, 20, 30, 40]) + >>> queue + Queue((10, 20, 30, 40)) >>> queue.rotate(1) - >>> str(queue) - '<20, 30, 40, 10>' + >>> queue + Queue((20, 30, 40, 10)) >>> queue.rotate(2) - >>> str(queue) - '<40, 10, 20, 30>' + >>> queue + Queue((40, 10, 20, 30)) """ - # An optimization to reduce the number of attribute look-ups in the for-loop. put = self.entries.append get = self.entries.pop for _ in range(rotation): put(get(0)) - def get_front(self) -> Any: + def get_front(self) -> _T: """Get the front item from the Queue - >>> queue = Queue() - >>> for i in (10, 20, 30): - ... queue.put(i) - ... + >>> queue = QueueByList((10, 20, 30)) >>> queue.get_front() 10 - >>> len(queue) == 3 - True - """ - - return self.entries[0] - - def size(self) -> int: - """Returns the length of the Queue - - >>> queue = Queue() - >>> queue.put(10) - >>> queue.size() - 1 - >>> queue.put(20) - >>> queue.size() - 2 + >>> queue + Queue((10, 20, 30)) >>> queue.get() 10 - >>> queue.size() == 1 - True + >>> queue.get_front() + 20 """ - return len(self.entries) + return self.entries[0] if __name__ == "__main__": From c2652f15bf0de9269bbed7346399e87c1e5d72b8 Mon Sep 17 00:00:00 2001 From: Amirsoroush Date: Sat, 8 Apr 2023 17:59:10 +0300 Subject: [PATCH 3/3] rename 'queue_on_list' to 'queue_by_list' to match the class name --- .../{queue_on_list.py => queue_by_list.py} | 282 +++++++++--------- 1 file changed, 141 insertions(+), 141 deletions(-) rename data_structures/queue/{queue_on_list.py => queue_by_list.py} (95%) diff --git a/data_structures/queue/queue_on_list.py b/data_structures/queue/queue_by_list.py similarity index 95% rename from data_structures/queue/queue_on_list.py rename to data_structures/queue/queue_by_list.py index 6bbfcaf56559..4b05be9fd08e 100644 --- a/data_structures/queue/queue_on_list.py +++ b/data_structures/queue/queue_by_list.py @@ -1,141 +1,141 @@ -"""Queue represented by a Python list""" - -from collections.abc import Iterable -from typing import Generic, TypeVar - -_T = TypeVar("_T") - - -class QueueByList(Generic[_T]): - def __init__(self, iterable: Iterable[_T] | None = None) -> None: - """ - >>> QueueByList() - Queue(()) - >>> QueueByList([10, 20, 30]) - Queue((10, 20, 30)) - >>> QueueByList((i**2 for i in range(1, 4))) - Queue((1, 4, 9)) - """ - self.entries: list[_T] = list(iterable or []) - - def __len__(self) -> int: - """ - >>> len(QueueByList()) - 0 - >>> from string import ascii_lowercase - >>> len(QueueByList(ascii_lowercase)) - 26 - >>> queue = QueueByList() - >>> for i in range(1, 11): - ... queue.put(i) - >>> len(queue) - 10 - >>> for i in range(2): - ... queue.get() - 1 - 2 - >>> len(queue) - 8 - """ - - return len(self.entries) - - def __repr__(self) -> str: - """ - >>> queue = QueueByList() - >>> queue - Queue(()) - >>> str(queue) - 'Queue(())' - >>> queue.put(10) - >>> queue - Queue((10,)) - >>> queue.put(20) - >>> queue.put(30) - >>> queue - Queue((10, 20, 30)) - """ - - return f"Queue({tuple(self.entries)})" - - def put(self, item: _T) -> None: - """Put `item` to the Queue - - >>> queue = QueueByList() - >>> queue.put(10) - >>> queue.put(20) - >>> len(queue) - 2 - >>> queue - Queue((10, 20)) - """ - - self.entries.append(item) - - def get(self) -> _T: - """ - Get `item` from the Queue - - >>> queue = QueueByList((10, 20, 30)) - >>> queue.get() - 10 - >>> queue.put(40) - >>> queue.get() - 20 - >>> queue.get() - 30 - >>> len(queue) - 1 - >>> queue.get() - 40 - >>> queue.get() - Traceback (most recent call last): - ... - IndexError: Queue is empty - """ - - if not self.entries: - raise IndexError("Queue is empty") - return self.entries.pop(0) - - def rotate(self, rotation: int) -> None: - """Rotate the items of the Queue `rotation` times - - >>> queue = QueueByList([10, 20, 30, 40]) - >>> queue - Queue((10, 20, 30, 40)) - >>> queue.rotate(1) - >>> queue - Queue((20, 30, 40, 10)) - >>> queue.rotate(2) - >>> queue - Queue((40, 10, 20, 30)) - """ - - put = self.entries.append - get = self.entries.pop - - for _ in range(rotation): - put(get(0)) - - def get_front(self) -> _T: - """Get the front item from the Queue - - >>> queue = QueueByList((10, 20, 30)) - >>> queue.get_front() - 10 - >>> queue - Queue((10, 20, 30)) - >>> queue.get() - 10 - >>> queue.get_front() - 20 - """ - - return self.entries[0] - - -if __name__ == "__main__": - from doctest import testmod - - testmod() +"""Queue represented by a Python list""" + +from collections.abc import Iterable +from typing import Generic, TypeVar + +_T = TypeVar("_T") + + +class QueueByList(Generic[_T]): + def __init__(self, iterable: Iterable[_T] | None = None) -> None: + """ + >>> QueueByList() + Queue(()) + >>> QueueByList([10, 20, 30]) + Queue((10, 20, 30)) + >>> QueueByList((i**2 for i in range(1, 4))) + Queue((1, 4, 9)) + """ + self.entries: list[_T] = list(iterable or []) + + def __len__(self) -> int: + """ + >>> len(QueueByList()) + 0 + >>> from string import ascii_lowercase + >>> len(QueueByList(ascii_lowercase)) + 26 + >>> queue = QueueByList() + >>> for i in range(1, 11): + ... queue.put(i) + >>> len(queue) + 10 + >>> for i in range(2): + ... queue.get() + 1 + 2 + >>> len(queue) + 8 + """ + + return len(self.entries) + + def __repr__(self) -> str: + """ + >>> queue = QueueByList() + >>> queue + Queue(()) + >>> str(queue) + 'Queue(())' + >>> queue.put(10) + >>> queue + Queue((10,)) + >>> queue.put(20) + >>> queue.put(30) + >>> queue + Queue((10, 20, 30)) + """ + + return f"Queue({tuple(self.entries)})" + + def put(self, item: _T) -> None: + """Put `item` to the Queue + + >>> queue = QueueByList() + >>> queue.put(10) + >>> queue.put(20) + >>> len(queue) + 2 + >>> queue + Queue((10, 20)) + """ + + self.entries.append(item) + + def get(self) -> _T: + """ + Get `item` from the Queue + + >>> queue = QueueByList((10, 20, 30)) + >>> queue.get() + 10 + >>> queue.put(40) + >>> queue.get() + 20 + >>> queue.get() + 30 + >>> len(queue) + 1 + >>> queue.get() + 40 + >>> queue.get() + Traceback (most recent call last): + ... + IndexError: Queue is empty + """ + + if not self.entries: + raise IndexError("Queue is empty") + return self.entries.pop(0) + + def rotate(self, rotation: int) -> None: + """Rotate the items of the Queue `rotation` times + + >>> queue = QueueByList([10, 20, 30, 40]) + >>> queue + Queue((10, 20, 30, 40)) + >>> queue.rotate(1) + >>> queue + Queue((20, 30, 40, 10)) + >>> queue.rotate(2) + >>> queue + Queue((40, 10, 20, 30)) + """ + + put = self.entries.append + get = self.entries.pop + + for _ in range(rotation): + put(get(0)) + + def get_front(self) -> _T: + """Get the front item from the Queue + + >>> queue = QueueByList((10, 20, 30)) + >>> queue.get_front() + 10 + >>> queue + Queue((10, 20, 30)) + >>> queue.get() + 10 + >>> queue.get_front() + 20 + """ + + return self.entries[0] + + +if __name__ == "__main__": + from doctest import testmod + + testmod()