fix few details, stacks

This commit is contained in:
Mari Wahl 2014-08-27 16:28:13 -04:00
parent 415e9c02e2
commit dffd4fb2b7
2 changed files with 68 additions and 52 deletions

Binary file not shown.

View File

@ -3,92 +3,108 @@
__author__ = "Mari Wahl" __author__ = "Mari Wahl"
__email__ = "marina.w4hl@gmail.com" __email__ = "marina.w4hl@gmail.com"
""" A class for an animal shelter"""
""" A class for an animal shelter with two queues"""
class Node(object): class Node(object):
def __init__(self, name, which): def __init__(self, animalName=None, animalKind=None, pointer=None):
self.name = name self.animalName = animalName
self.which = which self.animalKind = animalKind
self.next = next self.pointer = pointer
self.timestamp = 0 self.timestamp = 0
class AnimalShelter(object): class AnimalShelter(object):
def __init__(self): def __init__(self):
self.first_cat = None self.headCat = None
self.first_dog = None self.headDog = None
self.last_cat = None self.tailCat = None
self.last_dog = None self.tailDog = None
self.counter = 0 self.animalNumber = 0
def enqueue(self, name, which): # Queue any animal
self.counter += 1
node = Node(name, which)
node.timestamp = self.counter
if which == 'cat': def enqueue(self, animalName, animalKind):
if not self.first_cat: self.animalNumber += 1
self.first_cat = node newAnimal = Node(animalName, animalKind)
if self.last_cat: newAnimal.timestamp = self.animalNumber
self.last_cat.next = node
self.last_cat = node
if which == 'dog': if animalKind == 'cat':
if not self.first_dog: if not self.headCat:
self.first_dog = node self.headCat = newAnimal
if self.last_dog: if self.tailCat:
self.last_dog.next = node self.tailCat.pointer = newAnimal
self.last_dog = node 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): def dequeueDog(self):
if self.first_dog: if self.headDog:
node = self.first_dog newAnimal = self.headDog
self.first_dog = node.next self.headDog = newAnimal.pointer
return str(node.name) return str(newAnimal.animalName)
raise Exception('No Dogs!') else:
return 'No Dogs!'
def dequeueCat(self): def dequeueCat(self):
if self.first_cat: if self.headCat:
node = self.first_cat newAnimal = self.headCat
self.first_cat = node.next self.headCat = newAnimal.pointer
return str(node.name) return str(newAnimal.animalName)
raise Exception('No Cats!') else:
return 'No Cats!'
def dequeueAny(self): def dequeueAny(self):
nodecat = self.first_cat if self.headCat and not self.headDog:
nodedog = self.first_dog
if nodecat and not nodedog:
return self.dequeueCat() return self.dequeueCat()
elif nodedog and not nodecat: elif self.headDog and not self.headCat:
return self.dequeueDog() return self.dequeueDog()
elif nodedog and nodecat: elif self.headDog and self.headCat:
if nodedog.timestamp < nodecat.timestamp: if self.headDog.timestamp < self.headCat.timestamp:
return self.dequeueDog() return self.dequeueDog()
else: else:
return self.dequeueCat() return self.dequeueCat()
raise Exception('No Animals!') 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__':
def main():
qs = AnimalShelter() qs = AnimalShelter()
qs.enqueue('bob', 'cat') qs.enqueue('bob', 'cat')
qs.enqueue('mia', 'cat') qs.enqueue('mia', 'cat')
qs.enqueue('yoda', 'dog') qs.enqueue('yoda', 'dog')
qs.enqueue('wolf', 'dog') qs.enqueue('wolf', 'dog')
qs._print()
assert(qs.dequeueDog() == 'yoda') print("Deque one dog and one cat...")
assert(qs.dequeueCat() == 'bob') qs.dequeueDog()
print(qs.dequeueAny() == 'mia') qs.dequeueCat()
qs._print()
if __name__ == '__main__':
main()