-
Exercism - Leap(With.Kotlin)문제풀이/Exercism 2019. 7. 22. 12:24반응형
[문제]
Given a year, report if it is a leap year.
The tricky thing here is that a leap year in the Gregorian calendar occurs:
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.
Notes
Though our exercise adopts some very simple rules, there is more to learn!
For a delightful, four minute explanation of the whole leap year phenomenon, go watch this youtube video.
[Solution1]
class Year(year: Int) { var isLeap: Boolean = false init { if(year % 400 != 0){ if(year % 100 != 0){ if(year %4 == 0){ isLeap = true } } }else{ isLeap = true } } }
테스트를 통과만 가능하도록 짠 부끄러운 코드
[Solution2]
class Year(year: Int) { val isLeap = checkLeap(year) private fun checkLeap(year: Int):Boolean { if(year % 400 == 0) return true if(year % 100 != 0 && year % 4 == 0) return true return false } }
정리하여 나온 형태
[Solution3]
class Year(year: Int) { val isLeap = (year % 400 == 0) || (year % 100 != 0 && year % 4 == 0) }
한줄로 요약해보라는 조언에 수정한 형태
반응형'문제풀이 > Exercism' 카테고리의 다른 글
Exercism - Difference Of Squares(with.Kotlin) (0) 2019.08.06 Exercism - Scrabble Score (With.Kotlin) (0) 2019.07.30 Exercism - Space Age (With.Kotlin) (0) 2019.07.26 Exercism - Gigasecond(With.Kotlin) (0) 2019.07.23 Exercism - Hamming(With.Kotlin) (0) 2019.07.21 Exercism - RNA Transcription(With.Kotlin) (0) 2019.07.16 Exercism - Twofer (with.Kotlin) (0) 2019.07.16 exercism submit 방법 (0) 2019.07.10