Skip to content

Instantly share code, notes, and snippets.

@martin-minarik
Created August 27, 2023 22:31
Show Gist options
  • Save martin-minarik/e75fad770e726cf298afc6f2609f6465 to your computer and use it in GitHub Desktop.
Save martin-minarik/e75fad770e726cf298afc6f2609f6465 to your computer and use it in GitHub Desktop.
FizzBuzz without using explicit "if"
def fizzbuzz(number):
"""
Return 'Fizz' if `num` is divisible by 3,
return 'Buzz' if `num` is divisible by 5,
return 'FizzBuzz' if `num` is divisible both by 3 and 5.
If `num` isn't divisible neither by 3 nor by 5, return `number`.
Example:
fizzbuzz(3) # Fizz
fizzbuzz(5) # Buzz
fizzbuzz(15) # FizzBuzz
fizzbuzz(8) # 8
"""
fizz = ((number % 3 == 0) and "Fizz") or '' # It's divisible by 3, then it is "Fizz" or otherwise empty string
buzz = ((number % 5 == 0) and "Buzz") or '' # It's divisible by 5, then it is "Buzz" or otherwise empty string
return fizz + buzz or number
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment