Re:Ask HN: starting a career in software dev in my thirties,...
by BrandonM on Thursday October 21, 2010 @02:51PM
(#1816921)
I mainly meant "improve" in terms of conciseness and readability. I didn't see any need for optimization because the snippet runs instantly:
$ time python -c "print sum(x for x in xrange(2, 1000) if sum(int(y) for y in str(x)) == 7)"
9324
real 0m0.138s
user 0m0.012s
sys 0m0.012s
And frankly I'm not sure if its faster to convert to string and back to int, or if it would be better to just use modulo arithmitic to get the values associated with the ones/tens/hundreds.
In any case, what you have is certainly readable though.