I was solving a collection of paiza level-up questions, but I didn't have a model answer, so I made it myself with explanations. The language is Python3.
Paiza's skill check sample problem Fizz Buzz (equivalent to paiza rank C) https://paiza.jp/works/mondai/skillcheck_sample/fizz-buzz?language_uid=python3 You can register paiza for free immediately, so I recommend you to register for the time being.
Let's program the following problems!
The integer N is given as input.
Display integers from 1 to N in order from 1.
However, the number you are trying to display is
・ When it is a multiple of 3 and a multiple of 5, "Fizz Buzz" ・ When it is a multiple of 3, "Fizz" ・ When it is a multiple of 5, "Buzz"
Please display >> instead of the number.
Value to be entered
The input is given in the following format.
N
N is an integer greater than or equal to 1 and less than or equal to N.
Input value One line break is inserted at the end of the last line. The string is passed from standard input.
Expected output
At the end, start a new line and do not include extra characters or blank lines.
Conditions
In all test cases, the following conditions are met.
・ 1 ≤ N ≤ 100 ・ N is an integer
Input example 1
5
Output example 1
1 2 Fizz 4 Buzz
Input example 2
20
Output example 2
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 Fizz Buzz 16 17 Fizz 19 Buzz
The input value is an integer value from 0-100. After receiving the input value using the input function, ** type conversion ** from str type to int type using the int function.
fizz-buzz_1.py
n = int(input())
Then look for the numbers that you should display instead. This time, we will use the ** range function ** because we only need to search for integers from 1 to n in order. At this time, you need to be careful about how to specify the ** range ** of the range function.
If you enter two arguments with the range function, the first argument will be ~~ begin ~~ start and the second argument will be ~~ end ~~ stop. At this time, the ~~ end ~~ stop argument specifies that the number itself is not included and the previous integer is output.
In other words, the output for range (1, 5)
is 1,2,3,4, and ** 5 is not included. ** **
If you want to output 1,2,3,4,5, you need to specify range (1, 5 + 1)
.
range_sample.py
print(list(range(1,5))) # [1,2,3,4]
print(list(range(1,6))) # [1,2,3,4,5]
In this case, we want an integer value from 1 to n, so pass ** 1, n + 1 ** as an argument to the range function. When combined with the for statement, it looks like this.
fizz-buzz_2.py
n = int(input())
for i in range(1, n+1):
print(i)
"""
n =At 5,
1
2
3
4
5
"""
Next, determine the numbers that need to be converted to fizzBuzz. For that, we use% (algebraic operator to find the remainder).
If n is divided by 3 and the remainder is zero, then n is a multiple of 3. If n is divided by 5 and the remainder is zero, then n is a multiple of 5. If the remainder of n divided by 15 is zero, then n is a multiple of 3 and a multiple of 5.
A multiple of 3 and a multiple of 5 means a multiple of ** 15.
This time, the if-elif statement is used for judgment.
At this time, it is necessary to pay attention to the ** judgment order **. To determine if ~~ n is a multiple of 3 or 5, If you write it before judging whether it is a multiple of 15, it will not work. ~~ To determine if> n is a multiple of 3 or 5, If you write it before judging whether it is a multiple of> 15, you need to devise a little.
There is a method pointed out by @shiracamus, please check it in the comment section below. This time, I would like to solve it according to the order of judgment.
if_sample.py
#Bad code
n = 15
if n%3 == 0:
print("n is a multiple of 3.")
elif n%5 == 0:
print("n is a multiple of 5.")
elif n%15 == 0:
print("n is a multiple of 3 and a multiple of 5.")
else:
print("n is neither a multiple of 3 nor a multiple of 5.")
#n is a multiple of 3.
If n is a multiple of 15, it will be determined to be a multiple of 3 or 5 instead of a multiple of 15. Keep in mind that it will be processed in order from the top.
So, first of all, determine if it is a multiple of 15. After that, it works well to determine if it is a multiple of 5 or a multiple of 3.
fizz-buzz_3.py
n = 15
if n%15 == 0:
print("n is a multiple of 3 and a multiple of 5.")
elif n%5 == 0:
print("n is a multiple of 5.")
elif n%3 == 0:
print("n is a multiple of 3.")
else:
print("n is neither a multiple of 3 nor a multiple of 5.")
#n is a multiple of 3 and a multiple of 5.
In general, when writing an if-elif statement, it seems that it often works well if you write the narrower conditions first.
fizz-buzz.py
n = int(input())
for i in range(1, n+1):
if i%15==0:
print("Fizz Buzz")
elif i%5==0:
print("Buzz")
elif i%3==0:
print("Fizz")
else:
print(i)
https://qiita.com/KoyanagiHitoshi/items/3286fbc65d56dd67737c
I'm going to explain in detail where I was caught in the past. It may have been roundabout and difficult to understand, but Feel free to comment if you have any concerns.
Recommended Posts