diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..01b9af2 Binary files /dev/null and b/.DS_Store differ diff --git a/currency.py b/currency.py new file mode 100644 index 0000000..8e42e36 --- /dev/null +++ b/currency.py @@ -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) diff --git a/test_currency.py b/test_currency.py new file mode 100644 index 0000000..4064792 --- /dev/null +++ b/test_currency.py @@ -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