fork download
  1. from functools import reduce
  2.  
  3. def str2num(s):
  4. try:
  5. return int(s)
  6. except:
  7. return float(s)
  8.  
  9. def calc(exp):
  10. ss = exp.split('+')
  11. ns = map(str2num, ss)
  12. return reduce(lambda acc, x: acc + x, ns)
  13.  
  14. def main():
  15. r = calc('100 + 200 + 345')
  16. print('100 + 200 + 345 =', r)
  17. r = calc('99 + 88 + 7.6')
  18. print('99 + 88 + 7.6 =', r)
  19.  
  20. main()
  21.  
  22. # your code goes here
  23. # your code goes here
  24. # your code goes here
Success #stdin #stdout 0.08s 14116KB
stdin
Standard input is empty
stdout
100 + 200 + 345 = 645
99 + 88 + 7.6 = 194.6