Skip to content

Commit 85f1401

Browse files
committed
Fix: TSP rename lambda parameter and add type hints
1 parent e39c3ce commit 85f1401

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

graphs/tests/test_traveling_salesman_problem.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ def sample_graph_2() -> list[list[int]]:
1616
[20, 25, 30, 0],
1717
]
1818

19-
def test_brute_force():
19+
def test_brute_force() -> None:
2020
graph = sample_graph_1()
2121
assert tsp_brute_force(graph) == 64
2222

23-
def test_dp():
23+
def test_dp() -> None:
2424
graph = sample_graph_1()
2525
assert tsp_dp(graph) == 64
2626

27-
def test_greedy():
27+
def test_greedy() -> None:
2828
graph = sample_graph_1()
2929
# The greedy algorithm does not guarantee an optimal solution;
3030
# it is necessary to verify that its output is an integer greater than 0.
@@ -33,15 +33,15 @@ def test_greedy():
3333
assert isinstance(result, int)
3434
assert result >= 64
3535

36-
def test_dp_larger_graph():
36+
def test_dp_larger_graph() -> None:
3737
graph = sample_graph_2()
3838
assert tsp_dp(graph) == 80
3939

40-
def test_brute_force_larger_graph():
40+
def test_brute_force_larger_graph() -> None:
4141
graph = sample_graph_2()
4242
assert tsp_brute_force(graph) == 80
4343

44-
def test_greedy_larger_graph():
44+
def test_greedy_larger_graph() -> None:
4545
graph = sample_graph_2()
4646
result = tsp_greedy(graph)
4747
assert isinstance(result, int)

graphs/traveling_salesman_problem.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def tsp_greedy(graph: list[list[int]]) -> int:
102102
# Find the nearest city to the current location that has not been visited.
103103
next_city = min(
104104
((city, cost) for city, cost in enumerate(graph[current]) if not visited[city] and city != current),
105-
key=lambda x: x[1],
105+
key=lambda cost: cost[1],
106106
default=(None, float('inf'))
107107
)[0]
108108

@@ -125,7 +125,7 @@ def tsp_greedy(graph: list[list[int]]) -> int:
125125
return total_cost
126126

127127

128-
def test_tsp_example():
128+
def test_tsp_example() -> None:
129129
graph = [
130130
[0, 10, 15, 20],
131131
[10, 0, 35, 25],

0 commit comments

Comments
 (0)