Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for The Cheapest Flight by mohamedsleymi2016
from typing import List
def base(l,a,b):
res=[]
for k in l:
next_l=l
if ((k[0]==a) and (k[1]==b)):
res+=[k[2]]
elif ((k[0]==a) and (k[1]!=b)):
next_l.remove(k)
data = base(next_l,k[1],b)
if (data!=0):
res+=[k[2]+data]
elif ((k[1]==a) and (k[0]!=b)):
ff= [i for i in l if i[0]==k[0] and i[1]==b]
if ff!=[]:
x,y,z=ff[0]
res+=[k[2]+z]
else:
pass
res = [i for i in res if i != 0]
if res==[]:
return 0
else:
return min(res)
def cheapest_flight(costs: List, a: str, b: str) -> int:
a,b=sorted([a,b])
l=[]
for x in costs:
i,j,k=x
i,j=sorted([i,j])
l+=[[i,j,k]]
return base(l,a,b)
if __name__ == '__main__':
print("Example:")
'''
print(cheapest_flight([['A', 'C', 100],
['A', 'B', 20],
['B', 'C', 50]],
'A',
'C'))
# These "asserts" are used for self-checking and not for an auto-testing
assert cheapest_flight([['A', 'C', 100],
['A', 'B', 20],
['B', 'C', 50]],
'A',
'C') == 70
print('1'*48)
assert cheapest_flight([['A', 'C', 100],
['A', 'B', 20],
['B', 'C', 50]],
'C',
'A') == 70
print('2'*48)
'''
assert cheapest_flight([['A', 'C', 40],
['A', 'B', 20],
['A', 'D', 20],
['B', 'C', 50],
['D', 'C', 70]],
'D',
'C') == 60
print('3'*48)
assert cheapest_flight([['A', 'C', 100],
['A', 'B', 20],
['D', 'F', 900]],
'A',
'F') == 0
assert cheapest_flight([['A', 'B', 10],
['A', 'C', 15],
['B', 'D', 15],
['C', 'D', 10]],
'A',
'D') == 25
assert cheapest_flight([["A","C",100],["A","B",20],["B","C",50]],"A","C") == 70
print("Coding complete? Click 'Check' to earn cool rewards!")
Dec. 28, 2020