Exercise 2 Adv: Solution not working
My code gives me exactly one solution in the required format but I get the error notification:
“Test Failed: AssertionError: True is not false : Too many route combinations. You only need to print the route with the minimum emmisions!”
Even though my answer comes out as:
“PAN NYC CAS AMS HEL 283.7 kg”
So I do not get why it is saying that there are too many combinations.
My code:
portnames = ["PAN", "AMS", "CAS", "NYC", "HEL"]
D = [
[0,8943,8019,3652,10545],
[8943,0,2619,6317,2078],
[8019,2619,0,5836,4939],
[3652,6317,5836,0,7825],
[10545,2078,4939,7825,0]
]
all=[]
rou = []
co2 = 0.020
port1 = 0
for port2 in range(1, 5):
for port3 in range(1, 5):
for port4 in range(1, 5):
for port5 in range(1, 5):
route = [port1, port2, port3, port4, port5]
distance = D[route[0]][route[1]] + D[route[1]][route[2]] + D[route[2]][route[3]] + D[route[3]][route[4]]
emissions = distance * co2
if route[1] != route[2] and route[1] != route[3] and route[1] != route[4] and route[2] != route[3] and route[2] != route[4] and route[3] != route[4]:
all.append(emissions)
rou.append(route)
mini=all.index(min(all))
print(' '.join([portnames[i] for i in rou[mini]])+" "+"%.1f kg"%all[mini])Update:
I get, that my code is only working for a specific length, but the Error message doesn’t make sense.