-
Exercism - Gigasecond(With.Kotlin)문제풀이/Exercism 2019. 7. 23. 14:29반응형
[문제]
Given a moment, determine the moment that would be after a gigasecond has passed.
A gigasecond is 10^9 (1,000,000,000) seconds.
[Solution1]
import java.time.LocalDate import java.time.LocalDateTime import java.time.LocalTime import java.time.temporal.ChronoUnit class Gigasecond { private val gigaSeconds = 1_000_000_000L val date: LocalDateTime constructor(localDate: LocalDate){ date = LocalDateTime.of(localDate, LocalTime.MIN) .plus(gigaSeconds, ChronoUnit.SECONDS) } constructor(localDateTime:LocalDateTime){ date = localDateTime.plus(gigaSeconds, ChronoUnit.SECONDS) } }
기본 생성자를 2개 만들어서 처리
[Solution2]
import java.time.LocalDate import java.time.LocalDateTime import java.time.temporal.ChronoUnit class Gigasecond { private val gigaSeconds = 1_000_000_000L val date: LocalDateTime constructor(localDate: LocalDate){ date = localDate.atStartOfDay().plus(gigaSeconds, ChronoUnit.SECONDS) } constructor(localDateTime:LocalDateTime){ date = localDateTime.plus(gigaSeconds, ChronoUnit.SECONDS) } }
atStartOfDay를 이용 해보라는 조언에 사용하여 처리한 코드
[Solution3]
import java.time.LocalDate import java.time.LocalDateTime import java.time.temporal.ChronoUnit class Gigasecond(localDateTime:LocalDateTime) { constructor(localDate: LocalDate) : this(localDate.atStartOfDay()) private val gigaSeconds = 1_000_000_000L val date = localDateTime.plus(gigaSeconds, ChronoUnit.SECONDS) }
Solution2에서 통과 되었지만 더 줄일수 있다는 조언에 다른이들의 해답을 확인해 본 결과
기본 constructor에 constructor를 추가로 이용하는 방법을 확인함
해당 방법으로 코드를 수정 하여 적용
반응형'문제풀이 > Exercism' 카테고리의 다른 글
Exercism - Perfect Numbers(with.Kotlin) (0) 2019.08.17 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 - Leap(With.Kotlin) (0) 2019.07.22 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