Created
January 29, 2016 06:43
-
-
Save yuanmai/844e899bb5f2a0632605 to your computer and use it in GitHub Desktop.
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
def has_factor(factor, word): | |
return lambda n: word if n % factor == 0 else '' | |
def has_substring(i, word): | |
return lambda n: word if str(i) in str(n) else '' | |
def concat(rule1, rule2): | |
return lambda n: rule1(n) + rule2(n) | |
def one_of(rule1, rule2): | |
return lambda n: rule1(n) or rule2(n) | |
def fizz_buzz_whizz(f, b, w): | |
fizz = has_factor(f, 'Fizz') | |
buzz = has_factor(b, 'Buzz') | |
whizz = has_factor(w, 'Whizz') | |
return one_of(has_substring(f, 'Fizz'), one_of(concat(fizz, concat(buzz, whizz)), str)) | |
rule = fizz_buzz_whizz(3, 5, 7) | |
for i in range(1, 101): | |
print rule(i) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment