-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm166.py
33 lines (24 loc) · 1.05 KB
/
m166.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution:
def fractionToDecimal(self, numerator: int, denominator: int) -> str:
pastDivs = dict()
outputs = []
if numerator == 0 :
return '0'
if ((numerator < 0) ^ (denominator < 0)) :
outputs.append('-')
numerator, denominator = abs(numerator), abs(denominator)
outputs.append(str(numerator // denominator))
numerator = (numerator % denominator) * 10
if numerator == 0 :
return ''.join(outputs)
outputs.append('.')
# Note: denominator will never chance ==> dict can be based on numerator
while not numerator in pastDivs.keys() :
if numerator == 0 :
return ''.join(outputs)
pastDivs[numerator] = len(outputs)
outputs.append(str(numerator // denominator))
numerator = (numerator % denominator) * 10
outputs.append(')')
return ''.join(outputs[0:pastDivs.get(numerator)]) + '(' + \
''.join(outputs[pastDivs.get(numerator):])