1
1
from flask import Flask , render_template , request , redirect , url_for
2
2
import random
3
- from heapq import heappop , heappush , heapify
4
- #heapq_max
3
+ #from heapq import heappop, heappush, heapify
4
+ from heapq_max import heappop_max , heappush_max , heapify_max
5
+
5
6
import csv
6
7
7
8
app = Flask (__name__ )
@@ -16,31 +17,12 @@ def questionaire():
16
17
data = request .form
17
18
18
19
def convert_questionnaire ():
19
- if data .get ("q1" ) == "No" :
20
+ if data .get ("q1" ) == "No" or data .get ("q3" ) == "N/A" or data .get ("q4" ) == "N/A" or data .get ("q5" ) == "N/A" :
21
+ #In this case, education is not considered
20
22
user_education = 0
21
23
22
- if data .get ("q8" ) == "Yes" :
23
- user_safety = 5
24
- elif data .get ("q8" ) == "No" :
25
- user_safety = 2
26
-
27
-
28
- if data .get ("q9" ) == "Yes" :
29
- user_safety += 2
30
- elif data .get ("9" ) == "No" :
31
- user_safety += 4
32
-
33
- user_safety += int (data .get ("q6" ))
34
-
35
- user_safety += int (data .get ("q7" ))
36
-
37
- user_safety += int (data .get ("q10" ))
38
-
39
- user_safety = int (round (user_safety / 25 * 5 ,0 ))
40
-
41
- return user_safety , user_education
42
-
43
24
else :
25
+ #In this case, education is considered and calculated
44
26
if data .get ("q2" ) == "Yes" :
45
27
user_education = 8
46
28
elif data .get ("q2" ) == "No" :
@@ -60,30 +42,25 @@ def convert_questionnaire():
60
42
user_education = user_education + (int (data .get ("q5" )) + int (data .get ("q5" )) - 1 )
61
43
62
44
user_education = int (round (user_education / 34 * 9 ,0 ))
63
-
64
- if data .get ("q8" ) == "Yes" :
65
- user_safety = 5
66
- elif data .get ("q8" ) == "No" :
67
- user_safety = 2
68
-
69
- if data .get ("q9" ) == "Yes" :
70
- user_safety += 2
71
- elif data .get ("q9" ) == "No" :
72
- user_safety += 4
73
-
74
- user_safety += int (data .get ("q6" ))
45
+
46
+ #Calculating safety
47
+ user_safety = 5 if data .get ("q8" ) == "Yes" else 2
48
+
49
+ user_safety += 2 if data .get ("q9" ) == "Yes" else 4
75
50
76
- user_safety += int (data .get ("q7 " ))
51
+ user_safety += int (data .get ("q6 " ))
77
52
78
- user_safety += int (data .get ("q10 " ))
53
+ user_safety += int (data .get ("q7 " ))
79
54
80
- user_safety = int (round (user_safety / 25 * 5 ,0 ))
55
+ user_safety += int (data .get ("q10" ))
56
+
57
+ user_safety = int (round (user_safety / 25 * 5 ,0 ))
81
58
82
59
return user_safety , user_education
83
60
84
61
85
62
safety , education = convert_questionnaire ()
86
- print ( safety , education )
63
+
87
64
letter_convert = {
88
65
9 :"A%2B" ,
89
66
8 :"A" ,
@@ -108,7 +85,7 @@ def search_results():
108
85
state = request .args .get ("state" )
109
86
education = request .args .get ("education" )
110
87
safety = request .args .get ("safety" )
111
- print ( safety )
88
+
112
89
price_range = request .args .get ("price_range" )
113
90
114
91
letter_convert = {
@@ -144,7 +121,7 @@ def search_results():
144
121
145
122
#Creating empty binary tree
146
123
possible_neighbours = []
147
- heapify (possible_neighbours )
124
+ heapify_max (possible_neighbours )
148
125
149
126
#Creating empty final list
150
127
display = []
@@ -159,82 +136,38 @@ def __init__(self, name, education, safety, price):
159
136
self .price = int (price .replace ("," , "" ))
160
137
self .rank = int (0 )
161
138
162
- def points (self , input_education , input_safety , input_price ):
139
+ def calculate_points (self , input_education , input_safety , input_price ):
163
140
if self .price > input_price :
164
141
return None
165
142
166
143
else :
167
- if self .education == input_education :
168
- self .rank += 2
169
-
170
- if self .education == (input_education - 1 ):
171
- self .rank += 1
172
-
173
- if self .education == (input_education + 1 ):
174
- self .rank += 3
175
-
176
- if self .education == (input_education + 2 ) or self .education == (input_education + 3 ) or self .education == (input_education + 4 ) or self .education == (input_education + 5 ) or self .education == (input_education + 6 ):
177
- self .rank += 4
178
-
179
- if self .education == (input_education - 2 ):
180
- self .rank = self .rank
181
-
182
- if self .education == (input_education - 3 ) or self .education == (input_education - 4 ):
183
- self .rank -= 1
184
-
185
- if self .education == (input_education - 5 ) or self .education == (input_education - 6 ):
186
- self .rank -= 2
187
-
188
- if self .safety == input_safety :
189
- self .rank += 2
190
-
191
- if self .safety == (input_safety - 1 ):
192
- self .rank += 1
193
-
194
- if self .safety == (input_safety + 1 ):
195
- self .rank += 3
196
-
197
- if self .safety == (input_safety + 2 ) or self .safety == (input_safety + 3 ) or self .safety == (input_safety + 4 ) or self .safety == (input_safety + 5 ) or self .safety == (input_safety + 6 ):
198
- self .rank += 4
199
-
200
- if self .safety == (input_safety - 2 ):
201
- self .rank = self .rank
202
-
203
- if self .safety == (input_safety - 3 ) or self .safety == (input_safety - 4 ):
204
- self .rank -= 1
205
-
206
- if self .safety == (input_safety - 5 ) or self .safety == (input_safety - 6 ):
207
- self .rank -= 2
208
-
209
- heappush (possible_neighbours , (- 1 * self .rank , self .name ))
210
-
211
- def no_education_points (self , input_safety , input_price ):
212
- if self .price > input_price :
213
- return None
214
-
215
- else :
216
- if self .safety == input_safety :
217
- self .rank += 2
218
-
219
- if self .safety == (input_safety - 1 ):
220
- self .rank += 1
221
-
222
- if self .safety == (input_safety + 1 ):
223
- self .rank += 3
224
-
225
- if self .safety == (input_safety + 2 ) or self .safety == (input_safety + 3 ) or self .safety == (input_safety + 4 ) or self .safety == (input_safety + 5 ) or self .safety == (input_safety + 6 ):
226
- self .rank += 4
227
-
228
- if self .safety == (input_safety - 2 ):
229
- self .rank = self .rank
230
-
231
- if self .safety == (input_safety - 3 ) or self .safety == (input_safety - 4 ):
232
- self .rank -= 1
233
-
234
- if self .safety == (input_safety - 5 ) or self .safety == (input_safety - 6 ):
235
- self .rank -= 2
236
-
237
- heappush (possible_neighbours , (- 1 * self .rank , self .name ))
144
+ difference_to_rank = {
145
+ 0 : 2 ,
146
+ - 1 :1 ,
147
+ 1 :3 ,
148
+ 2 :4 ,
149
+ 3 :4 ,
150
+ 4 :4 ,
151
+ 5 :4 ,
152
+ 6 :4 ,
153
+ - 2 :0 ,
154
+ - 3 :- 1 ,
155
+ - 4 :- 1 ,
156
+ - 5 :- 2 ,
157
+ - 6 :- 2
158
+ }
159
+ if input_education != 0 :
160
+ #If education level is 0, skip this part
161
+
162
+ #Difference between actual education & input
163
+ difference = self .education - input_education
164
+ self .rank += difference_to_rank [difference ]
165
+
166
+ #Difference between actual safety & input
167
+ difference = self .safety - input_safety
168
+ self .rank += difference_to_rank [difference ]
169
+
170
+ heappush_max (possible_neighbours , (self .rank , self .name ))
238
171
239
172
240
173
LA_database = []
@@ -246,50 +179,34 @@ def no_education_points(self, input_safety, input_price):
246
179
247
180
# Creating function to use method of distric and rank them
248
181
def ranking (database , user_education , user_safety , user_price ):
249
- if user_education == 0 :
250
- for i in database :
251
- i .no_education_points (user_safety , user_price )
252
-
253
- while len (display ) != 5 :
254
- aux = heappop (possible_neighbours )
255
- for i in LA_database :
256
- if i .name == aux [1 ]:
257
- aux2 = [aux [0 ], aux [1 ], i .price , i .safety , i .education ]
258
-
259
- display .append (aux2 )
260
- display .sort (key = lambda e : (e [0 ], e [2 ]))
182
+
183
+ for i in database :
261
184
262
- return display
185
+ i . calculate_points ( user_education , user_safety , user_price )
263
186
264
- else :
265
- for i in database :
266
- i .points (user_education , user_safety , user_price )
267
-
268
- while len (display ) != 5 :
269
- try :
270
- aux = heappop (possible_neighbours )
271
- except :
272
- return "No results found"
273
- for i in LA_database :
274
- if i .name == aux [1 ]:
275
- aux2 = [aux [0 ], aux [1 ], i .price , i .safety , i .education ]
276
-
277
- display .append (aux2 )
278
- display .sort (key = lambda e : (e [0 ], e [2 ]))
279
-
280
- return display
187
+ while len (display ) != 5 :
188
+ try :
189
+ aux = heappop_max (possible_neighbours )
190
+ except :
191
+ return "No results found"
192
+
193
+ for i in LA_database :
194
+ if i .name == aux [1 ]:
195
+ aux2 = [aux [0 ], aux [1 ], i .price , i .safety , i .education ]
281
196
197
+ display .append (aux2 )
198
+ display .sort (key = lambda e : (e [0 ], e [2 ]))
199
+
200
+ return display
282
201
283
202
results = ranking (LA_database , backend_education , backend_safety , backend_price_range [0 ])
284
203
if results == "No results found" :
285
204
return "No results found"
286
- print ( results )
205
+
287
206
for result in results :
288
- print ("AA" ,result )
289
207
result [2 ] = f'{ result [2 ]:,} '
290
208
result [3 ] = letter_convert_back [result [3 ]]
291
209
result [4 ] = letter_convert_back [result [4 ]]
292
210
293
- print (results )
294
211
295
212
return render_template ('results.html' , state = state , education = education , safety = safety , price_range = price_range , random_num = random .randint (9 ,60 ), results = results )
0 commit comments