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

W W srilyk at gmail.com
Tue Oct 28 15:16:51 CET 2008

On Tue, Oct 28, 2008 at 8:34 AM, Coltrey Mather <pyar2 at cowsgomoo.org> wrote:

> 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.

I doubt there's a faster method than string conversion... although here's
another method that's a *lot* longer to work out:

for x in xrange(0, 1000000):
    if x/1000000 == 1:
        count += 1
    if x/10 == 1:
        count += 1
        x = x%10
        x = x%10
    if x/1 == 1:
        count += 1

that *should* give you the right result... but it sure is a pain, and i
doubt it would be any quicker than a string conversion, though it is pure

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/mailman/private/pyar2/attachments/20081028/e321efaf/attachment-0001.htm>

More information about the PyAR2 mailing list