Skip to content

Commit b6660d6

Browse files
committed
Runtime: 69 ms (Top 5.88%) | Memory: 14 MB (Top 23.53%)
1 parent 5f145da commit b6660d6

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Runtime: 69 ms (Top 5.88%) | Memory: 14 MB (Top 23.53%)
12
class Solution:
23
# inspired from:
34
# https://coolconversion.com/math/recurring-decimals-as-a-fraction/
@@ -9,16 +10,16 @@ def isRationalEqual(self, s: str, t: str) -> bool:
910
# intuition:
1011
# write each numbes as fraction: num / den
1112
# then compare the two fractions.
12-
13+
1314
num1, den1 = self.toFraction(s)
1415
num2, den2 = self.toFraction(t)
15-
16+
1617
return den1 * num2 == den2 * num1
17-
18+
1819
def toFraction(self, s: str) -> typing.Tuple[int, int]:
1920
if "." not in s:
2021
return int(s), 1
21-
22+
2223
intp, frac = s.split(".")
2324
# decimal dot, but no repeating part:
2425
# xyz.abc = xyzabc / 1000
@@ -27,22 +28,22 @@ def toFraction(self, s: str) -> typing.Tuple[int, int]:
2728
num = int(intp) * (10 ** len(frac)) + ifrac
2829
den = 10 ** len(frac)
2930
return num, den
30-
31+
3132
# this is for cases like a.b(c)
32-
# let n = a.b(c)
33+
# let n = a.b(c)
3334
# then, 10^(len(b+c)) * n = abc.(c)
34-
# and 10^(len(b)) * n = ab.(c)
35+
# and 10^(len(b)) * n = ab.(c)
3536
# subtract the two, and solve for n:
3637
# n = (abc - ab) / (10^len(b + c) - 10^len(b))
3738
frac, repfrac = frac.split("(")
3839
repfrac = repfrac[:-1]
39-
40+
4041
iintp = int(intp)
4142
ifrac = int(frac) if len(frac) > 0 else 0
4243
irep = int(repfrac)
43-
44+
4445
return (
4546
(iintp * (10 ** (len(frac + repfrac))) + ifrac * 10 ** len(repfrac) + irep) - (iintp * 10 ** len(frac) + ifrac),
4647
(10** len(frac+repfrac) - 10 **len(frac))
4748
)
48-
```
49+
```

0 commit comments

Comments
 (0)