[With commentary] Solve Fizz Buzz (equivalent to paiza rank C) in Python

at first

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.

problem

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

Commentary

Receive input value

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())

Display numbers in order

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
"""

Find FizzBuzz

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.

Answer code

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)

reference

https://qiita.com/KoyanagiHitoshi/items/3286fbc65d56dd67737c

Finally

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

[With commentary] Solve Fizz Buzz (equivalent to paiza rank C) in Python
Solve Fizz Buzz (equivalent to paiza rank C) in Python
Solve word counts (equivalent to paiza rank C) in Python
Solve addition (equivalent to paiza rank D) in Python
Solve multiplication (equivalent to paiza rank D) in Python
Solve number sorting (equivalent to paiza rank D) in Python
Solve island hunting (equivalent to paiza rank S) in Python
Solve character matches (equivalent to paiza rank D) in Python
Solve mod7 fortune-telling (equivalent to paiza rank S) in Python
Solve the smallest value in Python (equivalent to paiza rank D)
Challenge Fizz Buzz! Problems with Python in 5 patterns (only now up to 398)
Fizz Buzz in Python
Fizz Buzz in Python
Solve ABC163 A ~ C with Python
Let's try Fizz Buzz in Python
I wrote Fizz Buzz in Python
Solve ABC168 A ~ C with Python
Solve ABC036 A ~ C in Python
How to wrap C in Python
Solve ABC162 A ~ C with Python
Solve ABC167 A ~ C with Python
Solve ABC158 A ~ C with Python
Solve ABC037 A ~ C in Python
I want to solve APG4b with Python (only 4.01 and 4.04 in Chapter 4)
Solve ABC175 A, B, C in Python
I wanted to solve ABC160 with Python
How to work with BigQuery in Python
I wanted to solve ABC159 in Python
To work with timestamp stations in Python
I wanted to solve ABC172 with Python
How to use the C library in Python
Sorting AtCoder ARC 086 C hashes to solve in Ruby, Perl, Java and Python
[REAPER] How to play with Reascript in Python
How to generate permutations in Python and C ++
I wanted to solve NOMURA Contest 2020 with Python
Convert PDFs to images in bulk with Python
Try to solve the man-machine chart with Python
Log in to Yahoo Business with Selenium Python
How to use tkinter with python in pyenv
I want to solve APG4b with Python (Chapter 2)
Type notes to Python scripts for running PyTorch model in C ++ with libtorch
[For beginners in competition professionals] I tried to solve 40 AOJ "ITP I" questions with python
How to convert / restore a string with [] in python
Try to solve the programming challenge book with python3
Try to make a Python module in C language
How to do hash calculation with salt in Python
Try to solve the internship assignment problem with Python
How to run tests in bulk with Python unittest
I tried to solve the soma cube with python
Try embedding Python in a C ++ program with pybind11
Convert the image in .zip to PDF with Python
Super Primer to python-Getting started with python3.5 in 3 minutes
I was addicted to scraping with Selenium (+ Python) in 2020
I want to work with a robot in python.
I tried to solve the problem with Python Vol.1
Solve Atcoder ABC176 (A, B, C, E) in Python
To do the equivalent of Ruby's ObjectSpace._id2ref in Python
I tried to solve AOJ's number theory with Python
Solve ABC168D in Python
Solve ABC167-D in Python
Solve AtCoder 167 with python