Skip to content

Instantly share code, notes, and snippets.

@yuanmai
Created January 29, 2016 06:43
Show Gist options
  • Save yuanmai/844e899bb5f2a0632605 to your computer and use it in GitHub Desktop.
Save yuanmai/844e899bb5f2a0632605 to your computer and use it in GitHub Desktop.
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