CS61A SU20 one-liner challenge for disc02, Q1.6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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