Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Repeating Decimals by juestr
def convert(numerator, denominator):
whole, r = divmod(numerator, denominator)
rstack, digits, d = [], [], whole
while True:
if r == 0 or numerator == 0:
return f'{whole}.' + ''.join(digits)
if r in rstack:
period = rstack.index(r)
a = ''.join(digits[:period])
b = ''.join(digits[period:])
return f'{whole}.{a}({b})'
else:
rstack.append(r)
d, r = divmod(r * 10, denominator)
digits.append(str(d))
Aug. 30, 2021
Comments: