← Back to challenges

Decimal to Rational

PythonHardmathnumbersregex

Instructions

Find rational representation of a decimal number. The input number is given as string in the form whole.between(period). The output should be a tuple numerator, denominator such that gcd(numerator, denominator) == 1.

Examples

find_fraction("0.4") ➞ (2, 5)

find_fraction("0.1(6)") ➞ (1, 6)

find_fraction("0.(3)") ➞ (1, 3)

find_fraction("0.(142857)") ➞ (1, 7)

find_fraction("0.(012987)") ➞ (1, 77)

Notes

  • The length of a periodic fraction can be larger than 20 digits.
  • The function should be efficient.
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.