Algorithm learned with Python 6th: Leap year

#Algorithms learned in Python

Introduction

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

leap year


Conditions
A year divisible by 4 is called a leap year.

In this way, if even the caution (*) of the condition is not satisfied, a leap year is simply divisible by 4. I implemented this in python. The code and output are shown below.

code

leap_year.py


"""
2020/12/18
@Yuya Shimizu

leap year
・ Year divisible by 4
However, a year that is divisible by 100 and not divisible by 400 is not a leap year.
"""

def leap_year(year):
    if year%100 == 0 and year%400 != 0:
        print(str(year) + "The year is not a leap year.")
    else:
        if year%4 == 0:
            print(str(year) + "The year is a leap year.")
        else:
            print(str(year) + "The year is not a leap year.")

year = int(input("Leap year judgment\n>>"))

leap_year(year)
Output 1
Leap year judgment
>>2000
2000 is a leap year.
Output 2
Leap year judgment
>>1900
1900 is not a leap year.

Judgment was made by user input. The code is pretty simple, and the caveat is that it brings in more specific conditions first.

Reorganized version

The above program was reorganized with reference to the comments from @shiracamus. Specifically, print in the function is omitted and True and False are returned. The code and output are shown below.

code

leap_year_.py


"""
2020/12/18
@Yuya Shimizu

leap year
・ Year divisible by 4
However, a year that is divisible by 100 and not divisible by 400 is not a leap year.

Reorganized version
@True, referring to the proposal of shiracamus,Made a program that returns False
"""

def leap_year(year):
    if year%100 == 0 and year%400 != 0:
        return False
    else:
        if year%4 == 0:
            return True
        else:
            return False

year = int(input("Leap year judgment\n>>"))

print(leap_year(year))
output
Leap year judgment
>>2020
True

I made a better one.

Impressions

I've written leap year code a couple of times before, but I think it's relatively simple, probably because I was able to sort out my mind a little through the algorithm.

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 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 4th: Prime numbers
Algorithm learned with Python 19th: Sorting (heapsort)
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
Algorithm learned with Python 3rd: Radix conversion
[Python3] Dijkstra's algorithm with 14 lines
[Python] Object-oriented programming learned with Pokemon
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
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
1. Statistics learned with Python 1-2. Calculation of various statistics (Numpy)
python starts with ()
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
The 14th offline real-time writing reference problem with Python
1st Algorithm Practical Test Solve past questions with python
Serial communication with Python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)