[PyAR2] Programming Challenge II - One is the Magic Number

W W srilyk at gmail.com
Tue Oct 28 12:33:44 CET 2008


That's a little more simplified than mine:
In [7]: def foo():
   ...:     count = 0
   ...:     for x in xrange(0,1000000):
   ...:         for num in str(x):
   ...:             if num == '1':
   ...:                 count+=1
   ...:     return count
   ...:

In [8]: foo()
Out[8]: 600000

On Mon, Oct 27, 2008 at 11:09 PM, Jesse Jaggars <jhjaggars at gmail.com> wrote:

> sum(int(str(x).count("1")) for x in xrange(1000000))
>
> Or, if you prefer to supply the parameters:
>
> def howmany( needle, haystack ):
>    return sum(int(str(z).count(str(needle))) for z in
> xrange(int(haystack)))
> _______________________________________________
> PyAR2 mailing list
> PyAR2 at python.org
> http://mail.python.org/mailman/listinfo/pyar2
>



-- 
To be considered stupid and to be told so is more painful than being called
gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
every vice, has found its defenders, its rhetoric, its ennoblement and
exaltation, but stupidity hasn't. - Primo Levi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/mailman/private/pyar2/attachments/20081028/eaded821/attachment.htm>


More information about the PyAR2 mailing list