Skip to content

Instantly share code, notes, and snippets.

@esehara
Forked from AyatoKinkori/fizzbuzz.py
Last active December 23, 2015 08:29
Show Gist options
  • Save esehara/6608229 to your computer and use it in GitHub Desktop.
Save esehara/6608229 to your computer and use it in GitHub Desktop.
from __future__ import print_function
def mul_check_closure(mul_num, out_str):
"""
Genelate multiple ckeck closure.
:param mul_num: check if n of mul_num.
:param out_str: For mul_num of multiple, return out_str
:returns: function (lambda)
>>> mul_check_closure(3, "fizz")(3)
'fizz'
>>> mul_check_closure(3, "fizz")(5)
''
"""
return lambda num: out_str if (num % mul_num == 0) else ""
fizz_check = mul_check_closure(3, "fizz")
buzz_check = mul_check_closure(5, "buzz")
def _fizz_buzz(num):
"""
process fizzbuzz
>>> _fizz_buzz(1)
1
>>> _fizz_buzz(3)
'fizz'
>>> _fizz_buzz(5)
'buzz'
>>> _fizz_buzz(15)
'fizzbuzz'
"""
return (fizz_check(num) + buzz_check(num)) or num
def fizz_buzz(num):
[print(_fizz_buzz(n)) for n in range(1, num + 1)]
def test_fizzbuzz():
import doctest
doctest.testmod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment