-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
150 lines (116 loc) · 4.55 KB
/
main.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import argparse
import input
import output
class Pizza:
PIZZAS = []
SORTED_PIZZAS = []
def __init__(self, pizza_num, nb_ingrediants, list_ingrediants):
self.pizza_num = pizza_num
self.nb_ingrediants = nb_ingrediants
self.list_ingrediants = list_ingrediants
self.delivered = False
def __str__(self):
return "num:{}\nnb_ingrediants:{}\ningrediants{}\n".format(self.pizza_num,
self.nb_ingrediants, self.list_ingrediants)
def __repr__(self):
return str(self)
def __contains__(self, ingrediant):
if ingrediant in self.list_ingrediants:
return True
else:
return False
def __getitem__(self, index):
return self.list_ingrediants[index]
def __iter__(self):
return iter(self.list_ingrediants)
def __next__(self):
return next(self.list_ingrediants)
class Team:
def __init__(self, nb, pizzas):
self.nb = nb
self.pizzas = pizzas
self.index_pizzas = [str(pizza.pizza_num) for pizza in pizzas]
def create_delivery(team_nb):
# we choose the pizza with the most ingrediants
selected_pizzas = [Pizza.PIZZAS[0]]
Pizza.PIZZAS[0].delivered = True
del(Pizza.PIZZAS[0])
# then we'll choose the pizza that add the max different igrediants
for _ in range(1,team_nb):
max_diff = 0
max_pizza = None
for pizza in Pizza.PIZZAS:
if pizza.delivered:
continue
diff = 0
for ingrediant in pizza:
present = False # if the ingrediant is already present in our selected pizzas
for pizza_i in selected_pizzas:
if ingrediant in pizza_i:
present = True # if we find the ingrediant we make the value to true
if not present:
# we increment the number of different ingredients if it's not present in the other pizzas
diff += 1
if diff > max_diff:
max_diff = diff
max_pizza = pizza
elif (diff == max_diff) and (max_pizza == None):
max_diff = diff
max_pizza = pizza
elif (diff == max_diff) and (max_pizza.nb_ingrediants > pizza.nb_ingrediants):
max_diff = diff
max_pizza = pizza
elif max_diff >= pizza.nb_ingrediants:
break
selected_pizzas.append(max_pizza)
max_pizza.delivered = True
del Pizza.PIZZAS[Pizza.PIZZAS.index(max_pizza)]
return Team(team_nb, selected_pizzas)
def build_solution(nb_pizza, nb_teams2, nb_teams3, nb_teams4):
deliveries = []
# sort our list
Pizza.PIZZAS.sort(key=lambda pizza: pizza.nb_ingrediants, reverse=True)
remaining_pizzas = nb_pizza
# we begin to fill the 4 members teams deliveries
for _ in range(nb_teams4):
if remaining_pizzas < 4:
break;
deliveries.append(create_delivery(4))
remaining_pizzas-=4
# then we fill the 3 members teams deliveries
for _ in range(nb_teams3):
if remaining_pizzas < 3:
break;
deliveries.append(create_delivery(3))
remaining_pizzas-=3
# finally the 2 members teams deliveries
for _ in range(nb_teams2):
if remaining_pizzas < 2:
break;
deliveries.append(create_delivery(2))
remaining_pizzas-=2
return deliveries
def parse_arguments():
parser = argparse.ArgumentParser()
parser.add_argument("-f", "--file", help="""name of file can be 'a' or 'b' or 'c' or 'd' or 'e'""")
return parser.parse_args()
def main():
args = parse_arguments()
if args.file not in ('a','b','c','d','e'):
raise ValueError("argument file (-f) must be equal to 'a' or 'b' or 'c' or 'd' or 'e'")
# read the file
content = input.read_file(input.path_files[args.file])
# separate the first line
first_line, content = input.separate_first_line(content)
# extract content from the first line
nb_pizza, nb_teams2, nb_teams3, nb_teams4 = input.first_line_content(first_line)
# separate the folowing lines
lines = input.separate_content(content)
# extract the content from each line
for i in range(nb_pizza):
nb_ingredients, ingrediants = input.line_content(lines[i])
Pizza.PIZZAS.append(Pizza(i, nb_ingredients, ingrediants))
deliveries = build_solution(nb_pizza, nb_teams2, nb_teams3, nb_teams4)
output.write_file(output.outputs_files[args.file], deliveries)
if __name__ == '__main__':
main()