Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Second solution in Clear category for Repeating Decimals by dagger126
def convert(numerator, denominator):
result, stack = [], []
result.append(str(numerator//denominator))
result.append('.')
i = numerator % denominator
while i not in stack:
stack.append(i)
numerator = 10*i
result.append(str(numerator//denominator))
i = numerator % denominator
n = stack.index(i)
result.insert(n+2, '(')
result.append(')')
return ''.join(result).replace('(0)', '')
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert convert(1, 3) == "0.(3)", "1/3 Classic"
assert convert(5, 3) == "1.(6)", "5/3 The same, but bigger"
assert convert(3, 8) == "0.375", "3/8 without repeating part"
assert convert(7, 11) == "0.(63)", "7/11 prime/prime"
assert convert(29, 12) == "2.41(6)", "29/12 not and repeating part"
assert convert(11, 7) == "1.(571428)", "11/7 six digits"
assert convert(0, 117) == "0.", "Zero"
July 27, 2014