Trains the technique from
LeetCode 166Fraction to Recurring DecimalThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A reporting tool has to print the ratio numerator / denominator in ordinary decimal notation and return it as a string. Follow this format exactly.
- appears when and only when the true value is below zero. A value of zero is printed as 0, never as -0.2.., then the digits after the point. A value between -1 and 1 has a whole part of 0, as in 0.75 or -0.75.( and ) and stop there. The ( goes at the earliest digit from which the repeat runs unbroken to the end, and the block inside the brackets must be as short as possible. So one ninth is 0.(1), one sixth is 0.1(6), and one forty-fourth is 0.02(27).0.875.Return that string.
Example 1
Seven eighths is 0.875 and the digits after the point stop, so no brackets appear.
Example 2
The digits after the point run 1, 1, 3, 6, 3, 6, 3, 6 and onward. The repeat is the pair 36 and the earliest place it starts running unbroken is the third digit, so the first two digits sit outside the brackets.
Example 3
The value is below zero, so the string opens with a minus sign, and nine quarters is 2.25.
Example 4
Eight divided by four comes out whole, so only the digits of the whole number are printed and there is no decimal point.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def fraction_to_decimal(numerator: int, denominator: int) -> str:public String fractionToDecimal(int numerator, int denominator)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.