forked from Adam-Jimenez/binarysearch-editorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecimal Number.py
More file actions
35 lines (30 loc) · 1.18 KB
/
Decimal Number.py
File metadata and controls
35 lines (30 loc) · 1.18 KB
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
34
35
"""
Decimal Number
Inspired by alex's solution.
This solution is based on grade-school division. The way we detect recurring digits is if we have a cycle while doing our division. Let's take 1/3 for example:
1 | 3
Since our numerator (1) is smaller than our denominator (3), we shift the decimal place and multiply the numerator by 10.
10 | 3 = 0.
3 can fix 3 times in ten, so we subtract 3*3 from 10 and add 3 to the decimal places:
1 | 3 = 0.3
1 has already been seen, so we are in a cycle. Everything between the last occurrence of 1 and now will be repeated forever.
"""
class Solution:
def solve(self, numerator, denominator):
sign = '-' if numerator * denominator < 0 else ''
numerator = abs(numerator)
denominator = abs(denominator)
intpart, r = divmod(numerator, denominator)
ans = [sign + str(intpart)]
if r: ans.append(".")
seen = {}
t = 0
while r and r not in seen:
seen[r] = t
t += 1
n, r = divmod(10 * r, denominator)
ans.append(str(n))
if r:
ans.insert(seen[r] + 2, '(')
ans.append(')')
return "".join(ans)