mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-22 03:00:14 +00:00
ae0fc85401
* Fix ruff errors Renamed neural_network/input_data.py to neural_network/input_data.py_tf because it should be left out of the directory for the following reasons: 1. Its sole purpose is to be used by neural_network/gan.py_tf, which is itself left out of the directory because of issues with TensorFlow. 2. It was taken directly from TensorFlow's codebase and is actually already deprecated. If/when neural_network/gan.py_tf is eventually re-added back to the directory, its implementation should be changed to not use neural_network/input_data.py anyway. * updating DIRECTORY.md * Change input_data.py_tf file extension Change input_data.py_tf file extension because algorithms-keeper bot is being picky about it --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
49 lines
1.6 KiB
Python
49 lines
1.6 KiB
Python
def decimal_to_fraction(decimal: 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(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') = }")
|