[Tutor] loops

Rich Lovely roadierich at googlemail.com
Tue Dec 8 17:18:46 CET 2009


2009/12/8 spir <denis.spir at free.fr>:
> Lie Ryan <lie.1296 at gmail.com> dixit:
>
>> On 12/8/2009 9:39 PM, Dave Angel wrote:
>> > Richard Hultgren wrote:
>> >> a = 0
>> >> b = 1
>> >> count = 0
>> >> max_count = 20
>> >> while count < max_count:
>> >> count = count + 1
>> >> # we need to keep track of a since we change it
>> >> old_a = a # especially here
>> >> old_b = b
>> >> a = old_b
>> >> b = old_a + old_b
>> >> # Notice that the , at the end of a print statement keeps it
>> >> # from switching to a new line
>> >> print old_a,
>> >>
>> >>
>> >>
>> > What's your question or comment?
>> >
>> > If you're just looking for a challenge, I'll point out that the same
>> > result could be gotten with 4 lines of Python.
>>
>> you lose, 1 line is all it takes for me:
>> print "0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181"
>
> you lose, I don't even need a one-liner python prog to write a text into whatever stdout may be -- except if remote ;-)
>
> Denis
> ________________________________
>
> la vita e estrany
>
> http://spir.wikidot.com/
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

This, of course is a rather dirty, implementation (and probably
version) specific hack, but I can /calculate/ the sequence, using just
one line:

>>> print " ".join(str(i) for i in [x if x<2 else (locals()['_[1]'][-1]+locals()['_[1]'][-2]) for x in xrange(20)])
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181

(CPython 2.6, on osx10.6)

It's slightly more typing than the plain string, but extend it to
about 30 entries, and I think I win?


Note to OP: don't _ever_ do it this way in a serious project.

-- 
Rich "Roadie Rich" Lovely

There are 10 types of people in the world: those who know binary,
those who do not, and those who are off by one.


More information about the Tutor mailing list