from fractions import gcd class Solution(object): def fractionAddition(self, expression): """ :type expression: str :rtype: str """ # exp = ''.join([('+' if c=='-' else '') + c for c in expression]) exp = expression.replace('-', '+-') # Thanks ManuelP. This is more clear than the above. # print exp exp = exp.split('+') # print exp a = [(int(s.split('/'))*3628800//int(s.split('/'))) if s else 0 for s in exp] # print a s = sum(a) b = 3628800 if a==0: return '0/1' g = gcd(s,b) return str(s//g) + '/' + str(b//g)
I don't think
exp = ''.join([('+' if c=='-' else '') + c for c in expression]) is "clear". I think it would be far clearer to instead use
exp = expression.replace('-', '+-'). Also, it's unclear why you have those two
return '0/1' and the
@ManuelP You are right. Thank you for your advice.