Created
November 3, 2017 22:05
-
-
Save unrivaledcreations/4cd736f65ccc9928ca02fc57665fc2ea to your computer and use it in GitHub Desktop.
FizzBuzz Solution 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
def fizzbuzz(n): | |
answer = '' | |
fizz = not n % 3 | |
buzz = not n % 5 | |
if (fizz): | |
answer += 'Fizz' | |
if (buzz): | |
answer += 'Buzz' | |
if (not fizz and not buzz): | |
answer += str(n) | |
return answer | |
for n in range(1,100+1): # +1 makes the 1-100 thing more readable. | |
print(fizzbuzz(n)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is an example of human-readable Python code. This entire exercise could be written in a single line of code: See this very, very excellent example FizzBuzz solution by @Kapppa. Kapppa's example shows a very deep understanding of the Python language which went straight into my cookbook. The contrast between the one-line solution and the solution given here (in this gist), raises questions about the moral dilemma: Should one write over-optimize software code; or should one write longer code for easier future code maintenance and human-readable clarity?