Skip to content

Instantly share code, notes, and snippets.

@bChiquet
Created June 17, 2016 17:06
Show Gist options
  • Save bChiquet/4ea55f30551dbfe47474c883603f915c to your computer and use it in GitHub Desktop.
Save bChiquet/4ea55f30551dbfe47474c883603f915c to your computer and use it in GitHub Desktop.
Maybe slightly overengineered ?
from fractions import gcd
import unittest
from itertools import combinations
baseMods=[(3, 'fizz'),
(5, 'buzz'),
(7, 'foo')]
mods = []
def deriveAnyOrderMods():
derivedMods=[]
for order in range(len(baseMods)+1)[1:]:
for derivative in combinations(baseMods, order):
newMod = [1, ""]
for baseComponent in derivative :
newMod[0] = (lambda a,b : a*b // gcd (a, b)) (newMod[0], baseComponent[0])
newMod[1] += baseComponent[1]
derivedMods.append(tuple(newMod))
del mods[:]
mods.extend(derivedMods)
def fizBuz(param):
deriveAnyOrderMods()
list = range(param+1)[1:]
mapListWithMods(param, list)
return str(list[-1])
def applyMods(function):
def applyThem(*args):
for mod in mods:
function(args[0], args[1], mod[0], mod[1])
return applyThem
@applyMods
def mapListWithMods(param, list, number=0, expression='oops'):
for item in range(param)[number-1::number]:
list[item] = expression
class fizBuzTest(unittest.TestCase):
def test_should_return_1_when_1(self):
self.assertEqual('1', fizBuz(1))
def test_should_return_2_when_2(self):
self.assertEqual('2', fizBuz(2))
def test_should_return_fizz_when_3(self):
self.assertEqual('fizz', fizBuz(3))
def test_should_return_fizz_when_6(self):
self.assertEqual('fizz', fizBuz(6))
def test_should_return_buzz_when_5(self):
self.assertEqual('buzz', fizBuz(5))
def test_should_return_fizzbuzz_when_15(self):
self.assertEqual('fizzbuzz', fizBuz(3*5))
def test_should_return_fizzfoo_when_15(self):
self.assertEqual('fizzfoo', fizBuz(3*7))
def test_should_return_fizzbuzzfoo_when_15(self):
self.assertEqual('fizzbuzzfoo', fizBuz(3*5*7))
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment