All problems
Fraction to Recurring Decimal
Problem
Given numerator and denominator, return the fraction as a decimal string, wrapping any repeating part in parentheses, such as 0.(3) for one third.
Handle the sign, emit the integer part, then simulate long division while remembering where each remainder was first seen; a repeated remainder marks the start of the cycle. Return 0 immediately when the numerator is 0.
Hint
A remainder that repeats means the digits from its first appearance repeat forever.
Test cases
case 1
in: [1,2]
expected: "0.5"
case 2
in: [2,1]
expected: "2"
case 3
in: [4,333]
expected: "0.(012)"
case 4
in: [1,3]
expected: "0.(3)"
case 5
in: [-50,8]
expected: "-6.25"
case 6
in: [1,6]
expected: "0.1(6)"
Starter codePython
def fraction_to_decimal(numerator, denominator):
# Your code here
pass6 test cases · run your code against them in the editor