-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeal.py
More file actions
79 lines (65 loc) · 2.4 KB
/
meal.py
File metadata and controls
79 lines (65 loc) · 2.4 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import json
from food import Fruit, Meat
class Meal:
"""Collection of foods that can be prepped and eaten by a group."""
def __init__(self, foods):
"""Create a meal from a list of foods."""
self.foods = list(foods)
def prep(self):
"""Prepare foods by washing/ripening fruit and cooking meat."""
for food in self.foods:
if isinstance(food, Fruit):
if not food.is_washed:
food.wash()
if not food.is_ripe:
food.ripen()
continue
if isinstance(food, Meat):
if not food.is_edible:
food.cook()
continue
if not food.is_edible:
print(f"No prep method for {food.name}.")
def eat(self, persons):
"""Feed foods to people using a round-robin strategy."""
persons = list(persons)
if not persons:
print("No persons to eat the meal.")
return
original_fullness = {person: person.is_full for person in persons}
ate_any = {person: False for person in persons}
for person in persons:
person.is_full = False
start_index = 0
for food in self.foods:
if food.eaten:
continue
ate = False
for offset in range(len(persons)):
person = persons[(start_index + offset) % len(persons)]
before = food.eaten
food.eat(person)
if not before and food.eaten:
ate = True
ate_any[person] = True
person.is_full = False
start_index = (start_index + offset + 1) % len(persons)
break
if not ate:
print(f"No one could eat {food.name}.")
for person in persons:
if ate_any[person]:
person.is_full = True
else:
person.is_full = original_fullness[person]
def to_dict(self):
"""Return a dictionary snapshot of the meal state."""
return {
"type": self.__class__.__name__,
"foods": [food.to_dict() for food in self.foods],
}
def to_json(self):
"""Print and return a JSON snapshot of the meal state."""
data = self.to_dict()
print(json.dumps(data))
return data