Skip to content

Commit 258d58a

Browse files
authored
Update traveling_salesman.py
fixed the linter
1 parent 348d616 commit 258d58a

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

src/python/traveling_salesman.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from itertools import permutations
22

3+
34
def calculate_total_distance(path, distances):
45
"""
56
Calculate the total distance of a path through cities.
@@ -22,6 +23,7 @@ def calculate_total_distance(path, distances):
2223
total_distance += distances[path[-1]][path[0]]
2324
return total_distance
2425

26+
2527
def traveling_salesman_bruteforce(distances):
2628
"""
2729
Find the shortest path through cities using brute force.
@@ -40,7 +42,7 @@ def traveling_salesman_bruteforce(distances):
4042
num_cities = len(distances)
4143
all_cities = list(range(num_cities))
4244
shortest_path = None
43-
shortest_distance = float('inf')
45+
shortest_distance = float("inf")
4446

4547
for path in permutations(all_cities):
4648
distance = calculate_total_distance(path, distances)
@@ -50,6 +52,7 @@ def traveling_salesman_bruteforce(distances):
5052

5153
return shortest_path, shortest_distance
5254

55+
5356
def create_distance_matrix(num_cities):
5457
"""
5558
Create a distance matrix for a given number of cities.
@@ -71,13 +74,16 @@ def create_distance_matrix(num_cities):
7174
if i == j:
7275
row.append(0)
7376
elif j > i:
74-
distance = int(input(f"Enter distance between City {i + 1} and City {j + 1}: "))
77+
distance = int(
78+
input(f"Enter distance between City {i + 1} and City {j + 1}: ")
79+
)
7580
row.append(distance)
7681
else:
7782
row.append(distances[j][i])
7883
distances.append(row)
7984
return distances
8085

86+
8187
def print_distance_matrix(distances):
8288
"""
8389
Print the distance matrix.
@@ -98,6 +104,7 @@ def print_distance_matrix(distances):
98104
for row in distances:
99105
print(row)
100106

107+
101108
def print_city_path(path):
102109
"""
103110
Print the shortest path through cities.
@@ -112,6 +119,7 @@ def print_city_path(path):
112119
city_path = [f"City {city + 1}" for city in path]
113120
print("Shortest Path:", " -> ".join(city_path))
114121

122+
115123
def main_menu():
116124
"""
117125
Display a menu for the traveling salesman problem and handle user input.
@@ -129,22 +137,25 @@ def main_menu():
129137

130138
choice = input("Enter your choice: ")
131139

132-
if choice == '1':
140+
if choice == "1":
133141
num_cities = int(input("Enter the number of cities: "))
134142
distances = create_distance_matrix(num_cities)
135143
print_distance_matrix(distances)
136-
elif choice == '2':
144+
elif choice == "2":
137145
if 'distances' in locals():
138-
shortest_path, shortest_distance = traveling_salesman_bruteforce(distances)
146+
shortest_path, shortest_distance = traveling_salesman_bruteforce(
147+
distances
148+
)
139149
print_city_path(shortest_path)
140150
print("Shortest Distance:", shortest_distance)
141151
else:
142152
print("Please create a distance matrix first.")
143-
elif choice == '3':
153+
elif choice == "3":
144154
print("Goodbye!")
145155
break
146156
else:
147157
print("Invalid choice. Please select a valid option.")
148158

159+
149160
if __name__ == "__main__":
150161
main_menu()

0 commit comments

Comments
 (0)