Advertisement
Iam_Sandeep

592. Fraction Addition and Subtraction

Jul 24th, 2022
878
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.40 KB | None | 0 0
  1. from math import gcd
  2. class Solution:
  3.     def fractionAddition(self, esp: str) -> str:
  4.         n=len(esp)
  5.         esp=esp.replace('+',' +').replace('-',' -')
  6.         nums=esp.split()
  7.    
  8.         n,d=0,1
  9.         for num in nums:
  10.             a,b=num.split('/')
  11.             a,b=int(a),int(b)
  12.             n,d=n*b+a*d,d*b
  13.             g=gcd(n,d)
  14.             n,d=n//g,d//g
  15.         return "{}/{}".format(n,d)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement