Skip to content

Commit

Permalink
166
Browse files Browse the repository at this point in the history
  • Loading branch information
RealHacker committed May 3, 2016
1 parent 4ed2d98 commit baadf4e
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 0 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
51 changes: 51 additions & 0 deletions 166_fraction_to_recurring_decimal/fraction_to_decimal_take2.py
@@ -0,0 +1,51 @@
class Solution(object):
def fractionToDecimal(self, numerator, denominator):
"""
:type numerator: int
:type denominator: int
:rtype: str
"""
if denominator==0:
raise Exception("Div by Zero")
if numerator==0:
return "0"
neg = (numerator<0) and (denominator>0) or (numerator>0) and (denominator<0)

n = abs(numerator)
d = abs(denominator)

if n%d==0:
result = str(n/d)
else:
result = str(n/d)+"."
n = n%d
digits = []
visited = {n:0}
loop_from = -1
while True:
n = n*10
digit = n/d
n = n%d

if n in visited:
digits.append(digit)
loop_from = visited[n]
break
else:
digits.append(digit)
visited[n] = len(digits)

if n==0:
break
print digits
if loop_from<0:
suffix = ''.join([str(d) for d in digits])
else:
first = ''.join([str(d) for d in digits[:loop_from]])
second = "("+''.join([str(d) for d in digits[loop_from:]])+")"
suffix = first+second
result += suffix

if neg:
return "-"+result
return result

0 comments on commit baadf4e

Please sign in to comment.