class Solution: # @param {integer} numerator # @param {integer} denominator # @return {string} def fractionToDecimal(self, numerator, denominator): negativeFlag = numerator * denominator < 0 numerator = abs(numerator); denominator = abs(denominator) numsList = [];count = 0; dictLoop = {}; loopstr = None while True: numsList.append(str(numerator / denominator)) numerator = 10 * (numerator%denominator) if numerator == 0: break count += 1 loc= dictLoop.get(numerator) if loc: loopstr = ''.join(numsList[loc:count]) break dictLoop[numerator] = count result = numsList[0] if len(numsList) > 1: result += '.' if loopstr: result += ''.join(numsList[1:len(numsList)-len(loopstr)]) + '(' + loopstr + ')' else: result += ''.join(numsList[1:]) if negativeFlag: result = '-' + result return result