This commit is contained in:
bt3gl 2023-07-30 21:40:09 -07:00
parent 1d44d182e2
commit a85ed914d3
320 changed files with 0 additions and 0 deletions

85
stacks/README.md Normal file
View file

@ -0,0 +1,85 @@
## stacks and queues
<br>
### stacks
<br>
* last in, first out structures (LIFO)
* useful in certain recursive algorithms, where you can push temp data as you recurse, and remove them from the (memory or data structure) stack as you backtrace.
<br>
----
### queues
<br>
* first in, first out structures (FIFO), i.e., items are removed at the same order they are added.
* queues can be implemented with two arrays or a dynamic array (linked list), as long as items are added and removed from opposite sides.
* if implemented with a dynamic array, a more efficient solution is to use a circular queue (ring buffer), i.e. a fixed-size array and two pointers to indicate the starting and ending positions. an advantage of circular queues is that we can use the spaces in front of the queue. in a normal queue, once the queue becomes full, we cannot insert the next element even if there is a space in front of the queue. but using the circular queue, we can use the space to store new values.
* queues are often used in breath-first search (where you store a list of nodes to be processed) or when implementing a cache.
<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/Stack.py Normal file
View 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}')