코드로 우주평화

Century From Year 본문

나는 이렇게 학습한다/Algorithm & SQL

Century From Year

daco2020 2022. 4. 13. 23:09
반응형

Introduction

The first century spans from the year 1 up to and including the year 100, the second century - from the year 101 up to and including the year 200, etc.

Task

Given a year, return the century it is in.

Examples

1705 --> 18
1900 --> 19
1601 --> 17
2000 --> 20

 

 

Solution:

1. Divide 'year' by 100.
2. Returns the decimal point rounded up.

 

import math

def century(year):
    return math.ceil(year / 100)

 

 

If it's too easy, there is a way to convert it to a string and solve it.

1. Convert the value divided by 100 into a string.
2. Convert the half-separated string to a number, and add 1 if the number on the right exceeds 0.

def century(year) -> int:
    year = str(year/100).split('.')
    return int(year[0]) + bool(int(year[1]))

 

 

 

반응형

'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글

Get the mean of an array  (0) 2022.04.15
Sum of Odd Cubed Numbers  (0) 2022.04.14
Isograms  (0) 2022.04.12
Student's Final Grade  (0) 2022.04.11
Remove First and Last Character Part Two  (0) 2022.04.10