-
Notifications
You must be signed in to change notification settings - Fork 0
/
homer.py
120 lines (101 loc) · 3.14 KB
/
homer.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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
class Food:
def __init__(self, name, expiration):
self.name = name
self.expiration = expiration
def openFridge(fridge):
print("Following items are in Homer's fridge:")
for food in fridge:
print("{0} (expires in: {1} days)".format(
str(food.name), str(food.expiration))
)
print("")
# Task 1
def maxExpirationDay(fridge):
if len(fridge) == 0:
return -1
else:
maxExDay = 0
for food in fridge:
if food.expiration > maxExDay:
maxExDay = food.expiration
return maxExDay
# Task 2
def histogramOfExpirations(fridge):
histogram = []
if len(fridge) == 0:
return fridge
maxDay = maxExpirationDay(fridge)
for j in range(maxDay + 1):
histogram.append(0)
for i in range(maxDay + 1):
for food in fridge:
if i == food.expiration:
histogram[i] += 1
return histogram
# Task 3
def cumulativeSum(histogram):
newHistogram = []
sum = 0
for i in range(len(histogram)):
sum += histogram[i]
newHistogram.append(sum)
return newHistogram
# Task 4
def sortFoodInFridge(fridge):
histogram = cumulativeSum(histogramOfExpirations(fridge))
newFridge = []
prevExpdate = 0
for j in range(len(fridge)):
newFridge.append(0)
for i in range(len(fridge)):
prevExpdate = fridge[i].expiration
histogram[prevExpdate] -= 1
poslnd = histogram[prevExpdate]
newFridge[poslnd] = fridge[i]
return newFridge
# Task 5
def reverseFridge(fridge):
newFridge = []
for i in range(len(fridge) - 1, -1, -1):
newFridge.append(fridge[i])
return newFridge
# Task 6
def eatFood(name, fridge):
newFridge = []
for i in fridge:
newFridge.append(i)
names = []
prevFood = 0
prevExpday = 800
if len(newFridge) == 0:
return newFridge
for food in fridge:
if food.name == name:
names.append(food)
if len(names) == 0:
return newFridge
else:
for j in names:
if j.name == name and j.expiration <= prevExpday:
prevFood = j
prevExpday = j.expiration
newFridge.remove(prevFood)
return newFridge
# print(openFridge(eatFood("donut0", [])))
# print(openFridge(eatFood("donut0",
# [Food("donut9", 36), Food("donut9", 34), Food("donut0", 9),
# Food("donut4", 35), Food("donut2", 26), Food("donut0", 45),
# Food("donut6", 20), Food("donut8", 49), Food("donut2", 29),
# Food("donut9", 8), Food("donut8", 41), Food("donut2", 39),
# Food("donut8", 46)])))
# openFridge(eatFood("donut",
# [Food("donut9", 36), Food("donut9", 34), Food("donut0", 9),
# Food("donut4", 35), Food("donut2", 26), Food("donut0", 45),
# Food("donut6", 20), Food("donut8", 49), Food("donut2", 29),
# Food("donut9", 8), Food("donut8", 41), Food("donut2", 39),
# Food("donut8", 46)]))
# openFridge(
# eatFood("donut",
# [Food("beer", 4), Food("steak", 1), Food("hamburger", 1),
# Food("donut", 3), Food("donut", 1), Food("donut", 6)]
# ))