-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q06_animal_shelter.py
52 lines (38 loc) · 1.31 KB
/
Q06_animal_shelter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import unittest
from collections import deque
class AnimalShelter:
def __init__(self):
self.dogs = deque([])
self.cats = deque([])
def enqueue(self, animal):
if animal.__class__ == Cat:
self.cats.appendleft(animal)
else:
self.dogs.appendleft(animal)
def dequeue_dog(self):
return self.dogs.pop()
def dequeue_cat(self):
return self.cats.pop()
def dequeue_any(self):
if not self.cats or self.dogs[-1].time_rescued < self.cats[-1].time_rescued:
return self.dequeue_dog()
return self.dequeue_cat()
class Animal:
def __init__(self, name, time_rescued):
self.name = name
self.time_rescued = time_rescued
class Cat(Animal):
pass
class Dog(Animal):
pass
class Unittest(unittest.TestCase):
def test_animal_shelter(self):
shelter = AnimalShelter()
shelter.enqueue(Cat("Garfield", 10))
shelter.enqueue(Dog("Fluffy", 15))
shelter.enqueue(Dog("Scooby", 20))
shelter.enqueue(Cat("Simba", 22))
self.assertEqual("Garfield", shelter.dequeue_any().name)
self.assertEqual("Fluffy", shelter.dequeue_dog().name)
self.assertEqual("Scooby", shelter.dequeue_any().name)
self.assertEqual("Simba", shelter.dequeue_cat().name)