"""Leetcode 166. Fraction to Recurring Decimal
Medium

URL: https://leetcode.com/problems/fraction-to-recurring-decimal/

Given two integers representing the numerator and denominator of a fraction,
return the fraction in string format.

If the fractional part is repeating, enclose the repeating part in parentheses.

Example 1:
Input: numerator = 1, denominator = 2
Output: "0.5"

Example 2:
Input: numerator = 2, denominator = 1
Output: "2"

Example 3:
Input: numerator = 2, denominator = 3
Output: "0.(6)"
"""

class Solution(object):
    def fractionToDecimal(self, numerator, denominator):
        """
        :type numerator: int
        :type denominator: int
        :rtype: str
        """
        pass


def main():
    pass


if __name__ == '__main__':
    main()

@@ -522,16 +522,37 @@
 Solution
+RemainderPositionDict
 (object)
@@ -706,55 +706,2260 @@
 str%0A
-        %22%22%22%0A        pass%0A%0A%0Adef main():%0A    pass
+%0A        Time complexity: O(denominator), due to mode operator %25.%0A        Space complexity: O(1), since remainder_positions dict only takes 10.%0A        %22%22%22%0A        # Note: To check repeated fractionals, just need to its 1st digit.%0A%0A        if denominator == 0:%0A            return ''%0A%0A        if numerator == 0:%0A            return '0'%0A%0A        res = %5B%5D%0A%0A        # Append sign.%0A        if numerator / denominator %3C 0:%0A            res.append('-')%0A%0A        # Convert to absoulte numerator and denominator.%0A        num, den = abs(numerator), abs(denominator)%0A        divisor, remainder = num // den, num %25 den%0A%0A        res.append(str(divisor))%0A%0A        # Only the integral part.%0A        if remainder == 0:%0A            return ''.join(res)%0A%0A        res.append('.')%0A%0A        # Use dict to collect remainder-%3E1st appearance position.%0A        from collections import defaultdict%0A%0A        remainder_positions = defaultdict(int)%0A%0A        # Iteratively compute divisor & %0A        while remainder != 0:%0A            if remainder in remainder_positions:%0A                # The fractional part starts recurring.%0A                res.insert(remainder_positions%5Bremainder%5D, '(')%0A                res.append(')')%0A                break%0A            else:%0A                remainder_positions%5Bremainder%5D = len(res)%0A%0A                next_num = remainder * 10%0A                divisor, remainder = next_num // den, next_num %25 den%0A                res.append(str(divisor))%0A%0A        return ''.join(res)%0A%0A%0Adef main():%0A    # Output: %220.5%22%0A    numerator = 1%0A    denominator = 2%0A    print SolutionRemainderPositionDict().fractionToDecimal(%0A        numerator, denominator)%0A%0A    # Output: %222%22%0A    numerator = 2%0A    denominator = 1%0A    print SolutionRemainderPositionDict().fractionToDecimal(%0A        numerator, denominator)%0A%0A    # Output: %220.(6)%22%0A    numerator = 2%0A    denominator = 3%0A    print SolutionRemainderPositionDict().fractionToDecimal(%0A        numerator, denominator)%0A%0A    # Output: %220.(571428)%22%0A    numerator = 4%0A    denominator = 7%0A    print SolutionRemainderPositionDict().fractionToDecimal(%0A        numerator, denominator)%0A%0A    # Output: %221.1(6)%22%0A    numerator = 7%0A    denominator = 6%0A    print SolutionRemainderPositionDict().fractionToDecimal(%0A        numerator, denominator)
 %0A%0A%0Ai

Complete remainder pos sol