Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save realrajapaksha/2dc2cbe132dd51a2f3994d4c20550912 to your computer and use it in GitHub Desktop.
Save realrajapaksha/2dc2cbe132dd51a2f3994d4c20550912 to your computer and use it in GitHub Desktop.
# pre define the all paths
dictionary1 = {"start":"SL", "distance":9.25, "end":"AUS"}
dictionary2 = {"start":"SIN", "distance":7.25, "end":"AUS"}
dictionary3 = {"start":"SL", "distance":4.0, "end":"SIN"}
dictionary4 = {"start":"SL", "distance":8.0, "end":"JP"}
dictionary5 = {"start":"JP", "distance":10.0, "end":"AUS"}
dictionary6 = {"start":"SL", "distance":11.45, "end":"UK"}
dictionary7 = {"start":"SIN", "distance":4.0, "end":"JP"}
dictionary8 = {"start":"UK", "distance":8.0, "end":"USA"}
dictionary9 = {"start":"JP", "distance":16.0, "end":"USA"}
# all paths set as the list
list = [dictionary1, dictionary2, dictionary3, dictionary4, dictionary5, dictionary6, dictionary7, dictionary8, dictionary9]
startInput = "" # user input
endInput = "" # user input
route = [] # all suitable paths
def findRoute(start, end, path=[], distance= 0):
path = path + [start]
if start == end:
# store the path and total distance of single path
res = {"path": path , "distance": distance}
route.append(res)
for node in list:
if node["start"] == start:
if node["end"] not in path:
findRoute(node["end"], end, path, distance+node["distance"])
def printAllRoute() :
# print all routes
for x in route :
for p in x["path"]:
print(p + " -> ",end=" ")
print("Expected Duration "+str(x["distance"]) + "Hours" ,end=" ")
print()
def printMinRoute() :
# get seperate distance value from all paths
distanceList = []
for x in route :
distanceList.append(x["distance"])
# get minimum duration index from list
min_index = min(range(len(distanceList)), key=distanceList.__getitem__)
# get minimun route by index
minRoute = route[min_index]
# print minimum route
print(" ----- Shorted Duration ----")
for p in minRoute["path"]:
print(p + " -> ",end=" ")
print("Expected Duration "+str(minRoute["distance"]) + "Hours", end = " ")
print()
def getUserInputs() :
global startInput
global endInput
# get user start
startInput = input("Enter Start : ")
# get user destination
endInput = input("Enter End : ")
# method calls
# user inputs
getUserInputs()
# find the paths
findRoute(startInput, endInput)
# get all paths
printAllRoute()
# get minimum path
printMinRoute()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment