Skip to content

Enhance the implementation of Queue using list #8608

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 118 additions & 38 deletions data_structures/queue/queue_on_list.py
Original file line number Diff line number Diff line change
@@ -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()