Correct answer not accepted as correct
For building AI course chapter 2: Optimization
I wrote the code for the Pineapple route emissions problem (intermediate). The code gives exactly same output as given by the sample solution. Not sure why this doesnt work.
My solution
def routeFinder(route, ports, D, co2):
if len(ports) < 1:
if set(route) == set(range(5)):
distance = D[route[0]][route[1]] + D[route[1]][route[2]] + D[route[2]][route[3]] + D[route[3]][route[4]]
emissions = distance * co2
print(' '.join([portnames[i] for i in route]) + " %.1f kg" % emissions)
else:
for i in range(len(ports)):
routeFinder(route + [ports[i]], ports[:i] + ports[i+1:], D, co2)
def main():
portnames = ["PAN", "AMS", "CAS", "NYC", "HEL"]
# https://sea-distances.org/
# nautical miles converted to km
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]
]
# https://timeforchange.org/co2-emissions-shipping-goods
# assume 20g per km per metric ton (of pineapples)
co2 = 0.020
route = [0, 1, 2, 3, 4]
for i in route:
routeFinder([route[i]], list(range(1, len(route))), D, co2)
main()Sample solution
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]
if 0 in route and 1 in route and 2 in route and 3 in route and 4 in route:
distance = D[route[0]][route[1]] + D[route[1]][route[2]] + \
D[route[2]][route[3]] + D[route[3]][route[4]]
emissions = distance * co2
print(' '.join([portnames[i] for i in route]) + " %.1f kg" % emissions)
Can anyone help finding the difference and reason why isnt my solution correct. Thanks folks !