Skip to content

Instantly share code, notes, and snippets.

@rgegriff
Created September 13, 2021 15:05
Show Gist options
  • Save rgegriff/a56bcc3030f65bd061639865d189f093 to your computer and use it in GitHub Desktop.
Save rgegriff/a56bcc3030f65bd061639865d189f093 to your computer and use it in GitHub Desktop.
# there is a more straightforward solution, but this test is boring, so here's a generator-based version :P
# Fibonacci generator
def fibgen():
'''generator that yields fibonacci numbers'''
yield 0
crnt = 0
nxt = 1
while True:
crnt, nxt = nxt, crnt+nxt
yield crnt
def oddgen(gen):
'''return only the odd results from a generator'''
while True:
val = gen.__next__()
if val%2!=0:
yield val
def stop_at_value(stop_val, gen):
'''stop returning values from a generator if value returned is greater than stop_val'''
while True:
val = gen.__next__()
if val < stop_val:
yield val
else:
return
solution_generator = stop_at_value(10000, oddgen(fibgen()))
answer = sum(solution_generator) # => 14328
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment