Last active
December 16, 2015 19:59
-
-
Save jomido/5488690 to your computer and use it in GitHub Desktop.
General Fizzbuzz III
This file contains hidden or 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
| def n_of_k(n, k): | |
| return lambda n: n % k == 0 | |
| def n_for_ks(n, *ks): | |
| return [n_of_k(n, k) for k in ks] | |
| def fizzbuzz(rules, data): | |
| def make(s): | |
| return lambda n: s(n) if callable(s) else s | |
| rules = [(r, make(s)) for r, s in rules] | |
| for n in data: | |
| for i, c in enumerate(rules): | |
| conds, fn = c | |
| if all([f(n) for f in n_for_ks(n, *conds)]): | |
| yield (fn(n)) | |
| break | |
| def test(): | |
| rules = [ | |
| ([3, 5, 2], "WOW!"), | |
| ([3, 5], "FizzBuzz"), | |
| ([3], "Fizz"), | |
| ([5], "Buzz"), | |
| ([1], lambda n: n) | |
| ] | |
| data = xrange(1, 31) | |
| fizzbuzz_1 = fizzbuzz(rules, data) | |
| for result in fizzbuzz_1: | |
| print (result) | |
| test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment