Skip to content

Commit b3624f5

Browse files
committed
reduced redundant if statements, made algo more efficient
1 parent b7d96fc commit b3624f5

File tree

1 file changed

+64
-147
lines changed

1 file changed

+64
-147
lines changed

app.py

Lines changed: 64 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from flask import Flask, render_template, request, redirect, url_for
22
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+
56
import csv
67

78
app = Flask(__name__)
@@ -16,31 +17,12 @@ def questionaire():
1617
data = request.form
1718

1819
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
2022
user_education = 0
2123

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-
4324
else:
25+
#In this case, education is considered and calculated
4426
if data.get("q2") == "Yes":
4527
user_education = 8
4628
elif data.get("q2") == "No":
@@ -60,30 +42,25 @@ def convert_questionnaire():
6042
user_education = user_education + (int(data.get("q5")) + int(data.get("q5")) - 1)
6143

6244
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
7550

76-
user_safety += int(data.get("q7"))
51+
user_safety += int(data.get("q6"))
7752

78-
user_safety += int(data.get("q10"))
53+
user_safety += int(data.get("q7"))
7954

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))
8158

8259
return user_safety, user_education
8360

8461

8562
safety, education = convert_questionnaire()
86-
print(safety, education)
63+
8764
letter_convert = {
8865
9:"A%2B",
8966
8:"A",
@@ -108,7 +85,7 @@ def search_results():
10885
state = request.args.get("state")
10986
education = request.args.get("education")
11087
safety = request.args.get("safety")
111-
print(safety)
88+
11289
price_range = request.args.get("price_range")
11390

11491
letter_convert = {
@@ -144,7 +121,7 @@ def search_results():
144121

145122
#Creating empty binary tree
146123
possible_neighbours = []
147-
heapify(possible_neighbours)
124+
heapify_max(possible_neighbours)
148125

149126
#Creating empty final list
150127
display = []
@@ -159,82 +136,38 @@ def __init__(self, name, education, safety, price):
159136
self.price = int(price.replace(",", ""))
160137
self.rank = int(0)
161138

162-
def points(self, input_education, input_safety, input_price):
139+
def calculate_points(self, input_education, input_safety, input_price):
163140
if self.price > input_price:
164141
return None
165142

166143
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))
238171

239172

240173
LA_database = []
@@ -246,50 +179,34 @@ def no_education_points(self, input_safety, input_price):
246179

247180
# Creating function to use method of distric and rank them
248181
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:
261184

262-
return display
185+
i.calculate_points(user_education, user_safety, user_price)
263186

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]
281196

197+
display.append(aux2)
198+
display.sort(key=lambda e: (e[0], e[2]))
199+
200+
return display
282201

283202
results = ranking(LA_database, backend_education, backend_safety, backend_price_range[0])
284203
if results == "No results found":
285204
return "No results found"
286-
print(results)
205+
287206
for result in results:
288-
print("AA",result)
289207
result[2] = f'{result[2]:,}'
290208
result[3] = letter_convert_back[result[3]]
291209
result[4] = letter_convert_back[result[4]]
292210

293-
print(results)
294211

295212
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

Comments
 (0)