mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-02 06:46:18 -04:00
reorganize dir
Signed-off-by: Mia Steinkirch <mia.steinkirch@gmail.com>
This commit is contained in:
parent
1b6f705e7c
commit
a8e71c50db
276 changed files with 23954 additions and 0 deletions
0
ebook_src/abstract_structures/queues/__init__.py
Normal file
0
ebook_src/abstract_structures/queues/__init__.py
Normal file
109
ebook_src/abstract_structures/queues/animal_shelter.py
Normal file
109
ebook_src/abstract_structures/queues/animal_shelter.py
Normal file
|
@ -0,0 +1,109 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
""" A class for an animal shelter with two queues"""
|
||||
|
||||
|
||||
class Node(object):
|
||||
def __init__(self, animalName=None, animalKind=None, pointer=None):
|
||||
self.animalName = animalName
|
||||
self.animalKind = animalKind
|
||||
self.pointer = pointer
|
||||
self.timestamp = 0
|
||||
|
||||
|
||||
|
||||
class AnimalShelter(object):
|
||||
def __init__(self):
|
||||
self.headCat = None
|
||||
self.headDog = None
|
||||
self.tailCat = None
|
||||
self.tailDog = None
|
||||
self.animalNumber = 0
|
||||
|
||||
|
||||
# Queue any animal
|
||||
|
||||
def enqueue(self, animalName, animalKind):
|
||||
self.animalNumber += 1
|
||||
newAnimal = Node(animalName, animalKind)
|
||||
newAnimal.timestamp = self.animalNumber
|
||||
|
||||
if animalKind == 'cat':
|
||||
if not self.headCat:
|
||||
self.headCat = newAnimal
|
||||
if self.tailCat:
|
||||
self.tailCat.pointer = newAnimal
|
||||
self.tailCat = newAnimal
|
||||
|
||||
elif animalKind == 'dog':
|
||||
if not self.headDog:
|
||||
self.headDog = newAnimal
|
||||
if self.tailDog:
|
||||
self.tailDog.pointer = newAnimal
|
||||
self.tailDog = newAnimal
|
||||
|
||||
|
||||
# Dequeue methods
|
||||
|
||||
def dequeueDog(self):
|
||||
if self.headDog:
|
||||
newAnimal = self.headDog
|
||||
self.headDog = newAnimal.pointer
|
||||
return str(newAnimal.animalName)
|
||||
else:
|
||||
return 'No Dogs!'
|
||||
|
||||
|
||||
def dequeueCat(self):
|
||||
if self.headCat:
|
||||
newAnimal = self.headCat
|
||||
self.headCat = newAnimal.pointer
|
||||
return str(newAnimal.animalName)
|
||||
else:
|
||||
return 'No Cats!'
|
||||
|
||||
|
||||
def dequeueAny(self):
|
||||
if self.headCat and not self.headDog:
|
||||
return self.dequeueCat()
|
||||
elif self.headDog and not self.headCat:
|
||||
return self.dequeueDog()
|
||||
elif self.headDog and self.headCat:
|
||||
if self.headDog.timestamp < self.headCat.timestamp:
|
||||
return self.dequeueDog()
|
||||
else:
|
||||
return self.dequeueCat()
|
||||
else:
|
||||
return ('No Animals!')
|
||||
|
||||
|
||||
|
||||
def _print(self):
|
||||
print("Cats:")
|
||||
cats = self.headCat
|
||||
while cats:
|
||||
print(cats.animalName, cats.animalKind)
|
||||
cats = cats.pointer
|
||||
print("Dogs:")
|
||||
dogs = self.headDog
|
||||
while dogs:
|
||||
print(dogs.animalName, dogs.animalKind)
|
||||
dogs = dogs.pointer
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
qs = AnimalShelter()
|
||||
qs.enqueue('bob', 'cat')
|
||||
qs.enqueue('mia', 'cat')
|
||||
qs.enqueue('yoda', 'dog')
|
||||
qs.enqueue('wolf', 'dog')
|
||||
qs._print()
|
||||
|
||||
print("Deque one dog and one cat...")
|
||||
qs.dequeueDog()
|
||||
qs.dequeueCat()
|
||||
qs._print()
|
39
ebook_src/abstract_structures/queues/deque.py
Normal file
39
ebook_src/abstract_structures/queues/deque.py
Normal file
|
@ -0,0 +1,39 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
''' a class for a double ended queue (also inefficient) '''
|
||||
|
||||
from queue import Queue
|
||||
|
||||
class Deque(Queue):
|
||||
|
||||
def enqueue_back(self, item):
|
||||
self.items.append(item)
|
||||
|
||||
def dequeue_front(self):
|
||||
return self.items.pop(0)
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
queue = Deque()
|
||||
print("Is the queue empty? ", queue.isEmpty())
|
||||
print("Adding 0 to 10 in the queue...")
|
||||
for i in range(10):
|
||||
queue.enqueue(i)
|
||||
print("Queue size: ", queue.size())
|
||||
print("Queue peek : ", queue.peek())
|
||||
print("Dequeue...", queue.dequeue())
|
||||
print("Queue peek: ", queue.peek())
|
||||
print("Is the queue empty? ", queue.isEmpty())
|
||||
print(queue)
|
||||
|
||||
print("\nNow using the dequeue methods...")
|
||||
print("Dequeue from front...", queue.dequeue_front())
|
||||
print("Queue peek: ", queue.peek())
|
||||
print(queue)
|
||||
print("Queue from back...")
|
||||
queue.enqueue_back(50)
|
||||
print("Queue peek: ", queue.peek())
|
||||
print(queue)
|
80
ebook_src/abstract_structures/queues/linked_queue.py
Normal file
80
ebook_src/abstract_structures/queues/linked_queue.py
Normal file
|
@ -0,0 +1,80 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
''' Queue acts as a container for nodes (objects) that are inserted and removed according FIFO'''
|
||||
|
||||
|
||||
class Node(object):
|
||||
def __init__(self, value=None, pointer=None):
|
||||
self.value = value
|
||||
self.pointer = None
|
||||
|
||||
|
||||
class LinkedQueue(object):
|
||||
def __init__(self):
|
||||
self.head = None
|
||||
self.tail = None
|
||||
|
||||
|
||||
def isEmpty(self):
|
||||
return not bool(self.head)
|
||||
|
||||
|
||||
def dequeue(self):
|
||||
if self.head:
|
||||
value = self.head.value
|
||||
self.head = self.head.pointer
|
||||
return value
|
||||
else:
|
||||
print('Queue is empty, cannot dequeue.')
|
||||
|
||||
|
||||
def enqueue(self, value):
|
||||
node = Node(value)
|
||||
if not self.head:
|
||||
self.head = node
|
||||
self.tail = node
|
||||
else:
|
||||
if self.tail:
|
||||
self.tail.pointer = node
|
||||
self.tail = node
|
||||
|
||||
|
||||
def size(self):
|
||||
node = self.head
|
||||
num_nodes = 0
|
||||
while node:
|
||||
num_nodes += 1
|
||||
node = node.pointer
|
||||
return num_nodes
|
||||
|
||||
|
||||
def peek(self):
|
||||
return self.head.value
|
||||
|
||||
|
||||
def _print(self):
|
||||
node = self.head
|
||||
while node:
|
||||
print(node.value)
|
||||
node = node.pointer
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
queue = LinkedQueue()
|
||||
print("Is the queue empty? ", queue.isEmpty())
|
||||
print("Adding 0 to 10 in the queue...")
|
||||
for i in range(10):
|
||||
queue.enqueue(i)
|
||||
print("Is the queue empty? ", queue.isEmpty())
|
||||
queue._print()
|
||||
|
||||
print("Queue size: ", queue.size())
|
||||
print("Queue peek : ", queue.peek())
|
||||
print("Dequeue...", queue.dequeue())
|
||||
print("Queue peek: ", queue.peek())
|
||||
queue._print()
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
""" Using our deque class and Python's deque class """
|
||||
|
||||
|
||||
import string
|
||||
import collections
|
||||
|
||||
from deque import Deque
|
||||
|
||||
|
||||
STRIP = string.whitespace + string.punctuation + "\"'"
|
||||
|
||||
def palindrome_checker_with_deque(str1):
|
||||
|
||||
d1 = Deque()
|
||||
d2 = collections.deque()
|
||||
|
||||
for s in str1.lower():
|
||||
if s not in STRIP:
|
||||
d2.append(s)
|
||||
d1.enqueue(s)
|
||||
|
||||
|
||||
eq1 = True
|
||||
while d1.size() > 1 and eq1:
|
||||
if d1.dequeue_front() != d1.dequeue():
|
||||
eq1 = False
|
||||
|
||||
eq2 = True
|
||||
while len(d2) > 1 and eq2:
|
||||
if d2.pop() != d2.popleft():
|
||||
eq2 = False
|
||||
|
||||
return eq1, eq2
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
str1 = 'Madam Im Adam'
|
||||
str2 = 'Buffy is a Slayer'
|
||||
print(palindrome_checker_with_deque(str1))
|
||||
print(palindrome_checker_with_deque(str2))
|
63
ebook_src/abstract_structures/queues/queue.py
Normal file
63
ebook_src/abstract_structures/queues/queue.py
Normal file
|
@ -0,0 +1,63 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
class Queue(object):
|
||||
|
||||
def __init__(self):
|
||||
self.in_stack = []
|
||||
self.out_stack = []
|
||||
|
||||
def _transfer(self):
|
||||
while self.in_stack:
|
||||
self.out_stack.append(self.in_stack.pop())
|
||||
|
||||
def enqueue(self, item):
|
||||
return self.in_stack.append(item)
|
||||
|
||||
def dequeue(self):
|
||||
if not self.out_stack:
|
||||
self._transfer()
|
||||
if self.out_stack:
|
||||
return self.out_stack.pop()
|
||||
else:
|
||||
return "Queue empty!"
|
||||
|
||||
def size(self):
|
||||
return len(self.in_stack) + len(self.out_stack)
|
||||
|
||||
def peek(self):
|
||||
if not self.out_stack:
|
||||
self._transfer()
|
||||
if self.out_stack:
|
||||
return self.out_stack[-1]
|
||||
else:
|
||||
return "Queue empty!"
|
||||
|
||||
def __repr__(self):
|
||||
if not self.out_stack:
|
||||
self._transfer()
|
||||
if self.out_stack:
|
||||
return '{}'.format(self.out_stack)
|
||||
else:
|
||||
return "Queue empty!"
|
||||
|
||||
def isEmpty(self):
|
||||
return not (bool(self.in_stack) or bool(self.out_stack))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
queue = Queue()
|
||||
print("Is the queue empty? ", queue.isEmpty())
|
||||
print("Adding 0 to 10 in the queue...")
|
||||
for i in range(10):
|
||||
queue.enqueue(i)
|
||||
print("Queue size: ", queue.size())
|
||||
print("Queue peek : ", queue.peek())
|
||||
print("Dequeue...", queue.dequeue())
|
||||
print("Queue peek: ", queue.peek())
|
||||
print("Is the queue empty? ", queue.isEmpty())
|
||||
|
||||
print("Printing the queue...")
|
||||
print(queue)
|
Loading…
Add table
Add a link
Reference in a new issue