Advertisement
ijontichy

fizzbuzz.py

Jul 15th, 2013
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.72 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. TRUETEST = lambda x: True
  4.  
  5. class FizzBuzz:
  6.     def __init__(self, value=None, test=None):
  7.         if getattr(test, '__call__', None) is None:
  8.             self.test = TRUETEST
  9.         else:
  10.             self.test = test
  11.  
  12.         self.value = value
  13.  
  14.     def output(self, testvalue):
  15.         if self.test(testvalue):
  16.             return str(self.value)
  17.  
  18.         return ""
  19.  
  20. def checkModulo(trueval):
  21.     def ret(checkval):
  22.         return (checkval % trueval) == 0
  23.  
  24.     return ret
  25.  
  26. fizz = FizzBuzz("Fizz", checkModulo(3))
  27. buzz = FizzBuzz("Buzz", checkModulo(5))
  28.  
  29. fizzes = [fizz, buzz]
  30.  
  31. for i in range(1, 101):
  32.     print("{: >3}: ".format(i), end="")
  33.     for f in fizzes:
  34.         print(f.output(i), end="")
  35.  
  36.     print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement