Last active
August 12, 2019 08:12
-
-
Save gpiancastelli/e474eee327559ea90618362008e5e1d4 to your computer and use it in GitHub Desktop.
A more general FizzBuzz solution that also works for Raindrops
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 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