[Tutor] How inefficient is this code?

C Smith illusiontechniques at gmail.com
Thu May 8 01:27:32 CEST 2014


A topic came up on slashdot concerning "intermediate" programming,
where the poster expressed the feeling that the easy stuff is too easy
and the hard stuff is too hard.

Someone did point out that 'intermediate' programming would still
involve actually selling code or at least some professional
experience, so this would probably fall into the category of 'novice'.

I feel similarly right now and the cs 101 classes from udacity or
coursera or the opencourseware stuff are very easy and boring. I tried
the class "design of computer programs" on udacity and made it about
halfway before being overwhelmed. The class involved making an api for
a regex-like function to parse text (if I am communicating that
correctly, it was sort of rewriting regex functionality in Python
terms), and it seemed to go over a very definite cliff in terms of
difficulty. Could someone provide a concise example of decorator use?

Someone suggested projecteuler.net and I started blazing through
things I had done before, when I realized this would be a good time to
start more efficient practices instead of code that just happens to
work and may be very inefficient.

#sum all even fib seq integers under 4 million
fibs = [1,2]
sum = 0
while fibs[-1] < 4000000:
    nexty = fibs[-1] + fibs[-2]
    fibs.append(nexty)

for xer in fibs:
    if xer%2 == 0:
        sum += xer
print sum

This gets the correct solution, but what would be ways to improve
speed or use more complicated parts of Python to do the same thing.
Thanks in advance


More information about the Tutor mailing list