Created
June 2, 2020 05:33
-
-
Save rvente/61372eba0533ace3e50a3d5a4c6b4fc2 to your computer and use it in GitHub Desktop.
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
# Solution A | |
for i in range(100): | |
rem3 = (i % 3 == 0) | |
rem5 = (i % 5 == 0) | |
while not rem3 and not rem5: | |
print(i) | |
break | |
while rem3 and not rem5: | |
print("Fizz") | |
break | |
while not rem3 and rem5: | |
print("Buzz") | |
break | |
while rem3 and rem5: | |
print("FizzBuzz") | |
break | |
# Solution B | |
def FizzBuzz(x): | |
FizzList = ['', 'Fizz'] | |
BuzzList = ['', 'Buzz'] | |
intList = [str(x), '', ''] | |
div3 = (x % 3 == 0) | |
div5 = (x % 5 == 0) | |
return(FizzList[div3] + BuzzList[div5] + intList[div3 + div5]) | |
for i in range(100): | |
print(i, FizzBuzz(i)) | |
# Solution C | |
def fizzbuzz(n): | |
for i in range(1,n+1): | |
string = ((i % 15 == 0) and "fizzbuzz") \ | |
or ((i % 3 == 0) and "fizz") \ | |
or ((i % 5 == 0) and "buzz") \ | |
or i | |
print(string) | |
fizzbuzz(20) | |
# Solution D | |
def FizzBuzz(n: int): | |
# return True if x is divisible by n | |
divisible = lambda x, n: not x % n | |
# return body if cond else "" | |
take_if = lambda cond, body: cond * body | |
ret = [] | |
for i in range(1,n+1): | |
fizz = take_if(divisible(i,3), "Fizz") | |
buzz = take_if(divisible(i,5), "Buzz") | |
num = take_if(not (fizz or buzz), str(i)) | |
ret.append(fizz + buzz + num) | |
return ret | |
if __name__ == "__main__": | |
print("\n".join(FizzBuzz(20))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment