[Fundamental Information Technology Engineer Examination] I wrote an algorithm for determining leap years in Python.

Overview

--There is an algorithm in the afternoon exam of the Fundamental Information Technology Engineer Examination. I can't understand even if I solve the past questions ... I would like to actually write the algorithm in Python to deepen my understanding.

--Last time, I wrote the algorithm of Euclidean algorithm. --This time, I will write from the algorithm of ** leap year judgment **.

Judgment of leap year

algorithm

--If the Christian era is not divisible by 4 and 100, it is a leap year. However, if the Christian era is divisible by 400, it is a leap year. If neither is true, it is not a leap year.

code

#IsLeapYear function to determine leap year
def IsLeapYear(Year):
    #Outer branch processing
    if Year % 4 == 0 and Year % 100 != 0: #If this condition is true
        Ans = True #Leap year
    else:
        #Inner branch processing
        if Year % 400 == 0: #If this condition is true
            Ans = True #Leap year
        else:
            Ans = False #Not a leap year
    return Ans

print("Execution result:",IsLeapYear(2104))
print("Execution result:",IsLeapYear(2105))
print("Execution result:",IsLeapYear(2200))
print("Execution result:",IsLeapYear(2400))

Execution result

Execution result: True
Execution result: False
Execution result: False
Execution result: True

Summary

――Judgment of leap year is unexpectedly complicated. --Next time, let's write an algorithm for ** maximum array value **

reference

--I quoted or referred to the judgment of Chapter 3 02 leap year in this book. Information processing textbook, book that can solve algorithm problems of the Fundamental Information Technology Engineer Examination, 2nd edition

Recommended Posts

[Fundamental Information Technology Engineer Examination] I wrote an algorithm for determining leap years in Python.
I tried it with Wolfram Alpha and google, referring to "[Fundamental Information Technology Engineer Examination] I wrote an algorithm for determining leap years in Python."
[Fundamental Information Technology Engineer Examination] I wrote an algorithm for the maximum value of an array in Python.
[Fundamental Information Technology Engineer Examination] I wrote the algorithm of Euclidean algorithm in Python.
[Fundamental Information Technology Engineer Examination] I wrote a linear search algorithm in Python.
python Basic sorting algorithm summary (Basic Information Technology Engineer Examination)
Fundamental Information Technology Engineer Examination (FE) Afternoon Exam Python Sample Question Explanation
Fundamental Information Technology Engineer Examination Implemented Python sample questions without using external libraries
A story about downloading the past question PDF of the Fundamental Information Technology Engineer Examination in Python at once
I wrote python in Japanese
I wrote an empty directory automatic creation script in Python
Develop an investment algorithm in Python 2
I wrote Fizz Buzz in Python
I wrote the queue in Python
I wrote the stack in Python
I searched for prime numbers in python
I wrote a graph like R glmnet in Python for sparse modeling in Lasso
Note that I understand the least squares algorithm. And I wrote it in Python.
I searched for the skills needed to become a web engineer in Python
I wrote an IPython Notebook-like Tkinter wrapper [Python]
I wrote a class in Python3 and Java
I wrote "Introduction to Effect Verification" in Python
I wrote an IPython Notebook-like Gtk wrapper [Python]