mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-22 08:21:12 -04:00
Add some cool queue, stacks, strings, math, bit manipulation examples (#35)
This commit is contained in:
parent
f3ee2cdf52
commit
0f455a0322
24 changed files with 932 additions and 13 deletions
120
stacks_and_queues/Queue.py
Normal file
120
stacks_and_queues/Queue.py
Normal file
|
@ -0,0 +1,120 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
# author: bt3gl
|
||||
|
||||
import heapq
|
||||
|
||||
|
||||
class Queue:
|
||||
|
||||
def __init__(self):
|
||||
self._in = []
|
||||
self._out = []
|
||||
|
||||
######################
|
||||
# Private methods
|
||||
######################
|
||||
def _transfer_in_to_out(self):
|
||||
while self._in:
|
||||
self._out.append(self._in.pop())
|
||||
|
||||
def __repr__(self):
|
||||
if not self._out:
|
||||
self._transfer_in_to_out()
|
||||
|
||||
return f'{self._out}'
|
||||
|
||||
######################
|
||||
# Properties
|
||||
######################
|
||||
@property
|
||||
def size(self):
|
||||
return len(self._in) + len(self._out)
|
||||
|
||||
@property
|
||||
def peek(self):
|
||||
if not self._out:
|
||||
self._transfer_in_to_out()
|
||||
if self._out:
|
||||
return self._out[-1]
|
||||
else:
|
||||
print('❌ Queue is empty, cannot peek.')
|
||||
|
||||
@property
|
||||
def is_empty(self):
|
||||
return not (bool(self._in) or bool(self._out))
|
||||
|
||||
######################
|
||||
# Public methods
|
||||
######################
|
||||
def enqueue(self, item):
|
||||
self._in.append(item)
|
||||
|
||||
def dequeue(self):
|
||||
if not self._out:
|
||||
while self._in:
|
||||
self.out.append(self._in.pop())
|
||||
|
||||
if self._out:
|
||||
self._out.pop()
|
||||
else:
|
||||
print('❌ Queue is empty, cannot dequeue.')
|
||||
|
||||
|
||||
class PriorityQueue:
|
||||
|
||||
def __init__(self):
|
||||
self.queue = []
|
||||
self.index = 0
|
||||
|
||||
def push(self, item, priority):
|
||||
heapq.heappush(self.queue, (-priority, self.index, item))
|
||||
self.index += 1
|
||||
|
||||
def pop(self):
|
||||
return heapq.heappop(self.queue)[-1]
|
||||
|
||||
class Item:
|
||||
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
|
||||
def __repr__(self):
|
||||
return f'{self.name}'
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
######################
|
||||
# Simple Queue
|
||||
######################
|
||||
print('🧪 Testing Queue...')
|
||||
queue = Queue()
|
||||
print(f"Is the queue empty? {queue.is_empty}")
|
||||
print("Adding 1 to 10 in the queue...")
|
||||
for i in range(1, 11):
|
||||
queue.enqueue(i)
|
||||
|
||||
print(f"Queue: {queue}")
|
||||
print(f"\nQueue size: {queue.size}")
|
||||
print(f"Queue peek : {queue.peek}")
|
||||
print(f"Is the queue empty? {queue.is_empty}")
|
||||
print(f"\nDequeue...")
|
||||
queue.dequeue()
|
||||
print(f"Queue: {queue}")
|
||||
print(f"\nQueue size: {queue.size}")
|
||||
print(f"Queue peek: {queue.peek}")
|
||||
print(f"Is the queue empty? {queue.is_empty}")
|
||||
|
||||
######################
|
||||
# Priority Queue
|
||||
######################
|
||||
print('\n\n🧪 Testing Priority Queue...')
|
||||
q = PriorityQueue()
|
||||
q.push(Item('Item 1'), 1)
|
||||
q.push(Item('Item 4'), 4)
|
||||
q.push(Item('Item 3'), 3)
|
||||
print(f"Priority Queue: {q.queue}")
|
||||
print(f"Pop: {q.pop()}")
|
||||
print(f"Priority Queue: {q.queue}")
|
|
@ -0,0 +1,59 @@
|
|||
## stacks and queues
|
||||
|
||||
<br>
|
||||
|
||||
### `Queues.py`
|
||||
|
||||
<br>
|
||||
|
||||
```python
|
||||
> python3 queues.py
|
||||
|
||||
🧪 Testing Queue...
|
||||
Is the queue empty? True
|
||||
Adding 1 to 10 in the queue...
|
||||
Queue: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
|
||||
|
||||
Queue size: 10
|
||||
Queue peek : 1
|
||||
Is the queue empty? False
|
||||
|
||||
Dequeue...
|
||||
Queue: [10, 9, 8, 7, 6, 5, 4, 3, 2]
|
||||
|
||||
Queue size: 9
|
||||
Queue peek: 2
|
||||
Is the queue empty? False
|
||||
|
||||
|
||||
🧪 Testing Priority Queue...
|
||||
Priority Queue: [(-4, 1, Item 4), (-1, 0, Item 1), (-3, 2, Item 3)]
|
||||
Pop: Item 4
|
||||
Priority Queue: [(-3, 2, Item 3), (-1, 0, Item 1)]
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### `stack.py`
|
||||
|
||||
<br>
|
||||
|
||||
```python
|
||||
python3 stack.py
|
||||
|
||||
Testing Stack...
|
||||
|
||||
Stack: [12, 13, 14, 15, 16, 17, 18, 19, 20]
|
||||
Stack size: 9
|
||||
Stack peek: 20
|
||||
Stack is empty: False
|
||||
Stack min: 12
|
||||
|
||||
Popping...
|
||||
20
|
||||
19
|
||||
18
|
||||
17
|
||||
16
|
||||
Stack: [12, 13, 14, 15]
|
||||
```
|
79
stacks_and_queues/Stack.py
Normal file
79
stacks_and_queues/Stack.py
Normal file
|
@ -0,0 +1,79 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
# author: bt3gl
|
||||
|
||||
|
||||
class Stack:
|
||||
|
||||
def __init__(self):
|
||||
self.content = []
|
||||
self.min_array = []
|
||||
self.min = float('inf')
|
||||
|
||||
######################
|
||||
# Private methods
|
||||
######################
|
||||
def __repr__(self):
|
||||
return f'{self.content}'
|
||||
|
||||
######################
|
||||
# Properties
|
||||
######################
|
||||
@property
|
||||
def size(self):
|
||||
return len(self.content)
|
||||
|
||||
@property
|
||||
def peek(self):
|
||||
if self.content:
|
||||
return self.content[-1]
|
||||
else:
|
||||
print('❌ Queue is empty, cannot peek.')
|
||||
|
||||
@property
|
||||
def is_empty(self):
|
||||
return not bool(self.content)
|
||||
|
||||
######################
|
||||
# Public methods
|
||||
######################
|
||||
def push(self, value):
|
||||
if value < self.min:
|
||||
self.min = value
|
||||
|
||||
self.content.append(value)
|
||||
self.min_array.append(self.min)
|
||||
|
||||
def pop(self):
|
||||
if self.content:
|
||||
value = self.content.pop()
|
||||
self.min_array.pop()
|
||||
if self.min_array:
|
||||
self.min = self.min_array[-1]
|
||||
return value
|
||||
|
||||
def find_min(self):
|
||||
if self.min_array:
|
||||
return self.min_array[-1]
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
######################
|
||||
# Simple Stack
|
||||
######################
|
||||
print('Testing Stack...')
|
||||
stack = Stack()
|
||||
for i in range(12, 21):
|
||||
stack.push(i)
|
||||
|
||||
print(f'\nStack: {stack}')
|
||||
print(f'Stack size: {stack.size}')
|
||||
print(f'Stack peek: {stack.peek}')
|
||||
print(f'Stack is empty: {stack.is_empty}')
|
||||
print(f'Stack min: {stack.find_min()}')
|
||||
|
||||
print('\nPopping...')
|
||||
for i in range(5):
|
||||
print(stack.pop())
|
||||
print(f'Stack: {stack}')
|
Loading…
Add table
Add a link
Reference in a new issue