From 1cf80761b3f078c10a9ca485b18e8f9711333dab Mon Sep 17 00:00:00 2001 From: Aaryan Raj <97806283+iaaryanraj@users.noreply.github.com> Date: Mon, 31 Oct 2022 00:23:06 +0530 Subject: [PATCH 1/2] Added algorithm to convert decimal number to its simplest fraction form --- maths/decimal_to_fraction.py | 48 ++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 maths/decimal_to_fraction.py diff --git a/maths/decimal_to_fraction.py b/maths/decimal_to_fraction.py new file mode 100644 index 000000000000..cb11468cebb2 --- /dev/null +++ b/maths/decimal_to_fraction.py @@ -0,0 +1,48 @@ +def decimal_to_fraction(decimal: int | float | str) -> tuple[int, int]: + """ + Return a decimal number in its simplest fraction form + >>> decimal_to_fraction(2) + (2, 1) + >>> decimal_to_fraction(89.) + (89, 1) + >>> decimal_to_fraction("67") + (67, 1) + >>> decimal_to_fraction("45.0") + (45, 1) + >>> decimal_to_fraction(1.5) + (3, 2) + >>> decimal_to_fraction("6.25") + (25, 4) + >>> decimal_to_fraction("78td") + Traceback (most recent call last): + ValueError: Please enter a valid number + """ + try: + decimal = float(decimal) + except ValueError: + raise ValueError("Please enter a valid number") + fractional_part = decimal - int(decimal) + if fractional_part == 0: + return int(decimal), 1 + else: + number_of_frac_digits = len(str(decimal).split(".")[1]) + numerator = int(decimal * (10**number_of_frac_digits)) + denominator = 10**number_of_frac_digits + divisor, dividend = denominator, numerator + while True: + remainder = dividend % divisor + if remainder == 0: + break + dividend, divisor = divisor, remainder + numerator, denominator = numerator / divisor, denominator / divisor + return int(numerator), int(denominator) + + +if __name__ == "__main__": + print(decimal_to_fraction(2)) + print(decimal_to_fraction(89.0)) + print(decimal_to_fraction("67")) + print(decimal_to_fraction("45.0")) + print(decimal_to_fraction(1.5)) + print(decimal_to_fraction("6.25")) + print(decimal_to_fraction("78td")) From b39bfe11899a8633959b09ad9c9f019f4bf8aa90 Mon Sep 17 00:00:00 2001 From: Aaryan Raj <97806283+iaaryanraj@users.noreply.github.com> Date: Wed, 30 Nov 2022 23:42:53 +0530 Subject: [PATCH 2/2] Apply suggested changes --- maths/decimal_to_fraction.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/maths/decimal_to_fraction.py b/maths/decimal_to_fraction.py index cb11468cebb2..9462bafe0171 100644 --- a/maths/decimal_to_fraction.py +++ b/maths/decimal_to_fraction.py @@ -39,10 +39,10 @@ def decimal_to_fraction(decimal: int | float | str) -> tuple[int, int]: if __name__ == "__main__": - print(decimal_to_fraction(2)) - print(decimal_to_fraction(89.0)) - print(decimal_to_fraction("67")) - print(decimal_to_fraction("45.0")) - print(decimal_to_fraction(1.5)) - print(decimal_to_fraction("6.25")) - print(decimal_to_fraction("78td")) + print(f"{decimal_to_fraction(2) = }") + print(f"{decimal_to_fraction(89.0) = }") + print(f"{decimal_to_fraction('67') = }") + print(f"{decimal_to_fraction('45.0') = }") + print(f"{decimal_to_fraction(1.5) = }") + print(f"{decimal_to_fraction('6.25') = }") + print(f"{decimal_to_fraction('78td') = }")