Skip to main content
Answer invalidation. Sigh.
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

The general technique works, kind of.

The comment # yields all fibonacci numbers below n is misleading. TheIt's weird that the fibonacci(n) generator actually yields n Fibonacci numbers, which is not the same as all the Fibonacci numbers that do not exceed n. It's also weird that thegenerates a sequence that starts with 1, 2, 3, 5, 8, …. Conventionally, the Fibonacci sequence starts with either 0, 1, 1, 2, 3, 5, 8, … or 1, 1, 2, 3, 5, 8, ….

A purer approach would be to use [itertools.takewhile()] in conjunction with an infinite generator.

You should also take advantage of parallel assignment.

The fibonacci() generator would look like this:

def fibonacci():
    a, b = 0, 1
    while True:
        yield a
        a, b = b, a + b

filter_even() is fine. You could also just write a generator expression instead.

from itertools import takewhile

print(sum(n for n in takewhile(lambda n: n < 4000000, fibonacci()) if n % 2 == 0))

The general technique works, kind of.

The comment # yields all fibonacci numbers below n is misleading. The fibonacci(n) generator actually yields n Fibonacci numbers, which is not the same as all the Fibonacci numbers that do not exceed n. It's also weird that the sequence starts with 1, 2, 3, 5, 8, …. Conventionally, the Fibonacci sequence starts with either 0, 1, 1, 2, 3, 5, 8, … or 1, 1, 2, 3, 5, 8, ….

A purer approach would be to use [itertools.takewhile()] in conjunction with an infinite generator.

You should also take advantage of parallel assignment.

The fibonacci() generator would look like this:

def fibonacci():
    a, b = 0, 1
    while True:
        yield a
        a, b = b, a + b

filter_even() is fine. You could also just write a generator expression instead.

from itertools import takewhile

print(sum(n for n in takewhile(lambda n: n < 4000000, fibonacci()) if n % 2 == 0))

It's weird that the fibonacci() generates a sequence that starts with 1, 2, 3, 5, 8, …. Conventionally, the Fibonacci sequence starts with either 0, 1, 1, 2, 3, 5, 8, … or 1, 1, 2, 3, 5, 8, ….

A purer approach would be to use [itertools.takewhile()] in conjunction with an infinite generator.

You should also take advantage of parallel assignment.

The fibonacci() generator would look like this:

def fibonacci():
    a, b = 0, 1
    while True:
        yield a
        a, b = b, a + b

filter_even() is fine. You could also just write a generator expression instead.

from itertools import takewhile

print(sum(n for n in takewhile(lambda n: n < 4000000, fibonacci()) if n % 2 == 0))
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

The general technique works, kind of.

The comment # yields all fibonacci numbers below n is misleading. The fibonacci(n) generator actually yields n Fibonacci numbers, which is not the same as all the Fibonacci numbers that do not exceed n. It's also weird that the sequence starts with 1, 2, 3, 5, 8, …. Conventionally, the Fibonacci sequence starts with either 0, 1, 1, 2, 3, 5, 8, … or 1, 1, 2, 3, 5, 8, ….

A purer approach would be to use [itertools.takewhile()] in conjunction with an infinite generator.

You should also take advantage of parallel assignment.

The fibonacci() generator would look like this:

def fibonacci():
    a, b = 0, 1
    while True:
        yield a
        a, b = b, a + b

filter_even() is fine. You could also just write a generator expression instead.

from itertools import takewhile

print(sum(n for n in takewhile(lambda n: n < 4000000, fibonacci()) if n % 2 == 0))