Hacker News new | past | comments | ask | show | jobs | submit login
Adding Digits in Python (or Smarter-looking doesn't mean better) (explique.me)
6 points by mauricioc on May 13, 2015 | hide | past | favorite | 1 comment



Surprised you didn't have `sum(map(int, number_string))` in there.

  In [5]: def f(x):
     ...:     x = str(x)
     ...:     return sum(map(int, x))

  In [6]: %timeit second_way_optimized(x)
  1000 loops, best of 3: 200 µs per loop

  In [7]: %timeit f(x)
  1000 loops, best of 3: 223 µs per loop
Marginally slower, a hell of a lot easier to read what's going on.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: