Algorithm learned with Python 7th: Year conversion

#Algorithm learned in Python

Introduction

Implement the basic algorithm in Python to deepen your understanding of the algorithm. The year is treated as the seventh bullet.

Year to year

This time, we made it possible to convert from Meiji to Reiwa. However, as will be described later, this time the program is versatile, so it can be expanded as much as possible by adding only the year standard and year name. However, the year of the same year (eg, 1988 and 1989) will be the new era.

The year boundary for the first year from the Meiji era to Reiwa, that is, the boundary of the era, is shown below.

Year Year
First year of the Meiji era 1868
Taisho era 1912
Showa 1st year 1926
1989 1989
First year of Reiwa 2019

Based on the above, implement it in python. The code and output are shown below.

code

era_name.py


"""
2020/12/18
@Yuya Shimizu

Year conversion
"""

def trans2era_name(year):
    year_dict = {2019:"Reiwa", 1989:"Heisei", 1926:"Showa", 1912:"Taisho", 1868:"Meiji"} #Year boundary
    change = True #Variable for determining whether conversion was possible
    
    #Year judgment
    for Y in year_dict:
        if year >= Y:
            year -= Y
            if year == 0:
                print(year_dict[Y] + "First year")
            else:    
                print(year_dict[Y] + str(year + 1) + "Year")
            change = False
            break
        
    #Error message for not subject to year conversion
    if change:
        print("Conversion is possible only in the range from Meiji to Reiwa")
        
trans2era_name(1868)
Output 1 (2020)
Reiwa 2 years
Output 2 (1868)
First year of the Meiji era
Output 3 (1800)
Conversion is possible only in the range from Meiji to Reiwa

As I mentioned at the beginning, I think we have created something that is highly versatile. I will explain the code a little. The year_dict contains the year and the year set of the first year. In the defined function, the dictionary type array is rotated by for, so the target of year conversion can be expanded by simply adding the contents of ** year_dict **. However, it may be necessary to change the content of the error message that is output when the year cannot be converted.

Impressions

The year conversion dealt with this time was given as a reference question, and I thought about it myself and created the program. As I mentioned earlier, I think it was well organized. Although I have just begun to learn algorithms, I felt that algorithms and ideas were important, and I became more motivated to learn algorithms.

References

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

Recommended Posts

Algorithm learned with Python 7th: Year conversion
Algorithm learned with Python 6th: Leap year
Algorithm learned with Python 10th: Binary search
Algorithm learned with Python 5th: Fibonacci sequence
Algorithm learned with Python 9th: Linear search
Algorithm learned with Python 8th: Evaluation of algorithm
Algorithm learned with Python 4th: Prime numbers
Algorithm learned with Python 19th: Sorting (heapsort)
Algorithm learned with Python 3rd: Radix conversion
Algorithm learned with Python 12th: Maze search
Algorithm learned with Python 11th: Tree structure
Algorithm learned with Python 16th: Sorting (insertion sort)
Algorithm learned with Python 14th: Tic-tac-toe (ox problem)
Algorithm learned with Python 15th: Sorting (selection sort)
Algorithm learned with Python 17th: Sorting (bubble sort)
Algorithm learned with Python 18th: Sorting (stack and queue)
Algorithm learned with Python 2nd: Vending machine
[Python3] Dijkstra's algorithm with 14 lines
[Python] Object-oriented programming learned with Pokemon
MP3 to WAV conversion with Python
Perceptron learning experiment learned with Python
Python data structures learned with chemoinformatics
Efficient net pick-up learned with Python
1. Statistics learned with Python 1-1. Basic statistics (Pandas)
Implementation of Dijkstra's algorithm with python
Python algorithm
[Python] Reactive Extensions learned with RxPY (3.0.1) [Rx]
Search the maze with the python A * algorithm
1. Statistics learned with Python 1-3. Calculation of various statistics (statistics)
Let's develop an investment algorithm with Python 1
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Latitude / longitude coordinates ↔ UTM coordinate conversion with python
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
1. Statistics learned with Python 1-2. Calculation of various statistics (Numpy)
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
I tried it with SymPy Live, Wolfram Alpha and google with reference to "Algorithm learned with Python 4th: Prime numbers".
Use Python and word2vec (learned) with Azure Databricks
1. Statistics learned with Python 2-1. Probability distribution [discrete variable]
Find the shortest path with the Python Dijkstra's algorithm
Solving the Python knapsack problem with the greedy algorithm
Excel with Python
Microcomputer with Python
"Principle of dependency reversal" learned slowly with Python
Cast with python
I learned Python with a beautiful girl at Paiza # 02
Learn Nim with Python (from the beginning of the year).
I learned Python with a beautiful girl at Paiza # 01
The first algorithm to learn with Python: FizzBuzz problem
Problems when creating a csv-json conversion tool with python
The 14th offline real-time writing reference problem with Python
1st Algorithm Practical Test Solve past questions with python