83.4k views
0 votes
Return the appropriate part of FizzBuzz This is a small part of a classic software engineering interview question. fizz_buzz_check takes a single integer argument, number. Complete the function such that it returns a value using the following rules: If the number is divisible by 3 (i.e division by 3 has a remainder of 0), return the string Fizz. If it is divisible by five, return the string Buzz. If it is divisible by 3 AND 5, return FizzBuzz. Otherwise, just return the original argument back without any changes.

User Jaksky
by
9.1k points

1 Answer

6 votes

Answer:

The function in Python:

def fizz_buzz_check(num):

if num%3 == 0:

result = "Fizz"

if num%5 == 0:

result = "Buzz"

if num%3 == 0 and num%5 == 0:

result = "FizzBuzz"

if num%3 != 0 and num%5 != 0:

result = num

return result

Step-by-step explanation:

The function to complete is not given. So, I write a new function from scratch.

This defines the function

def fizz_buzz_check(num):

This checks if num is divisible by 3

if num%3 == 0:

If yes, the returned value is stored as "Fizz"

result = "Fizz"

This checks if num is divisible by 5

if num%5 == 0:

If yes, the returned value is stored as "Buzz"

result = "Buzz"

This checks if num is divisible by 3 and 5

if num%3 == 0 and num%5 == 0:

If yes, the returned value is stored as "FizzBuzz"

result = "FizzBuzz"

This checks if num is not divisible by 3 and by 5

if num%3 != 0 and num%5 != 0:

If yes, the returned value is stored as the initial argument

result = num

This returns the appropriate value

return result

User Anish Gupta
by
8.6k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.