The first algorithm to learn with Python: FizzBuzz problem

#Algorithms learned in Python

Introduction

Implement the basic algorithm in Python to deepen your understanding of the algorithm. The first of these is the FizzBuzz problem.

What is the FizzBuzz problem?

While displaying a series of numbers in order, "Fizz" is displayed if it is a multiple of 3, "Buzz" is displayed if it is a multiple of 5, and "FizzBuzz" is displayed instead of a number if it is a multiple of both 3 and 5. That is. It seems easy, but I will actually implement it once to see the details. This time, we will deal with 1 to 100 FizzBuzz problems.

Implementation code

FizzBuzz


"""
2020/12/15
@Yuya Shimizu

FizzBuzz
1~Numbers up to 100 are displayed in order, and those divisible by 3 are Fizz,
Those that are divisible by 5 are displayed as Buzz, and those that are divisible by 3 or 5 are displayed as FizzBuzz.
"""
# FizzBuzz
for i in range(1, 101):
    #Generally, it is easier to see if you write from a special case
    if (i%3==0) and (i%5==0):
        print('FizzBuzz', end=' ') #end as an argument to print= ' 'If so, it will be displayed with a space without line breaks.
    elif i%3==0:
        print('Fizz', end=' ')
    elif i%5==0:
        print('Buzz', end=' ')
    else:
        print(i, end=' ')

Implementation result

As described in the comment of the implementation code, if end ='' is set as an argument of print, it will be displayed with a space without line breaks.

1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz 31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz 43 44 FizzBuzz 46 47 Fizz 49 Buzz Fizz 52 53 Fizz Buzz 56 Fizz 58 59 FizzBuzz 61 62 Fizz 64 Buzz Fizz 67 68 Fizz Buzz 71 Fizz 73 74 FizzBuzz 76 77 Fizz 79 Buzz Fizz 82 83 Fizz Buzz 86 Fizz 88 89 FizzBuzz 91 92 Fizz 94 Buzz Fizz 97 98 Fizz Buzz 

Certainly, it can be seen that "Fizz" is displayed for multiples of 3, "Buzz" is displayed for multiples of 5, and "FizzBuzz" is displayed for multiples of both 3 and 5.

Impressions

I didn't know that it is possible to display with spaces without automatic line breaks when multiple prints are used by simply setting end ='' to the argument of print that has been used frequently. It was good to know. It was also reconfirmed that the conditions must be set with priority given to special cases.

References

Introduction to algorithms starting with Python: Standards and computational complexity learned with traditional algorithms Written by Toshikatsu Masui, Shoeisha

Recommended Posts

The first algorithm to learn with Python: FizzBuzz problem
Try to solve the fizzbuzz problem with Keras
Solving the Python knapsack problem with the greedy algorithm
[Python] Try to read the cool answer to the FizzBuzz problem
Try to solve the internship assignment problem with Python
I tried to solve the problem with Python Vol.1
Try to solve the traveling salesman problem with a genetic algorithm (Python code)
Finding a solution to the N-Queen problem with a genetic algorithm (2)
[Python] The first step to making a game with Pyxel
Finding a solution to the N-Queen problem with a genetic algorithm (1)
Search the maze with the python A * algorithm
I wanted to solve the ABC164 A ~ D problem with Python
Learn the design pattern "Singleton" with Python
Learn the design pattern "Facade" with Python
The road to compiling to Python 3 with Thrift
The 16th offline real-time how to write problem was solved with Python
The 16th offline real-time how to write reference problem to solve with Python
Try to solve the traveling salesman problem with a genetic algorithm (Theory)
The 19th offline real-time how to write reference problem to solve with Python
The 15th offline real-time how to write problem was solved with python
My friend seems to do python, so think about the problem ~ fizzbuzz ~
Try to solve the Python class inheritance problem
The first step in the constraint satisfaction problem in Python
The easiest way to synthesize speech with python
Try to solve the man-machine chart with Python
Specify the Python executable to use with virtualenv
How to try the friends-of-friends algorithm with pyfof
Say hello to the world with Python with IntelliJ
Algorithm learned with Python 14th: Tic-tac-toe (ox problem)
The easiest way to use OpenCV with python
[Algorithm x Python] How to use the list
Introduction to Python with Atom (on the way)
[Introduction to Algorithm] Find the shortest path [Python3]
[Introduction to Udemy Python3 + Application] 9. First, print with print
How to Learn Kaldi with the JUST Corpus
Find the shortest path with the Python Dijkstra's algorithm
Learn Python with ChemTHEATER
Note: How to get the last day of the month with python (added the first day of the month)
I tried to learn the sin function with chainer
Basic information Write the 2018 fall algorithm problem in Python
[First API] Try to get Qiita articles with Python
Learn Nim with Python (from the beginning of the year).
[Introduction to Python] How to iterate with the range function?
I tried to touch the CSV file with Python
I tried to solve the soma cube with python
[Python] How to specify the download location with youtube-dl
Convert the image in .zip to PDF with Python
I want to inherit to the back with python dataclass
The first step to getting Blender available from Python
Specify MinGW as the compiler to use with Python
The 14th offline real-time writing reference problem with Python
[Python] How to rewrite the table style with python-pptx [python-pptx]
The 15th offline real-time I tried to solve the problem of how to write with python
Connect to BigQuery with Python
ABC163 C problem with python3
Connect to Wikipedia with Python
Post to slack with Python 3
First neuron simulation with NEURON + Python
[Python3] Dijkstra's algorithm with 14 lines
Switch python to 2.7 with alternatives
Write to csv with Python