From 18a49d48587b3b6dcaa5949cbef3f6022ebfdc5a Mon Sep 17 00:00:00 2001 From: grizax Date: Wed, 21 Jan 2015 00:38:19 -0500 Subject: [PATCH 1/4] Conversion created with basic tests run thru --- currency.py | 17 +++++++++++++++++ test_currency.py | 10 ++++++++++ 2 files changed, 27 insertions(+) create mode 100644 currency.py create mode 100644 test_currency.py diff --git a/currency.py b/currency.py new file mode 100644 index 0000000..3298f43 --- /dev/null +++ b/currency.py @@ -0,0 +1,17 @@ +#!/bin/python + +rates = [("USD", "EUR", 0.86), ("EUR", "JPY", 136.28)] + + +def convert(rates, value, from_a, to_b): + """Convert one currency to another using the 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 new_value diff --git a/test_currency.py b/test_currency.py new file mode 100644 index 0000000..a34bbd4 --- /dev/null +++ b/test_currency.py @@ -0,0 +1,10 @@ +#!/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 From dfd49c8b98346675a9b91b9c0a60071f8c2820ec Mon Sep 17 00:00:00 2001 From: grizax Date: Wed, 21 Jan 2015 00:56:57 -0500 Subject: [PATCH 2/4] Add another test converting more complicated conversions --- currency.py | 4 ++-- test_currency.py | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/currency.py b/currency.py index 3298f43..5692886 100644 --- a/currency.py +++ b/currency.py @@ -1,6 +1,6 @@ #!/bin/python -rates = [("USD", "EUR", 0.86), ("EUR", "JPY", 136.28)] +rates = [("USD", "EUR", 0.86), ("EUR", "JPY", 137.05)] def convert(rates, value, from_a, to_b): @@ -14,4 +14,4 @@ def convert(rates, value, from_a, to_b): elput = new_list[0] new_rate = elput[2] new_value = new_rate * value - return new_value + return round(new_value, 2) diff --git a/test_currency.py b/test_currency.py index a34bbd4..f254c8f 100644 --- a/test_currency.py +++ b/test_currency.py @@ -3,8 +3,14 @@ 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 From a2418b018a623dd2a67c9102018214643ce77562 Mon Sep 17 00:00:00 2001 From: grizax Date: Wed, 21 Jan 2015 01:25:39 -0500 Subject: [PATCH 3/4] Added the backwards convert --- currency.py | 13 ++++++++++++- test_currency.py | 8 ++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/currency.py b/currency.py index 5692886..698cf9f 100644 --- a/currency.py +++ b/currency.py @@ -4,7 +4,7 @@ def convert(rates, value, from_a, to_b): - """Convert one currency to another using the current exchange rate""" + """Convert one currency stright to another using current exchange rate""" if from_a == to_b: return value else: @@ -15,3 +15,14 @@ def convert(rates, value, from_a, to_b): 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 index f254c8f..1f2ae30 100644 --- a/test_currency.py +++ b/test_currency.py @@ -14,3 +14,11 @@ def test_conversion(): 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 From cf58adb7f412691aff694b1e481363f920c6236d Mon Sep 17 00:00:00 2001 From: grizax Date: Wed, 21 Jan 2015 01:32:23 -0500 Subject: [PATCH 4/4] Fixed pep8 and added another test testing convert function --- test_currency.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test_currency.py b/test_currency.py index 1f2ae30..c906a92 100644 --- a/test_currency.py +++ b/test_currency.py @@ -22,3 +22,7 @@ def test_convert_backwards(): 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