-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path592.py
48 lines (38 loc) · 1.51 KB
/
592.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
__________________________________________________________________________________________________
sample 28 ms submission
class Solution:
def fractionAddition(self, expression: str) -> str:
def toInt(x):
x = x.split('/')
return [int(x[0]), int(x[1])]
exp = expression.replace('-', '+-')
exp = [x for x in exp.split('+') if x]
exp = list(map(lambda x: toInt(x), exp))
while len(exp) > 1:
num1, den1 = exp.pop(0)
num2, den2 = exp.pop(0)
lcm = den1 * den2 // math.gcd(den1, den2)
num1 *= lcm // den1
num2 *= lcm // den2
exp.append([num1 + num2, lcm])
num, den = exp[0]
if num == 0:
return '0/1'
else:
gcd = math.gcd(num, den)
return '{}/{}'.format(num//gcd, den//gcd)
__________________________________________________________________________________________________
sample 32 ms submission
class Solution:
def fractionAddition(self, expression):
ints = map(int, re.findall('[+-]?\d+', expression))
A, B = 0, 1
for a in ints:
b = next(ints)
A = A * b + a * B
B *= b
g = math.gcd(A, B)
A //= g
B //= g
return '%d/%d' % (A, B)
__________________________________________________________________________________________________