forked from sunilchauhan/Cheapest-Restaurants
-
Notifications
You must be signed in to change notification settings - Fork 0
/
restaurant.py
272 lines (235 loc) · 11.4 KB
/
restaurant.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
'''
Program helps to find out cheapest rate of restaurant in city for the provided input items
'''
class Restaurants(object):
def __init__(self):
self.price = 0.0
self.current_price = None
self.sublist_price = 0.0
self.total_price = 0.0
self.final_price = 0.0
#These three variables are useful for final output
self.feasible_solution = {}
self.optimal_solution = False
self.price_output = 0.0
'''
Create database from csv file.
CAUTION: Make sure csv file is in current directory
'''
def create_database(self, filename):
database = defaultdict(dict)
with open(os.path.join(os.curdir, filename), 'rb') as f:
reader = csv.reader(f, delimiter=',')
for row in reader:
record = row[0].split(',')
if record[0] not in database:
database[record[0]] = {}
'''
converting all items to tuple so can use items as dict key and price as
value
'''
items = tuple([ i.strip() for i in record[2:]])
record[1] = record[1].strip()
database[record[0]][items] = record[1]
return database
def check_price(self, menu_dict=None, restaurant_id=None, input_data=None):
self.best_price = None
backup = list(input_data)
while backup :
if not sorted(list(set(input_data))) == sorted(list(input_data)): #if input contains repeated item
if not backup.count(backup[0]) == len(backup): #if list not containing same repeated element
backup_input = set(backup)
'''
backup_input set should not be more than max size of item combo.
If it is, then remove individual costly item to reduce set length to max item combo size
In our case max combo size width is 3
'''
if len(list(set(backup_input)))>3:
remove_element = self.excess_input(menu_dict, set(backup_input))
backup_input = list(set(backup_input))
backup_input.remove(remove_element[0])
self.final_price = self.best_option(menu_dict, set(backup_input))
for item in set(backup_input):
backup.remove(item)
self.total_price = float(self.total_price) + float(self.final_price)
else:
'''
Here remaining list contains same element multiple times.
'''
count = backup.count(backup[0])
self.final_price = self.best_option(menu_dict, set(backup))
self.final_price = float(self.final_price)*float(count)
self.total_price = float(self.total_price) + float(self.final_price)
break
else:
'''
If input contains distinct item. No need to do extra processing. Just call best_option()
to get optimum price
'''
self.total_price = 0.0
self.final_price = self.best_option(menu_dict, backup)
self.total_price = self.total_price + float(self.final_price)
break
return self.total_price
'''
Method responsible for finding best price for given input
'''
def best_option(self, menu_dict, input_data):
self.best_price = None #Initialise best_price for current set of input
if tuple(sorted(list((input_data)))) in menu_dict: #if exact input as key in menu_dict
self.price = menu_dict[tuple(sorted(list((input_data))))]
if not self.best_price:
self.best_price = float(self.price)
else:
if float(self.best_price) > float(self.price):
self.best_price = float(self.price)
if len(list(set(input_data))) > 1:
for i in range(1, (len(menu_dict.keys())+1)):
'''
Apply brute force approch to calculate every possible combination of keys in menu_dict
'''
total_combination = list(itertools.combinations(menu_dict, i))
for groups in total_combination:
current_members = set().union(*groups) # Finding unique element from the group through union
if sorted(list(input_data)) == sorted(list(current_members)): #If all input exist in union of groups
'''
Since this group has all the input element. Calculate its cost and compare with best_price
'''
self.current_price = sum(float(menu_dict[i]) for i in groups)
if not self.best_price:
self.best_price = float(self.current_price)
else:
if float(self.best_price) > float(self.current_price):
self.best_price = float(self.current_price)
else:
'''
Now, since the Union of group element didn't exactly equal to input
(May be input has some extra element which is not in current group of combo)..
Therefore, find if our input is sublist of union of current group.
If it is, then calculate the cost
'''
sublist = self.is_sublist(list(input_data), list(current_members))
if sublist:
for g in groups:
self.sublist_price = float(self.sublist_price) + float(menu_dict[g])
if not self.best_price:
self.best_price = float(self.sublist_price)
else:
if float(self.best_price) > float(self.sublist_price):
self.best_price = float(self.sublist_price)
else:
'''
In case, input is single element
'''
key = (input_data.pop(),)
if key in menu_dict:
return menu_dict[key]
return self.best_price
'''
Method returns flag to indicate input_data is sublist of current combo items
'''
def is_sublist(self, input_list=None, currrent_group=None):
flag = True
for i in input_list:
if i not in currrent_group:
flag = False
return flag
'''
Method responsible for filtering max cost individual element from
set in case set has more element, than maximum item combo size.
'''
def excess_input(self, menu_dict=None, input_data=None):
total_combination = list(itertools.combinations(menu_dict, 2))
result_list = []
value = None
for groups in total_combination:
current_members = set().union(*groups)
largest_value = menu_dict[max(groups, key=len)]
if sorted(list(input_data)) == sorted(list(current_members)): #If all input exist in union of groups
if len(max(groups, key=len)) == 3 and len(min(groups, key=len)) == 1:
if not value:
value = largest_value
element = min(groups, key=len)
else:
if float(value) > float(largest_value):
value = float(largest_value)
element = min(groups, key=len)
return element
'''
This method is only useful for helping to perform unittesting.
'''
def initialise_test1(self,filename,input_data):
#call create_database to initialise restaurants data from csv file
database = self.create_database(filename)
for restaurant_id, items_dict in database.iteritems():
process_data = database[restaurant_id]
result = self.check_price(process_data, restaurant_id, input_data)
self.feasible_solution.update({restaurant_id:result})
if self.feasible_solution:
self.optimal_solution = min(self.feasible_solution, key=self.feasible_solution.get)
self.price_output = self.feasible_solution[self.optimal_solution]
return (self.optimal_solution, self.price_output)
import unittest
class InputTests(unittest.TestCase):
def test_input1(self):
input_data = ('i1',)
obj1 = Restaurants()
output = obj1.initialise_test1('input.csv', input_data)
self.assertEqual(output, ('1',1.0))
def test_input2(self):
input_data = ('i2',)
obj2 = Restaurants()
output = obj2.initialise_test1('input.csv', input_data)
self.assertEqual(output, ('2',1.9))
def test_input3(self):
input_data = ('i2','i3')
obj3 = Restaurants()
output = obj3.initialise_test1('input.csv', input_data)
self.assertEqual(output, ('1',4.0))
def test_input4(self):
input_data = ('i1','i4')
obj4 = Restaurants()
output = obj4.initialise_test1('input.csv', input_data)
self.assertEqual(output, ('1',5.0))
def test_input5(self):
input_data = ('i2','i4')
obj5 = Restaurants()
output = obj5.initialise_test1('input.csv', input_data)
self.assertEqual(output, ('2',5.9))
def test_input6(self):
input_data = ('i3','i4')
obj6 = Restaurants()
output = obj6.initialise_test1('input.csv', input_data)
self.assertEqual(output, ('1',6.5))
def test_input7(self):
input_data = ('i2','i3','i4')
obj7 = Restaurants()
output = obj7.initialise_test1('input.csv', input_data)
self.assertEqual(output, ('1',8))
def test_input8(self):
input_data = ('i1','i2','i3','i4')
obj8 = Restaurants()
output = obj8.initialise_test1('input.csv', input_data)
self.assertEqual(output, ('2',8.45))
def test_input9(self):
input_data = ('i1', 'i2', 'i2', 'i2', 'i3', 'i3', 'i3')
obj9 = Restaurants()
output = obj9.initialise_test1('input.csv', input_data)
self.assertEqual(output, ('1',12.5))
def test_input10(self):
input_data = ('i1', 'i1', 'i1', 'i1', 'i1', 'i2', 'i3', 'i3', 'i3', 'i4', 'i4')
obj10 = Restaurants()
output = obj10.initialise_test1('input.csv', input_data)
self.assertEqual(output, ('1',19.5))
def test_input11(self):
input_data = ('i1', 'i1', 'i1', 'i3', 'i4', 'i1', 'i3', 'i4', 'i4', 'i3', 'i4', 'i4', 'i4', 'i2', 'i3', 'i2', 'i3', 'i1', 'i2', 'i3', 'i1', 'i2', 'i3', 'i2', 'i2')
obj11 = Restaurants()
output = obj11.initialise_test1('input.csv', input_data)
self.assertEqual(output, ('1',54))
if __name__ == "__main__":
import csv
from collections import defaultdict
import itertools
import os
suite = unittest.TestLoader().loadTestsFromTestCase(InputTests)
unittest.TextTestRunner(verbosity=2).run(suite)