Avatar of kpreston1

kpreston1's solution

to Space Age in the Scala Track

Published at Oct 16 2019 · 0 comments
Instructions
Test suite
Solution

Given an age in seconds, calculate how old someone would be on:

  • Mercury: orbital period 0.2408467 Earth years
  • Venus: orbital period 0.61519726 Earth years
  • Earth: orbital period 1.0 Earth years, 365.25 Earth days, or 31557600 seconds
  • 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.

The Scala exercises assume an SBT project scheme. The exercise solution source should be placed within the exercise directory/src/main/scala. The exercise unit tests can be found within the exercise directory/src/test/scala.

To run the tests simply run the command sbt test in the exercise directory.

Please see the learning and installation pages if you need any help.

Source

Partially inspired by Chapter 1 in Chris Pine's online Learn to Program tutorial. http://pine.fm/LearnToProgram/?Chapter=01

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

SpaceAgeTest.scala

import org.scalatest.{Matchers, FunSuite}

/** @version 1.1.0 */
class SpaceAgeTest extends FunSuite with Matchers {

  test("age on Earth") {
    SpaceAge.onEarth(1000000000) should be (31.69 +- 0.01)
  }

  test("age on Mercury") {
    pending
    SpaceAge.onMercury(2134835688) should be (280.88 +- 0.01)
  }

  test("age on Venus") {
    pending
    SpaceAge.onVenus(189839836) should be (9.78 +- 0.01)
  }

  test("age on Mars") {
    pending
    SpaceAge.onMars(2.329871239E9) should be (39.25 +- 0.01)
  }

  test("age on Jupiter") {
    pending
    SpaceAge.onJupiter(901876382) should be (2.41 +- 0.01)
  }

  test("age on Saturn") {
    pending
    SpaceAge.onSaturn(3.0E9) should be (3.23 +- 0.01)
  }

  test("age on Uranus") {
    pending
    SpaceAge.onUranus(3.210123456E9) should be (1.21 +- 0.01)
  }

  test("age on Neptune") {
    pending
    SpaceAge.onNeptune(8.210123456E9) should be (1.58 +- 0.01)
  }
}
object SpaceAge {

	val MERCURY = "MERCURY"
	val VENUS = "VENUS"
	val EARTH = "EARTH"
	val MARS = "MARS"
	val JUPITER = "JUPITER"
	val SATURN = "SATURN"
	val URANUS = "URANUS"
	val NEPTUNE = "NEPTUNE"

	val MERCURY_ORBITAL_PERIOD: Double = 0.2408467
	val VENUS_ORBITAL_PERIOD: Double = 0.61519726
	val EARTH_ORBITAL_PERIOD: Double = 1.0
	val MARS_ORBITAL_PERIOD: Double = 1.8808158
	val JUPITER_ORBITAL_PERIOD: Double = 11.862615 
	val SATURN_ORBITAL_PERIOD: Double = 29.447498
	val URANUS_ORBITAL_PERIOD: Double = 84.016846
	val NEPTUNE_ORBITAL_PERIOD: Double = 164.79132

	val EARTH_ORBITAL_PERIOD_IN_SECONDS: Double = 31557600

	def onMercury(age: Double): Double = 
		calculateEarthAge(age, planetOrbitalPeriodMatch(MERCURY))
	def onVenus(age: Double): Double   = 
		calculateEarthAge(age, planetOrbitalPeriodMatch(VENUS))
	def onEarth(age: Double): Double   = 
		calculateEarthAge(age, planetOrbitalPeriodMatch(EARTH))
	def onMars(age: Double): Double    = 
		calculateEarthAge(age, planetOrbitalPeriodMatch(MARS))
	def onJupiter(age: Double): Double = 
		calculateEarthAge(age, planetOrbitalPeriodMatch(JUPITER))
	def onSaturn(age: Double): Double  = 
		calculateEarthAge(age, planetOrbitalPeriodMatch(SATURN))
	def onUranus(age: Double): Double  = 
		calculateEarthAge(age, planetOrbitalPeriodMatch(URANUS))
	def onNeptune(age: Double): Double = 
		calculateEarthAge(age, planetOrbitalPeriodMatch(NEPTUNE))

	def calculateEarthAge(age: Double, orbitalPeriod: Double): Double = 
		age / ( orbitalPeriod * EARTH_ORBITAL_PERIOD_IN_SECONDS )

	def planetOrbitalPeriodMatch(planetName: String): Double = planetName match {
		case MERCURY => MERCURY_ORBITAL_PERIOD
		case VENUS => VENUS_ORBITAL_PERIOD
		case EARTH => EARTH_ORBITAL_PERIOD
		case MARS => MARS_ORBITAL_PERIOD
		case JUPITER => JUPITER_ORBITAL_PERIOD
		case SATURN => SATURN_ORBITAL_PERIOD
		case URANUS => URANUS_ORBITAL_PERIOD
		case NEPTUNE => NEPTUNE_ORBITAL_PERIOD
	}
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?