Пример #1
0
class Shelter():
	def __init__(self):
		self.Cats = Queue()
		self.Dogs = Queue()
		self.id = 0

	def Add(self, animal, species):
		"""take a cat or dog and its name, add to the shelter"""
		self.id +=1
		if species == 'Cat':
			self.Cats.Push((animal, species , self.id))  
		elif species == 'Dog':
			self.Dogs.Push((animal, species , self.id))
		else:
			raise ValueError('Must specify the species of the animal. String: Cat or Dog')

	def Adopt(self, preference = None):
		""" if preference given, adopt oldest member of that queue, 
			otherwise compare data of the two and adopt lower id number """
		if preference == 'Cat':
			return self.Cats.Pop()
		elif preference == 'Dog':
			return self.Dogs.Pop()
		elif preference == None:
			cat_last = self.Cats.Peek()
			dog_last = self.Dogs.Peek()
			if cat_last[-1] < dog_last [-1]:
				return self.Cats.Pop()
			else:
				return self.Dogs.Pop()

		else:
			raise ValueError('Must specify the preferred species of the animal. String: Cat or Dog\n'+\
								'If no preference then no value should be passed.')