Skip to content

Instantly share code, notes, and snippets.

@gpiancastelli
Last active August 12, 2019 08:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gpiancastelli/e474eee327559ea90618362008e5e1d4 to your computer and use it in GitHub Desktop.
Save gpiancastelli/e474eee327559ea90618362008e5e1d4 to your computer and use it in GitHub Desktop.
A more general FizzBuzz solution that also works for Raindrops
def createConverter(factors):
def convert(n):
converted = ''.join([
factor[1] if n % factor[0] == 0 else ''
for factor in factors
])
return converted or f'{n}'
return convert
fizzbuzz = createConverter([(3, 'Fizz'), (5, 'Buzz')])
for n in (9, 35, 15, 8):
# Fizz, Buzz, FizzBuzz, 8
print(fizzbuzz(n))
raindrops = createConverter([(3, 'Pling'), (5, 'Plang'), (7, 'Plong')])
for n in (9, 10, 14, 15, 21, 35, 52, 105):
# Pling, Plang, Plong, PlingPlang, PlingPlong, PlangPlong, 52, PlingPlangPlong
print(raindrops(n))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment