Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from math import gcd
- class Solution:
- def fractionAddition(self, esp: str) -> str:
- n=len(esp)
- esp=esp.replace('+',' +').replace('-',' -')
- nums=esp.split()
- n,d=0,1
- for num in nums:
- a,b=num.split('/')
- a,b=int(a),int(b)
- n,d=n*b+a*d,d*b
- g=gcd(n,d)
- n,d=n//g,d//g
- return "{}/{}".format(n,d)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement