blob: f7cb8d12a9c56d32a18dcd2cd9203f0f9847e904 (
plain)
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
|
# 592. Fraction Addition and Subtraction
from re import findall
from math import gcd
"""
given a string 'expression' representing an expression of fraction addition
and subtraction, return the calculation result in string format. the final
result should an irreducible fraction. if your final result is an integer,
change it to the format of a fraction has a denominator 1. so in this case, 2
should be converted to "2/1"
"""
class Solution(object):
def fractionAddition(self, expression):
"""
:type expression: str
:rtype: str
"""
nums = map(int, findall("[+-]?\d+", expression))
a, b = 0, 1
for i in nums:
j = next(nums)
a = a * j + i * b
b *= j
g = gcd(a, b)
a //= g
b //= g
return "%d/%d" % (a, b)
if __name__ == "__main__":
obj = Solution()
print(obj.fractionAddition(expression="-1/2+1/2"))
print(obj.fractionAddition(expression="-1/2+1/2+1/3"))
print(obj.fractionAddition(expression="1/3-1/2"))
|