mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-18 01:00:15 +00:00
Enhance the implementation of Queue using list (#8608)
* enhance the implementation of queue using list * enhance readability of queue_on_list.py * rename 'queue_on_list' to 'queue_by_list' to match the class name
This commit is contained in:
parent
8cce9cf066
commit
384c407a26
141
data_structures/queue/queue_by_list.py
Normal file
141
data_structures/queue/queue_by_list.py
Normal file
|
@ -0,0 +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()
|
|
@ -1,52 +0,0 @@
|
|||
"""Queue represented by a Python list"""
|
||||
|
||||
|
||||
class Queue:
|
||||
def __init__(self):
|
||||
self.entries = []
|
||||
self.length = 0
|
||||
self.front = 0
|
||||
|
||||
def __str__(self):
|
||||
printed = "<" + str(self.entries)[1:-1] + ">"
|
||||
return printed
|
||||
|
||||
"""Enqueues {@code item}
|
||||
@param item
|
||||
item to enqueue"""
|
||||
|
||||
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):
|
||||
for _ in range(rotation):
|
||||
self.put(self.get())
|
||||
|
||||
"""Enqueues {@code item}
|
||||
@return item at front of self.entries"""
|
||||
|
||||
def get_front(self):
|
||||
return self.entries[0]
|
||||
|
||||
"""Returns the length of this.entries"""
|
||||
|
||||
def size(self):
|
||||
return self.length
|
Loading…
Reference in New Issue
Block a user