Skip to content

Instantly share code, notes, and snippets.

@LarynQi
Created June 30, 2020 00:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LarynQi/677333bf832bd4fe90abc86c5ea09f0d to your computer and use it in GitHub Desktop.
Save LarynQi/677333bf832bd4fe90abc86c5ea09f0d to your computer and use it in GitHub Desktop.
CS61A SU20 one-liner challenge for disc02, Q1.6
import doctest
def make_keeper(n):
"""Returns a function which takes one parameter cond and prints out
all integers 1..i..n where calling cond(i) returns True.
>>> def is_even(x):
... # Even numbers have remainder 0 when divided by 2.
... return x % 2 == 0
>>> make_keeper(5)(is_even)
2
4
"""
return lambda cond: list(map(print, filter(cond, range(1, n + 1)))) and None
doctest.testmod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment