From 4198c6f733d093531af80af501b669c85227383b Mon Sep 17 00:00:00 2001 From: fdai7764 Date: Fri, 19 Jan 2024 15:24:49 +0100 Subject: [PATCH] added a test for conversion from number to fraction as well as corresponding functionality --- src/main/py/fractions.py | 3 +++ src/test/py/test_fractions.py | 3 +++ 2 files changed, 6 insertions(+) diff --git a/src/main/py/fractions.py b/src/main/py/fractions.py index 2470caf..ce1c287 100644 --- a/src/main/py/fractions.py +++ b/src/main/py/fractions.py @@ -3,6 +3,9 @@ from src.main.py.more_advanced_calculations import gcd, lcm def frac_to_dec(num, denom): return num/denom +def dec_to_frac(dec_number): + return dec_number.as_integer_ratio() + def reduceFraction(num, denom): gcdOfFraction = gcd(num, denom) num /= gcdOfFraction diff --git a/src/test/py/test_fractions.py b/src/test/py/test_fractions.py index b776a68..9613789 100644 --- a/src/test/py/test_fractions.py +++ b/src/test/py/test_fractions.py @@ -15,6 +15,9 @@ class fractions(unittest.TestCase): def test_dec_value_of_negative_20_by_5_should_be_negative_4(self): self.assertEqual(frac_to_dec(-20,5), -4) + def test_dec_value_to_fraction_point_two_five_should_be_1_by_4(self): + self.assertEqual(dec_to_frac(0.25), (1,4)) + def test_reduce_fraction_56_by_32_should_be_7_by_4(self): self.assertEqual(reduceFraction(56,32), (7,4))