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
Binary file added .DS_Store
Binary file not shown.
8 changes: 8 additions & 0 deletions currency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

def convert(rates, value, origin, to):

for countries in rates:
if countries[0] == origin and countries[1] == to:
return countries[2]*value
elif countries[0] == to and countries[1] == origin:
return round(1/countries[2]*value, 2)
38 changes: 38 additions & 0 deletions test_currency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

from currency import convert


def test_convert():

assert convert([("USD", "USD", 1)], 75, "USD", "USD") == 75
assert convert([("USD", "EUR", 0.74)], 1, "USD", "EUR") == .74
assert convert([("USD", "EUR", 0.74)], .5, "USD", "EUR") == .37


def test_convert_with_reverse_rate():

assert convert([("USD", "EUR", 0.74)], 1, "EUR", "USD") == 1.35


def test_convert_with_two_rates():

assert convert([("USD", "EUR", 0.74), ("EUR", "JPY", 145.949)],
1, "EUR", "JPY") == 145.949

assert convert([("USD", "EUR", 0.74), ("EUR", "JPY", 145.949)],
1, "JPY", "EUR") == .01

assert convert([("USD", "EUR", 0.74), ("EUR", "JPY", 145.949)],
1, "USD", "EUR") == .74

assert convert([("USD", "EUR", 0.74), ("EUR", "JPY", 145.949)],
1, "EUR", "USD") == 1.35


def test_convert_with_more_than_two_rates():

assert convert([("USD", "EUR", 0.74), ("EUR", "JPY", 145.949),
("USD", "AUD", 1.22)], 1, "USD", "AUD") == 1.22

assert convert([("USD", "EUR", 0.74), ("EUR", "JPY", 145.949),
("USD", "AUD", 1.22)], 1, "AUD", "USD") == .82