# 3-line python solution using dictionary with explanation

• class Solution(object):

``````def fractionToDecimal(self, n, d):
sign, p_int, point, p_frac, d, n, dic = "-"*(n*d<0), str(abs(n)//abs(d)), "."*(n%d!=0), "", abs(d), abs(n)%abs(d), []
while n not in dic and n!=0: dic, p_frac, n = dic+[n], p_frac+str(n*10//d), n*10%d
return sign+p_int+point+p_frac if n==0 else sign+p_int+point+p_frac[:dic.index(n)]+"("+p_frac[dic.index(n):]+")"
``````

The result is segmented into "sign", "integer part", "point", "fractional part"

First line: initialize all parts, and convert inputs to positive numbers

Second line: update fractional part, with what you learned from elementary school

Third line: combine all parts, find the place to add "()"

• I don't think it is a true 3-line Python solution. I don't think it should be considered as ONE line if you just don't press the enter button after a colon to start a new line.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.