mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-30 04:36:08 -04:00
Add files via upload
This commit is contained in:
parent
132a5700c4
commit
8a30000724
115
stacks_and_queues/circular_queue.py
Normal file
115
stacks_and_queues/circular_queue.py
Normal file
@ -0,0 +1,115 @@
|
|||||||
|
## implement a circular queue
|
||||||
|
|
||||||
|
|
||||||
|
class CircularQueue:
|
||||||
|
|
||||||
|
def __init__(self, k: int):
|
||||||
|
self.head = 0
|
||||||
|
self.tail = 0
|
||||||
|
self.size = k
|
||||||
|
self.queue = [None] * self.size
|
||||||
|
|
||||||
|
def enQueue(self, value: int) -> bool:
|
||||||
|
|
||||||
|
if value is None:
|
||||||
|
return False
|
||||||
|
|
||||||
|
if self.isFull():
|
||||||
|
return False
|
||||||
|
|
||||||
|
if self.isEmpty():
|
||||||
|
self.heard = 0
|
||||||
|
|
||||||
|
while self.queue[self.tail] != None:
|
||||||
|
self.tail += 1
|
||||||
|
if self.tail == self.size:
|
||||||
|
self.tail = 0
|
||||||
|
|
||||||
|
self.queue[self.tail] = value
|
||||||
|
return True
|
||||||
|
|
||||||
|
def deQueue(self) -> bool:
|
||||||
|
|
||||||
|
if self.isEmpty():
|
||||||
|
return False
|
||||||
|
|
||||||
|
value = self.queue[self.head]
|
||||||
|
self.queue[self.head] = None
|
||||||
|
self.head += 1
|
||||||
|
|
||||||
|
if self.head == self.size:
|
||||||
|
self.head = 0
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
def Front(self) -> int:
|
||||||
|
return self.queue[self.head] or -1
|
||||||
|
|
||||||
|
def Rear(self) -> int:
|
||||||
|
return self.queue[self.tail] or -1
|
||||||
|
|
||||||
|
def isEmpty(self) -> bool:
|
||||||
|
for n in self.queue:
|
||||||
|
if n is not None:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
def isFull(self) -> bool:
|
||||||
|
for n in self.queue:
|
||||||
|
if n is None:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
|
||||||
|
q = CircularQueue(5)
|
||||||
|
print(f'q: {q.queue}')
|
||||||
|
print(f'q.isEmpty(): {q.isEmpty()}')
|
||||||
|
print(f'q.isFull(): {q.isFull()}')
|
||||||
|
print(f'q.enQueue(1): {q.enQueue(1)}')
|
||||||
|
print(f'h: {q.head}, t: {q.tail}')
|
||||||
|
print(f'q.enQueue(2): {q.enQueue(2)}')
|
||||||
|
print(f'h: {q.head}, t: {q.tail}')
|
||||||
|
print(f'q.enQueue(3): {q.enQueue(3)}')
|
||||||
|
print(f'h: {q.head}, t: {q.tail}')
|
||||||
|
print(f'q.enQueue(4): {q.enQueue(4)}')
|
||||||
|
print(f'h: {q.head}, t: {q.tail}')
|
||||||
|
print(f'isFull(): {q.isFull()}')
|
||||||
|
print(f'q.enQueue(5): {q.enQueue(5)}')
|
||||||
|
print(f'h: {q.head}, t: {q.tail}')
|
||||||
|
print(f'isFull(): {q.isFull()}')
|
||||||
|
print(f'q.isEmpty(): {q.isEmpty()}')
|
||||||
|
print(f'q: {q.queue}')
|
||||||
|
print(f'q.enQueue(6): {q.enQueue(6)}')
|
||||||
|
print(f'h: {q.head}, t: {q.tail}')
|
||||||
|
print(f'isFull(): {q.isFull()}')
|
||||||
|
print(f'q.isEmpty(): {q.isEmpty()}')
|
||||||
|
print()
|
||||||
|
print(f'q.deQueue(): {q.deQueue()}')
|
||||||
|
print(f'q: {q.queue}')
|
||||||
|
print(f'h: {q.head}, t: {q.tail}')
|
||||||
|
print(f'q.deQueue(): {q.deQueue()}')
|
||||||
|
print(f'q: {q.queue}')
|
||||||
|
print(f'h: {q.head}, t: {q.tail}')
|
||||||
|
print(f'q.deQueue(): {q.deQueue()}')
|
||||||
|
print(f'q: {q.queue}')
|
||||||
|
print(f'h: {q.head}, t: {q.tail}')
|
||||||
|
print(f'q.deQueue(): {q.deQueue()}')
|
||||||
|
print(f'q: {q.queue}')
|
||||||
|
print(f'h: {q.head}, t: {q.tail}')
|
||||||
|
print(f'q.deQueue(): {q.deQueue()}')
|
||||||
|
print(f'q: {q.queue}')
|
||||||
|
print(f'h: {q.head}, t: {q.tail}')
|
||||||
|
print(f'q.deQueue(): {q.deQueue()}')
|
||||||
|
print(f'q: {q.queue}')
|
||||||
|
print(f'h: {q.head}, t: {q.tail}')
|
||||||
|
print(f'q.Front(): {q.Front()}')
|
||||||
|
print(f'q.isEmpty(): {q.isEmpty()}')
|
||||||
|
print(f'q.isFull(): {q.isFull()}')
|
||||||
|
print(f'q.deQueue(): {q.deQueue()}')
|
||||||
|
print(f'q: {q.queue}')
|
||||||
|
print(f'h: {q.head}, t: {q.tail}')
|
||||||
|
print(f'q.Front(): {q.Front()}')
|
||||||
|
print(f'q.isEmpty(): {q.isEmpty()}')
|
||||||
|
print(f'q.isFull(): {q.isFull()}')
|
107
stacks_and_queues/circular_queue_II.py
Normal file
107
stacks_and_queues/circular_queue_II.py
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
## implement a circular queue
|
||||||
|
|
||||||
|
|
||||||
|
class CircularQueue:
|
||||||
|
|
||||||
|
def __init__(self, k: int):
|
||||||
|
self.head = -1
|
||||||
|
self.tail = -1
|
||||||
|
self.size = k
|
||||||
|
self.queue = [None] * self.size
|
||||||
|
|
||||||
|
def enQueue(self, value: int) -> bool:
|
||||||
|
|
||||||
|
if self.isFull():
|
||||||
|
return False
|
||||||
|
|
||||||
|
if self.isEmpty():
|
||||||
|
head = 0;
|
||||||
|
|
||||||
|
self.tail = (self.tail + 1) % self.size
|
||||||
|
self.queue[self.tail] = value
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
def deQueue(self) -> bool:
|
||||||
|
|
||||||
|
if self.isEmpty():
|
||||||
|
return False
|
||||||
|
|
||||||
|
if self.head == self.tail:
|
||||||
|
self.head = -1
|
||||||
|
self.tail = -1
|
||||||
|
return True
|
||||||
|
|
||||||
|
self.head = (self.head + 1) % self.size
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
def Front(self) -> int:
|
||||||
|
if self.isEmpty():
|
||||||
|
return -1
|
||||||
|
return self.queue[self.head]
|
||||||
|
|
||||||
|
def Rear(self) -> int:
|
||||||
|
if self.isEmpty():
|
||||||
|
return -1
|
||||||
|
return self.queue[self.tail]
|
||||||
|
|
||||||
|
def isEmpty(self) -> bool:
|
||||||
|
return self.head == -1
|
||||||
|
|
||||||
|
def isFull(self) -> bool:
|
||||||
|
return (self.tail + 1) % self.size == self.head
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
|
||||||
|
q = CircularQueue(5)
|
||||||
|
print(f'q: {q.queue}')
|
||||||
|
print(f'q.isEmpty(): {q.isEmpty()}')
|
||||||
|
print(f'q.isFull(): {q.isFull()}')
|
||||||
|
print(f'q.enQueue(1): {q.enQueue(1)}')
|
||||||
|
print(f'h: {q.head}, t: {q.tail}')
|
||||||
|
print(f'q.enQueue(2): {q.enQueue(2)}')
|
||||||
|
print(f'h: {q.head}, t: {q.tail}')
|
||||||
|
print(f'q.enQueue(3): {q.enQueue(3)}')
|
||||||
|
print(f'h: {q.head}, t: {q.tail}')
|
||||||
|
print(f'q.enQueue(4): {q.enQueue(4)}')
|
||||||
|
print(f'h: {q.head}, t: {q.tail}')
|
||||||
|
print(f'isFull(): {q.isFull()}')
|
||||||
|
print(f'q.enQueue(5): {q.enQueue(5)}')
|
||||||
|
print(f'h: {q.head}, t: {q.tail}')
|
||||||
|
print(f'isFull(): {q.isFull()}')
|
||||||
|
print(f'q.isEmpty(): {q.isEmpty()}')
|
||||||
|
print(f'q: {q.queue}')
|
||||||
|
print(f'q.enQueue(6): {q.enQueue(6)}')
|
||||||
|
print(f'h: {q.head}, t: {q.tail}')
|
||||||
|
print(f'isFull(): {q.isFull()}')
|
||||||
|
print(f'q.isEmpty(): {q.isEmpty()}')
|
||||||
|
print()
|
||||||
|
print(f'q.deQueue(): {q.deQueue()}')
|
||||||
|
print(f'q: {q.queue}')
|
||||||
|
print(f'h: {q.head}, t: {q.tail}')
|
||||||
|
print(f'q.deQueue(): {q.deQueue()}')
|
||||||
|
print(f'q: {q.queue}')
|
||||||
|
print(f'h: {q.head}, t: {q.tail}')
|
||||||
|
print(f'q.deQueue(): {q.deQueue()}')
|
||||||
|
print(f'q: {q.queue}')
|
||||||
|
print(f'h: {q.head}, t: {q.tail}')
|
||||||
|
print(f'q.deQueue(): {q.deQueue()}')
|
||||||
|
print(f'q: {q.queue}')
|
||||||
|
print(f'h: {q.head}, t: {q.tail}')
|
||||||
|
print(f'q.deQueue(): {q.deQueue()}')
|
||||||
|
print(f'q: {q.queue}')
|
||||||
|
print(f'h: {q.head}, t: {q.tail}')
|
||||||
|
print(f'q.deQueue(): {q.deQueue()}')
|
||||||
|
print(f'q: {q.queue}')
|
||||||
|
print(f'h: {q.head}, t: {q.tail}')
|
||||||
|
print(f'q.Front(): {q.Front()}')
|
||||||
|
print(f'q.isEmpty(): {q.isEmpty()}')
|
||||||
|
print(f'q.isFull(): {q.isFull()}')
|
||||||
|
print(f'q.deQueue(): {q.deQueue()}')
|
||||||
|
print(f'q: {q.queue}')
|
||||||
|
print(f'h: {q.head}, t: {q.tail}')
|
||||||
|
print(f'q.Front(): {q.Front()}')
|
||||||
|
print(f'q.isEmpty(): {q.isEmpty()}')
|
||||||
|
print(f'q.isFull(): {q.isFull()}')
|
25
stacks_and_queues/moving_average.py
Normal file
25
stacks_and_queues/moving_average.py
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
'''
|
||||||
|
Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window.
|
||||||
|
|
||||||
|
Implement the MovingAverage class:
|
||||||
|
|
||||||
|
MovingAverage(int size) Initializes the object with the size of the window size.
|
||||||
|
double next(int val) Returns the moving average of the last size values of the stream.
|
||||||
|
'''
|
||||||
|
|
||||||
|
class MovingAverage:
|
||||||
|
|
||||||
|
def __init__(self, size: int):
|
||||||
|
self.queue = []
|
||||||
|
self.size = size
|
||||||
|
|
||||||
|
|
||||||
|
def next(self, val: int) -> float:
|
||||||
|
|
||||||
|
self.queue.append(val)
|
||||||
|
|
||||||
|
if len(self.queue) > self.size:
|
||||||
|
self.queue.pop(0)
|
||||||
|
|
||||||
|
return sum(self.queue) / len(self.queue)
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user