Skip to content

Instantly share code, notes, and snippets.

@robbdimitrov
Last active April 27, 2022 09:22
Show Gist options
  • Save robbdimitrov/62a91c631f3ef38be35b236531a9983c to your computer and use it in GitHub Desktop.
Save robbdimitrov/62a91c631f3ef38be35b236531a9983c to your computer and use it in GitHub Desktop.
Problems
def convert(graph, start, end, path=None):
if path is None:
path = []
if start == end:
return 1
for child in graph:
if child[0] == start and child[0] not in path:
result = convert(graph, child[1], end, path + [child[0]])
if result != 0:
return child[2] * result
elif child[1] == start and child[1] not in path:
result = convert(graph, child[0], end, path + [child[1]])
if result != 0:
return (1 / child[2]) * result
return 0
if __name__ == '__main__':
currencies = [
('BGN', 'EUR', 0.51),
('EUR', 'GBP', 0.87),
('USD', 'GBP', 0.72),
('CAD', 'BGN', 1.30),
]
print(convert(currencies, 'BGN', 'USD')) # 0.61625
print(convert(currencies, 'GBP', 'BGN')) # 2.25377
print(convert(currencies, 'USD', 'CAD')) # 1.24824
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment