Created
May 27, 2015 13:18
-
-
Save jshawl/1c27f935a256dd3148f0 to your computer and use it in GitHub Desktop.
Python FizzBuzz
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
class FizzBuzz: | |
def __init__(self,num): | |
self.count(num) | |
def count(self,num): | |
if num >= 1: | |
self.count(num - 1) | |
self.fizz_buzz(num) | |
def fizz_buzz(self,num): | |
if num % 15 == 0: | |
print "FizzBuzz" | |
elif num % 3 == 0: | |
print "Fizz" | |
elif num % 5 == 0: | |
print "Buzz" | |
else: | |
print num | |
FizzBuzz(100) |
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 count(num): | |
if num >= 1: | |
count(num - 1) | |
fizz_buzz(num) | |
def fizz_buzz(num): | |
if num % 15 == 0: | |
print "FizzBuzz" | |
elif num % 3 == 0: | |
print "Fizz" | |
elif num % 5 == 0: | |
print "Buzz" | |
else: | |
print num | |
count(100) |
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
for i in range(1,101): | |
string = "" | |
if i % 3 == 0: | |
string += "Fizz" | |
if i % 5 == 0: | |
string += "Buzz" | |
if i % 5 != 0 and i % 3 != 0: | |
string += str(i) | |
print string |
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
for i in range(1,101): | |
if i % 15 == 0: | |
print "FizzBuzz" | |
elif i % 3 == 0: | |
print "Fizz" | |
elif i % 5 == 0: | |
print "Buzz" | |
else: | |
print i |
["Fizzbuzz" if i % 15==0 else "Fizz" if i % 3==0 else "Buzz" if i % 5==0 else i for i in range(1,101)]
def fizzbuzz(x):
if x % 3 == 0 and x % 5 == 0:
return "Fizzbuzz"
elif x % 3 ==0:
return "Fizz"
elif x % 5 ==0:
return "Buzz"
else:
return str(x)
for x in range(1, 101):
print (fizzbuzz(x))
def f(n:int,m:list,w:list):
s=''
for x in m:
if n%x==0: s+=w[m.index(x)]
if s=='': s=n
return s
for i in range(1, 100 +1): print(f(i,[3,5],['Fizz','Buzz']))
i like this solution because it can work with more than just 3 and 5
you can do fizzbuzzfuzz
with 3, 5, and 7 just by editing the contents of the lists
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
print('\n'.join([str(x)*(x%3 != 0 and x%5 != 0) +'Fizz'*(x%3==0)+'Buzz'*(x%5==0) for x in range(1,101)]))
It's disgusting but it's fun