-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapter5_test.py
More file actions
80 lines (61 loc) · 1.98 KB
/
Copy pathchapter5_test.py
File metadata and controls
80 lines (61 loc) · 1.98 KB
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
from chapter5 import *
import pytest
def test_min_num_paper_money():
change = [1, 2, 5, 10, 20, 50, 100]
total = 17
print('\n纸币面额有:', change)
print('共{}元,至少需要{}张纸币'.format(total, min_num_paper_money(change, total)))
def test_fibonacci_array():
n = 10
print('\n斐波那契的前{}个元素为:'.format(n), fibonacci_array(n))
def test_tsp_solution():
g = [[999, 3, 6, 7],
[5, 999, 2, 3],
[6, 4, 999, 2],
[3, 7, 5, 999]]
res = tsp_solution(g)
print('\n最短路径:', res)
def test_min_route():
g = [[0, 4, 2, 3, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 9, 8, 0, 0, 0, 0],
[0, 0, 0, 0, 6, 7, 8, 0, 0, 0],
[0, 0, 0, 0, 0, 4, 7, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 5, 6, 0],
[0, 0, 0, 0, 0, 0, 0, 8, 6, 0],
[0, 0, 0, 0, 0, 0, 0, 6, 5, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 7],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 3],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
min_cost, path = min_route(g)
for i in range(len(g)):
print('\n到顶点{}的最小开销为{}'.format(i, min_cost[i]), '路径:', i, end='')
pre = i
while path[pre]:
print(' <-', path[pre], end='')
pre = path[pre]
def test_package_problem():
w = [2, 2, 6, 5, 4]
v = [6, 3, 5, 4, 9]
c = 10
print('\n', package_problem(w, v, c))
def test_max_common_arr():
x = ['a', 'b', 'c', 'b', 'd', 'b']
y = ['a', 'c', 'b', 'b', 'a', 'b', 'd', 'b', 'b']
print('\n最长公共子序列长度为:', max_common_arr(x, y))
def test_optimal_bst():
p = [0.1, 0.2, 0.4, 0.3]
# print(optimal_bst(p))
def test_asm():
p = 'happy'
t = 'have a hsppy day'
k = 1
res, dp = asm(p, t, k)
print()
print(res)
for i in dp:
for j in i:
print(j, '\t', end='')
print()
if __name__ == '__main__':
# 运行该文件中的所有测试函数
pytest.main(['chapter5_test.py', '-s'])