diff --git a/currency.py b/currency.py new file mode 100644 index 0000000..698cf9f --- /dev/null +++ b/currency.py @@ -0,0 +1,28 @@ +#!/bin/python + +rates = [("USD", "EUR", 0.86), ("EUR", "JPY", 137.05)] + + +def convert(rates, value, from_a, to_b): + """Convert one currency stright to another using current exchange rate""" + if from_a == to_b: + return value + else: + new_list = [(start, end, rate) + for (start, end, rate) + in rates if from_a == start] + elput = new_list[0] + new_rate = elput[2] + new_value = new_rate * value + return round(new_value, 2) + + +def convert_backwards(rates, value, from_a, to_b): + """Convert one currency to another using the reverse function""" + new_list = [(end, start, rate) + for (end, start, rate) + in rates if to_b == start] + elput = new_list[0] + new_rate = elput[2] + new_value = (value / new_rate) + return round(new_value, 2) diff --git a/test_currency.py b/test_currency.py new file mode 100644 index 0000000..c906a92 --- /dev/null +++ b/test_currency.py @@ -0,0 +1,28 @@ +#!/bin/python +import currency as cy + +rates = [("USD", "EUR", 0.86), ("EUR", "JPY", 137.05)] + + +def test_dollar_for_dollar(): + assert cy.convert(rates, 1, "USD", "USD") == 1 + + +def test_conversion(): + assert cy.convert(rates, 1, "USD", "EUR") == .86 + + +def test_converting_more(): + assert cy.convert(rates, 444, "EUR", "JPY") == 60850.2 + + +def test_convert_backwards(): + assert cy.convert_backwards(rates, 323, "EUR", "USD") == 44267.15 + + +def test_convert_backwards(): + assert cy.convert_backwards(rates, 500, "JPY", "EUR") == 581.4 + + +def test_convert_backwards(): + assert cy.convert_backwards(rates, 2, "JPY", "EUR") == 2.33