-
Exercism - Space Age (With.Kotlin)문제풀이/Exercism 2019. 7. 26. 14:08반응형
[문제]
Given an age in seconds, calculate how old someone would be on:
- Earth: orbital period 365.25 Earth days, or 31557600 seconds
- Mercury: orbital period 0.2408467 Earth years
- Venus: orbital period 0.61519726 Earth years
- Mars: orbital period 1.8808158 Earth years
- Jupiter: orbital period 11.862615 Earth years
- Saturn: orbital period 29.447498 Earth years
- Uranus: orbital period 84.016846 Earth years
- Neptune: orbital period 164.79132 Earth years
So if you were told someone were 1,000,000,000 seconds old, you should be able to say that they're 31.69 Earth-years old.
If you're wondering why Pluto didn't make the cut, go watch this youtube video.
[Solution1]
class SpaceAge(age: Long) { constructor(age: Int) : this(age = age.toLong()) private val earthSeconds = age.toDouble() / 31557600.toDouble() fun onEarth(): Double = formatDouble(earthSeconds) fun onMercury(): Double = formatDouble(earthSeconds / 0.2408467) fun onVenus(): Double = formatDouble(earthSeconds / 0.61519726) fun onMars(): Double = formatDouble(earthSeconds / 1.8808158) fun onJupiter(): Double = formatDouble(earthSeconds / 11.862615) fun onSaturn(): Double = formatDouble(earthSeconds / 29.447498) fun onUranus(): Double = formatDouble(earthSeconds / 84.016846) fun onNeptune(): Double = formatDouble(earthSeconds / 164.79132) private fun formatDouble(double: Double): Double = String.format("%.2f", double).toDouble() }
반응형'문제풀이 > Exercism' 카테고리의 다른 글
Exercism - Acronym(with.Kotlin) (0) 2019.08.19 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 - Gigasecond(With.Kotlin) (0) 2019.07.23 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