Python vs Ruby
Magnus Lie Hetland
mlh at idi.ntnu.no
Mon Jan 29 10:43:39 EST 2001
> > (By the way, your function wasn't altogether correct...)
>
> With all respect, yours is even worse.
I know. I noticed after posting. (That's what you get for being cocky ;-)
Although... "Worse"? In this case incorrect is still only incorrect, no?
Oh, well...
The *correct* version would of course be:
def fib(n):
x, y, z = 1, 1, 1
if n > 2:
for i in range(n-2):
x, y, z = y, x+y, x+y
return z
Now, one might ask... Why use the z? Why indeed... :-)
> My version will correctly return 0 for fib(0); your function returns 1.
Well... I thought my version "correctly" disallowed 0 as an argument. I
guess it depends on how you define the sequence...
For instance, (quoting Encyclopaedia Britannica):
> Fibonacci numbers
>
> the elements of the sequence of numbers 1, 1, 2, 3, 5, 8, 13, 21, . . . ,
each of
> which is the sum of the two previous numbers. The interesting properties
of
> these numbers were first noted by the medieval Italian mathematician
Leonardo
> Pisano.
I believe this was the original version of the sequence. I see that many
start with a
zero... Oh, well...
> And I avoid the range() call.
Yes you do indeed. I guess I just dislike for-loops simulated by while-loops
in generals <wink>, but in Python it seems it might actually be a good idea.
(After all, isn't it true that Python once didn't have a for loop?)
Oh, well. What a discussion anyway... :-)
happy-to-spend-time-on-anything-useless'ly y'rs
- Magnus
--
Magnus Lie Hetland (magnus at hetland dot org)
"Reality is what refuses to disappear when you stop
believing in it" -- Philip K. Dick
More information about the Python-list
mailing list