Last active
October 8, 2015 05:38
-
-
Save zonuexe/3286382 to your computer and use it in GitHub Desktop.
FizzBuzz
This file contains hidden or 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
| import sys;[i for i in range(1,101)if(sys.stdout.write((""if i%3 else"Fizz")+(""if i%5 else "Buzz")+(str(i)if i%3 and i%5 else"")+"\n"))] |
This file contains hidden or 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
| print "\n".join([ {1:{1:"FizzBuzz",0:"Fizz"},0:{1:"Buzz",0:str(i)}}[(i%3==0)+0][(i%5==0)+0] for i in range(1, 101)]) |
This file contains hidden or 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
| class FizzBuzz(object): | |
| def __init__ (self, l=100): | |
| self.n = 0 | |
| self.l = l | |
| def __iter__(self): | |
| return self | |
| def next(self): | |
| self.n += 1 | |
| if self.n > self.l: | |
| self.n = 0 | |
| raise StopIteration | |
| else: | |
| return self.fizzbuzz(self.n) | |
| def fizzbuzz(self, n): | |
| if n % 3 == 0 and n % 5 == 0: | |
| return "FizzBuzz" | |
| elif n % 3 == 0: | |
| return "Fizz" | |
| elif n % 5 == 0: | |
| return "Buzz" | |
| else: | |
| return str(n) | |
| if __name__ == "__main__": | |
| for n in FizzBuzz(120): | |
| print n |
This file contains hidden or 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 fizzbuzz (last): | |
| def isfizz (n): | |
| return n % 3 == 0 | |
| def isbuzz (n): | |
| return n % 5 == 0 | |
| def isfizzbuzz (n): | |
| return isfizz(n) and isbuzz(n) | |
| def tofizzbuzz (n): | |
| if (isfizzbuzz(n)): | |
| return "FizzBuzz" | |
| elif (isfizz(n)): | |
| return "Fizz" | |
| elif (isbuzz(n)): | |
| return "Buzz" | |
| else: | |
| return str(n) | |
| def fizzbuzz_iter(l): | |
| if (len(l) == 0): | |
| return [] | |
| else: | |
| return [tofizzbuzz(l[0])] + fizzbuzz_iter(l[1:]) | |
| return "\n".join(fizzbuzz_iter(range(1, last+1))) | |
| if __name__ == "__main__": | |
| print fizzbuzz(100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment