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

Coltrey Mather pyar2 at cowsgomoo.org
Tue Oct 28 14:34:27 CET 2008


def f(x):
    '''Since I do x+1, you can enter 999999 and it'll be inclusive'''
    return len([i
                for i in
                ''.join(map(str, range(x+1)))
                if i is '1'])

seems to be pretty fast (judged by profile.run()), since everything is
in memory the slowest part is string conversion -- someone please tell
me how to not need that.

On Tue, Oct 28, 2008 at 06:42,  <gslindstrom at gmail.com> wrote:
> It's much better to use xrange() -- as you did -- for things like this. If
> you use range(), Python will create a list and then traverse through it. In
> our case, it will make an array with 1,000,000 elements. xrange() creates
> generator which creates the values as they are used. Your program is almost
> identical to mine, though I like Jesse's program in a Perlish sort of way
> (hey, Jesse...no offense intended!!). If I get the chance, I'll run them
> both and evaluate the time/cpu usage.
>
> --greg
>
> On Oct 28, 2008 6:33am, W W <srilyk at gmail.com> wrote:
>> That's a little more simplified than mine:
>
> <snip>
> _______________________________________________
> PyAR2 mailing list
> PyAR2 at python.org
> http://mail.python.org/mailman/listinfo/pyar2
>
>


More information about the PyAR2 mailing list