관리 메뉴

A seeker after truth

exercism leap solution 본문

Algorithm/문제풀이

exercism leap solution

dr.meteor 2020. 1. 24. 18:47

P)

Given a year, report if it is a leap year.

 

The tricky thing here is that a leap year in the Gregorian calendar occurs:

 

```text

on every year that is evenly divisible by 4

except every year that is evenly divisible by 100

unless the year is also evenly divisible by 400

```

 

For example, 1997 is not a leap year, but 1996 is. 1900 is not a leap

year, but 2000 is.

 

 

mysol)

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

S2)

def is_leap_year(x): # 디논 때 배웠던 진리표 아이디어 활용! 대신 명제를 첨부터 잘 설정해야함.
    # 400의 배수는 무조건 100의 배수이므로 뒤쪽 명제만 참이면 된다. 하필 or를 쓴 이유를 생각해볼 것. 그리고 != 를 쓴 이유도.
    return x % 4 == 0 and (x % 100 != 0 or x % 400 == 0)

'Algorithm > 문제풀이' 카테고리의 다른 글

exercism RNA-transcription solution  (0) 2020.01.24
exercism pangram solution  (0) 2020.01.24
exercism reverse-string solution  (0) 2020.01.24
프로그래머스 정렬 문제풀이(1)  (0) 2020.01.24
문제5  (0) 2020.01.08