Commit 0b695d3a authored by Roberts Ivanovs's avatar Roberts Ivanovs
Browse files

Added some documentation to the code

parent 420e7fa0
......@@ -3,6 +3,30 @@ from data_validation import generate_matrix
def mans_cels(city_1, city_2, matrix, cities, block_tuple_list=None):
"""
The dijkstra's algorithm implementation for finding the route between cities
Parameters
----------
city_1 : String
[description]
city_2 : String
[description]
matrix : A list of lists containing integers
A matrix representation for the distances between cities
cities : a list of strings
City names that can be set as labels for the matrix variable
block_tuple_list : An iterable containing 2 city
names and the distance between them, optional
eg. `("Ventspils", "Talsi", inf)`, by default None.
This is meant for overriding initial data presented by the matrix var.
Returns
-------
list
The route from the starting city to the end city as a list of strings
containing city names
"""
print(f"Finding the route between {city_1} and {city_2}")
if block_tuple_list is not None:
......@@ -11,7 +35,7 @@ def mans_cels(city_1, city_2, matrix, cities, block_tuple_list=None):
block_2 = blockage[1] # City to block to
distance = blockage[2] # Distance to use for "blocking"
print(
f"Changing the distance from {block_1} to {block_2} with ",
f"Changing the distance from {block_1} to {block_2} with",
f"distance {distance}"
)
block_1_idx = cities.index(block_1)
......@@ -50,6 +74,8 @@ def mans_cels(city_1, city_2, matrix, cities, block_tuple_list=None):
curr_smallest_len = sum_dist[i_node]
curr_smallest_id = i_node
# The following part of the script was inspitred by Verners Šmits from
# my course
# If we're at the end node, trace back the graph
if visited[end_node]:
route = [cities[end_node]]
......@@ -74,6 +100,21 @@ def mans_cels(city_1, city_2, matrix, cities, block_tuple_list=None):
curr_node = curr_smallest_id
def user_friendly_route_formatter(route):
"""Return a route formatted for the end user
Parameters
----------
route : list
A list containing cities (strings) to format into a route string
Returns
-------
string
A string formatted for the end user to see
"""
return "road: "+str(" uz ".join(route))
if __name__ == "__main__":
matrix_data = generate_matrix()
cities_string = ", ".join(matrix_data['cities'])
......@@ -81,10 +122,12 @@ if __name__ == "__main__":
route = mans_cels(
"Ventspils", "Riga", matrix_data["matrix"], matrix_data["cities"]
)
print(route, '\n')
print(user_friendly_route_formatter(route))
print('\n\n')
block_tuple_list = [
("Ventspils", "Talsi", inf),
("Talsi", "Ventspils", inf)]
("Talsi", "Ventspils", inf)
]
route = mans_cels(
"Ventspils",
"Riga",
......@@ -92,4 +135,4 @@ if __name__ == "__main__":
matrix_data["cities"],
block_tuple_list=block_tuple_list
)
print("road: ", " uz ".join(route))
print(user_friendly_route_formatter(route))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment