취미생활/CheckIO

[checkIO]Element 06.Fizz Buzz

우주먼지의하루 2020. 3. 3. 14:33
728x90

"Fizz buzz" is a word game we will use to teach the robots about division. Let's learn computers.

You should write a function that will receive a positive integer and return:
"Fizz Buzz" if the number is divisible by 3 and by 5;
"Fizz" if the number is divisible by 3;
"Buzz" if the number is divisible by 5;
The number as a string for other cases.

 

Input: A number as an integer.

Output: The answer as a string.

 

Example:

checkio(15) == "Fizz Buzz"

checkio(6) == "Fizz"

checkio(5) == "Buzz"

checkio(7) == "7"

 

* 3,5로 나누어 떨어지면 "Fizz Buzz" , 3으로만 나누어지면 "Fizz", 5로만 나누어지면 "Buzz"

* 3,5로 나누어 떨어지지않으면 숫자 그대로 return

 

 

 

- 풀이

 

def checkio(number: int) -> str:
    if number % 3 == 0 and number % 5 == 0 :
        return "Fizz Buzz"
    elif number % 3 == 0:
        return "Fizz"
    elif number % 5 == 0:
        return "Buzz"
    else:
        return str(number)

 

- 다른 풀이

 

def checkio(number: int) -> str:
    out = []
    if number % 3 == 0:
        out.append("Fizz")
    if number % 5 == 0:
        out.append("Buzz")
    return str(number) if not out else " ".join(out)
반응형

'취미생활 > CheckIO' 카테고리의 다른 글

[checkIO]Element 08.Best Stock  (0) 2020.03.14
[checkIO]Element 07.Even the Last  (0) 2020.03.07
[checkIO]Element 05.Secret Message  (0) 2020.03.01
[checkIO]Element 04.Digits Multiplication  (0) 2020.02.26
[checkIO]Element 03.Index Power  (0) 2020.02.24