-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmodules.py
43 lines (40 loc) · 1.48 KB
/
modules.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
def showResult(previous, start, goal):
curCity = goal # Truy vết từ điểm đích về điểm bắt đầu
print('Result:', curCity, end=' ')
while curCity != start:
curCity = previous[curCity]
print('->', curCity, end=' ')
def showResultWithAttr(previous, start, goal, attr='total_cost'):
curCity = goal # Truy vết từ điểm đích về điểm bắt đầu
print('Result:', (curCity, previous[curCity][attr]), end=' ')
while curCity != start:
curCity = previous[curCity]['from']
print('->', (curCity, previous[curCity][attr]), end=' ')
def showStep(counter, q, previous, attr='total_cost'):
print('%d. {' % counter, end=' ')
i = 0
lenQ = len(q)
for v in q:
i += 1
if (i < lenQ):
print((v, previous[v][attr], previous[v]['from']), end=', ')
else:
print((v, previous[v][attr], previous[v]['from']), end=' ')
print('}')
def aweSomeSort(array, previous, sortBy='total_cost'): # QuickSort (python version)
less = []
equal = []
greater = []
if len(array) > 1:
pivot = previous[array[0]][sortBy]
for city in array:
cost = previous[city][sortBy]
if cost < pivot:
less.append(city)
if cost == pivot:
equal.append(city)
if cost > pivot:
greater.append(city)
return aweSomeSort(less, previous, sortBy) + equal + aweSomeSort(greater, previous, sortBy) # toán tử nối mảng
else:
return array