mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-25 18:38:39 +00:00
Compare commits
No commits in common. "629eb86ce0d30dd6031fa482f4a477ac3df345ab" and "8cce9cf066396bb220515c03849fbc1a16d800d0" have entirely different histories.
629eb86ce0
...
8cce9cf066
@ -740,7 +740,6 @@
|
|||||||
* [Tower Of Hanoi](other/tower_of_hanoi.py)
|
* [Tower Of Hanoi](other/tower_of_hanoi.py)
|
||||||
|
|
||||||
## Physics
|
## Physics
|
||||||
* [Altitude Pressure](physics/altitude_pressure.py)
|
|
||||||
* [Archimedes Principle](physics/archimedes_principle.py)
|
* [Archimedes Principle](physics/archimedes_principle.py)
|
||||||
* [Basic Orbital Capture](physics/basic_orbital_capture.py)
|
* [Basic Orbital Capture](physics/basic_orbital_capture.py)
|
||||||
* [Casimir Effect](physics/casimir_effect.py)
|
* [Casimir Effect](physics/casimir_effect.py)
|
||||||
|
@ -1,141 +0,0 @@
|
|||||||
"""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()
|
|
52
data_structures/queue/queue_on_list.py
Normal file
52
data_structures/queue/queue_on_list.py
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
"""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
|
@ -57,7 +57,7 @@ def collatz_sequence(n: int) -> Generator[int, None, None]:
|
|||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
n = int(input("Your number: "))
|
n = 43
|
||||||
sequence = tuple(collatz_sequence(n))
|
sequence = tuple(collatz_sequence(n))
|
||||||
print(sequence)
|
print(sequence)
|
||||||
print(f"Collatz sequence from {n} took {len(sequence)} steps.")
|
print(f"Collatz sequence from {n} took {len(sequence)} steps.")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user