Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions currency_converter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
def convert(rates, value, a_from, a_to):
"""This function is designed to take in the below arguments and return
the value in the specified currency.

rates = a list of tupleswith each tuple containing a currency code
you can convert from, a currency code you can convert to,
and a rate
value = the amount of money you'd like to exchange for
from = the currency code in the form of a string
to = the currency code you'd like the value in. also a string"""

if a_from == a_to:
return value

for list_from, list_to, list_rate in rates:
if list_from == a_from and list_to == a_to:
return round(value * list_rate, 3)

elif list_from == a_to and list_to == a_from:
return round(value / list_rate, 3)


if __name__ == '__main__':
new_rates = [("JPY", "EUR", .0069),
("EUR", "USD", 1.35)]

print(convert(new_rates, 1, "JPY", "EUR"))
47 changes: 47 additions & 0 deletions dijkstras_algorithm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from collections import namedtuple


inf = float('inf')
Edge = namedtuple('Edge', 'start, end, cost')

class Graph():
def __init__(self, edges):
self.edges = edges2 = [Edge(*edge) for edge in edges]
self.vertices = set(sum(([e.start, e.end] for e in edges2), []))

def dijkstra(self, source, dest):
assert source in self.vertices
dist = {vertex: inf for vertex in self.vertices}
previous = {vertex: None for vertex in self.vertices}
dist[source] = 0
q = self.vertices.copy()
neighbours = {vertex: set() for vertex in self.vertices}
for start, end, cost in self.edges:
neighbours[start].add((end, cost))

while q:
u = min(q, key=lambda vertex: dist[vertex])
q.remove(u)
if dist[u] == inf or u == dest:
break
for v, cost in neighbours[u]:
alt = dist[u] + cost
if alt < dist[v]: # Relax (u,v,a)
dist[v] = alt
previous[v] = u
s, u = [], dest
while previous[u]:
s.insert(0, u)
u = previous[u]
s.insert(0, u)
return s


graph = Graph([("JPY", "EUR", .0069),
("EUR", "USD", 1.35),
])


print(graph.dijkstra("JPY", "USD"))

# Answer is .0092
39 changes: 39 additions & 0 deletions test_currency_converter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from currency_converter import convert


rates = [("USD", "EUR", 0.74)]
value = 1
a_from = 'USD'
a_to = 'EUR'

def test_convert():
assert convert(rates, value, 'USD', 'USD') == 1

def test_simple_conversion():
assert convert(rates, value, a_from, a_to) == .74

def test_different_values():
assert convert(rates, 10, a_from, a_to) == 7.4
assert convert(rates, 50, a_from, a_to) == 37
assert int(convert(rates, 1.5, a_from, a_to)) == 1

def test_reverse_lookup():
assert convert([("EUR", "USD", 0.74)], value, a_from, a_to) == 1.351

def test_multiple_dif_rates():
new_rates = [("USD", "EUR", 0.74), ("EUR", "JPY", 145.949),
("USD", "MXN", 14.64)]

assert convert(new_rates, value, 'EUR', 'JPY') == 145.949
assert convert(new_rates, value, "JPY", "EUR") == .007
assert convert(new_rates, value, "JPY", "EUR") == .007
assert convert(new_rates, 500, "MXN", "USD") == 34.153

def BROKEN_test_values_into_new_rates():
new_rates = [("USD", "EUR", 0.74), ("EUR", "JPY", 145.949)]

USD_to_EUR = convert(new_rates, value, "USD", "EUR")
EUR_to_JPY = convert(new_rates, USD_to_EUR, 'EUR', 'JPY')
JPY_to_USD = convert(new_rates, EUR_to_JPY, 'JPY', 'USD')

assert JPY_to_USD == 1