1
+ // Runtime : 69 ms (Top 5.88 % ) | Memory : 14 MB (Top 23.53 % )
1
2
class Solution :
2
3
# inspired from:
3
4
# https://coolconversion.com/math/recurring-decimals-as-a-fraction/
@@ -9,16 +10,16 @@ def isRationalEqual(self, s: str, t: str) -> bool:
9
10
# intuition:
10
11
# write each numbes as fraction: num / den
11
12
# then compare the two fractions.
12
-
13
+
13
14
num1 , den1 = self .toFraction (s )
14
15
num2 , den2 = self .toFraction (t )
15
-
16
+
16
17
return den1 * num2 == den2 * num1
17
-
18
+
18
19
def toFraction (self , s : str ) -> typing .Tuple [int , int ]:
19
20
if "." not in s :
20
21
return int (s ), 1
21
-
22
+
22
23
intp , frac = s .split ("." )
23
24
# decimal dot, but no repeating part:
24
25
# xyz.abc = xyzabc / 1000
@@ -27,22 +28,22 @@ def toFraction(self, s: str) -> typing.Tuple[int, int]:
27
28
num = int (intp ) * (10 ** len (frac )) + ifrac
28
29
den = 10 ** len (frac )
29
30
return num , den
30
-
31
+
31
32
# this is for cases like a.b(c)
32
- # let n = a.b(c)
33
+ # let n = a.b(c)
33
34
# then, 10^(len(b+c)) * n = abc.(c)
34
- # and 10^(len(b)) * n = ab.(c)
35
+ # and 10^(len(b)) * n = ab.(c)
35
36
# subtract the two, and solve for n:
36
37
# n = (abc - ab) / (10^len(b + c) - 10^len(b))
37
38
frac , repfrac = frac .split ("(" )
38
39
repfrac = repfrac [:- 1 ]
39
-
40
+
40
41
iintp = int (intp )
41
42
ifrac = int (frac ) if len (frac ) > 0 else 0
42
43
irep = int (repfrac )
43
-
44
+
44
45
return (
45
46
(iintp * (10 ** (len (frac + repfrac ))) + ifrac * 10 ** len (repfrac ) + irep ) - (iintp * 10 ** len (frac ) + ifrac ),
46
47
(10 ** len (frac + repfrac ) - 10 ** len (frac ))
47
48
)
48
- `` `
49
+ `` `
0 commit comments