Format a recurring decimal
Convert integer numerator divided by nonzero integer denominator to an exact decimal string. Enclose the recurring part in parentheses, with no redundant trailing decimal point.
remainder, divisor = 1, 6
digit, remainder = divmod(remainder*10, divisor)
print(digit, remainder)Reason about the state
Long division revisits a remainder exactly when digits begin repeating. Store the first output position of every remainder and handle the sign separately. Before writing code, identify what each variable means after one iteration. Walk through the example and explain why the next transition preserves that meaning. Then check the smallest permitted input and a case with repeated values. A correct answer follows the contract for every input, including the boundary cases in the checks. Keep any input mutation within the stated rules; return a fresh result when the task asks for one.
Your task
Convert integer numerator divided by nonzero integer denominator to an exact decimal string. Enclose the recurring part in parentheses, with no redundant trailing decimal point.
The explanation opens here once every check passes.