Me and a friend setup a competition between ourselves with the goal: Print out all numbers in the Fibonacci sequence that are less than or equal to 3000 using Python. The winner was whoever could accomplish this in the least amount of lines of code. As soon as I began writing code I wondered if we should also take into account cleanliness and readability in the competition requirements. After pondering this more, I decided to write 3 different ways to accomplish this task. I would love it if you would please judge each of them and tell me which one you feel is "better" Python code and also give ideas on how to possibly improve the code.

1.

fib = [0, -1]
while fib[0] < 3000:
    print(fib[0])
    fib[1] = fib[0] - fib[1]
    fib[0] += fib[1]

2.

fib = [0, -1]
while fib[0] < 3000:
    print(fib[0]) ; fib[1] = fib[0] - fib[1] ; fib[0] += fib[1]

3.

lol = '1\n1\n2\n3\n5\n8\n13\n21\n34\n55\n89\n144\n233\n377\n610\n987\n1597\n2584' ; print(lol)
share|improve this question
1  
Fewest lines of code can be very misleading to the point of being irrelevant, as is shown in your second and third snippets. – Andrew Marshall Dec 14 '11 at 3:22
I think you shouldn't modify a collection while you're iterating over it. (line 5, listing 1). What do you think guys? I'm not a python expert – santiagobasulto Dec 14 '11 at 3:22
2  
@Andrew: I'd go further: lines of code and readability tend to be inversely correlated, once you go beyond a certain threshold. I think all three of these are poor. – Glenn Maynard Dec 14 '11 at 3:30
2nd shouldnt even count because its the same as the first with a semicolin taking place of a newline. 3rd is just a result string and shouldntcount because t doesnt actually calulate the result. Also I think its fine to modify the collection here because you arent changing its length. You just always eval the zero index. Though why even use a list and indexing when you could use two variables? – jdi Dec 14 '11 at 3:34
1  
@santiagobasulto, there is no iteration over a collection occurring here. – Winston Ewert Dec 15 '11 at 18:34
show 3 more comments

migrated from stackoverflow.com Dec 15 '11 at 16:52

4 Answers

How about this:

a, b = 1, 0
while b < 3000:
    print b
    a, b = a+b, a

It's short an efficient, since it's an iterative solution, but it closely resembles the recursive solution. Also, it doesn't waste space in temporary lists - only the current and previous values are needed, and for storing those, using local variables is enough.

share|improve this answer
2  
1  
I wrote that snippet off the top of my head, and to think it was in the documentation! – Óscar López Dec 14 '11 at 3:36

Minified Oscar's solution:

a=b=1
while a<3e3:print a;a,b=a+b,a

35 chars

share|improve this answer
Wait, when did I switch to codegolf.stackexchange.com ?! – Vincent Savard Dec 21 '11 at 5:21
It's not equivalent, as the initial value of b is 0, not 1. – lbolla Dec 22 '11 at 9:35

Of the answers you gave us, one is certainly the best option when it comes to the question of whether or not it is "efficient, clean, readable python code". Your option 2 is your option 1, just with 3 lines crushed together into 1. Just because it is done in the fewest lines (I'm not going to justify #3 with serious response, though I admire your cleverness), that doesn't mean it's "better". For a "pythonic" (that's the 'in' word for good, elegant python code) see Oscar Lopez's response, also on this page. (Very clever Oscar!)

That being said, lines of code is always a very arbitrary restriction for competitions with friends (I have personal experience with this one). If you want to really ramp up the challenge with your friend, try comparing how many comparisons, swaps, or calculations you can do. You should rank them in the following order, in the order of descending desirability.

  1. Calculations
  2. Comparisons
  3. Swaps

Just remember, that although calculations are more efficient than comparisons, you should still never have needless calculations. The same goes with the other two. Don't use 'em if you don't need 'em.

share|improve this answer
What exactly do you mean by "swaps"? Variables swapping values? – Zizma Dec 14 '11 at 4:25
Swapping is a very common inefficiency problem in searching in sorting. Try looking up different sorts online (bubble sort, selection sort, quick sort are a few, in increasing order of efficiency). It involves storing data in a temporary location to use it in another location later on. It is inefficient because you have to spend processor time and ram opening up memory, making a variable, storing it, and later getting it from the variable. Swaps aren't used in this problem but they are in many others. – Mike Holler Dec 15 '11 at 16:36
Thanks for clarifying. – Zizma Dec 15 '11 at 23:15

For the heck of it:

fib = [0,1]
while fib[-1] < 3000:
    fib.append(fib[-1]+fib[-2])
print fib[:-1]

One extra operation, so shoot me...

share|improve this answer

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.