fork(3) download
  1. def convert(numerator, denominator):
  2. integer_part = str(numerator // denominator)
  3. ans = ''
  4. l = dict()
  5. i = 0
  6.  
  7. numerator = numerator % denominator
  8. l[numerator] = i
  9. i += 1
  10.  
  11. if numerator==0:
  12. return integer_part
  13.  
  14. while True:
  15. if numerator==0:
  16. return '{}.{}'.format(integer_part, ans)
  17.  
  18. digit, numerator = divmod(numerator*10, denominator)
  19. ans += str(digit)
  20. if numerator not in l:
  21. l[numerator] = i
  22. i += 1
  23. else:
  24. return '{}.{}({})'.format(integer_part, ans[:l[numerator]], ans[l[numerator]:])
  25.  
  26. assert convert(1230, 10) == '123', convert(1230, 10)
  27. assert convert(123, 100) == '1.23', convert(123, 100)
  28. assert convert(10, 12) == '0.8(3)', convert(10, 12)
  29. assert convert(100, 12) == '8.(3)', convert(100, 12)
  30. assert convert(1000, 12) == '83.(3)', convert(1000, 12)
  31. assert convert(10, 13) == '0.(769230)', convert(10, 13)
  32.  
  33. print('OK')
Success #stdin #stdout 0.01s 27696KB
stdin
Standard input is empty
stdout
OK