Created
July 20, 2012 18:02
-
-
Save jasonsperske/3152293 to your computer and use it in GitHub Desktop.
A cleaner Fizz/Buzz in Python
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
# My Third Python program | |
# Based on this spec: http://www.codinghorror.com/blog/2007/02/why-cant-programmers-program.html | |
import time | |
import random | |
def fbPrinter(simulator, translator, i): | |
time.sleep(simulator(i)) | |
print(translator(i)) | |
def fbTranslator(n): | |
output = '{0}'.format(n) | |
if n % 15 == 0: | |
return '{0}, uh I mean "FizzBuzz"! Said the child'.format(n) | |
elif n % 5 == 0: | |
return 'Buzz!' | |
elif n % 3 == 0: | |
return 'Fizz!' | |
return output | |
def fbSimulateProficiency(n): | |
if n % 15 == 0: | |
return random.randint(3, 5) | |
elif n % 5 == 0: | |
if i < 20: | |
return random.randint(2, 3) | |
elif i < 20: | |
return random.randint(1,3) | |
return random.randint(0, 1) | |
[fbPrinter(fbSimulateEfficiency, fbTranslator, i) for i in range(1, 100)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Switched over to passing functions (cleaner)