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
9 changes: 9 additions & 0 deletions README_Ana.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
currency_converter.py should be called with no arguments.

convert function takes a list of tuples of the form (curr1,curr2,rate)
where each tuple has 3 elements:
currency1
currency2
the exchange rate to convert from currency1 to currency2

and 3 more arguments: currency one, currency 2 and the value to be converted
20 changes: 20 additions & 0 deletions currency_converter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

def get_exchange_rate(rates, cu_from, cu_to):
exch_tuple = [conv_element
for conv_element in rates
if cu_from in conv_element and cu_to in conv_element]
if exch_tuple == []:
return 0
else:
if cu_from == exch_tuple[0][0]:
return exch_tuple[0][2]
else:
return (1/exch_tuple[0][2])


def convert(rates, value, cu_from, cu_to):
if cu_from == cu_to:
return value
else:
exch_converter = get_exchange_rate(rates, cu_from, cu_to)
return exch_converter*value
22 changes: 22 additions & 0 deletions test_currency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from currency_converter import *


rates = [("USD", "EUR", 0.74), ("EUR", "JPY", 145.949)]


def test_get_exchange_rate():
assert get_exchange_rate(rates, "USD", "EUR") == 0.74
assert get_exchange_rate(rates, "EUR", "JPY") == 145.949
assert get_exchange_rate(rates, "USD", "JPY") == 0
assert get_exchange_rate(rates, "JPY", "EUR") == 1 / 145.949
assert get_exchange_rate(rates, "EUR", "USD") == 1 / 0.74


def test_convert():
assert convert(rates, 1, "USD", "USD") == 1
assert convert(rates, 1, "USD", "EUR") == 0.74
assert convert(rates, 1, "POU", "EUR") == 0
assert convert(rates, 2, "USD", "EUR") == 1.48
assert convert(rates, 2, "EUR", "JPY") == 291.898
assert convert(rates, 2, "EUR", "USD") == 2 * 1 / 0.74
assert convert(rates, 2, "JPY", "EUR") == 2 * 1 / 145.949