TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Adding Digits in Python (or Smarter-looking doesn't mean better)

6 pointsby mauriciocabout 10 years ago

1 comment

chriswittsabout 10 years ago
Surprised you didn&#x27;t have `sum(map(int, number_string))` in there.<p><pre><code> 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 </code></pre> Marginally slower, a hell of a lot easier to read what&#x27;s going on.