🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of epequeno

epequeno's solution

to Space Age in the Elm Track

Published at Apr 19 2021 · 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.

Elm Installation

Refer to the Installing Elm page for information about installing elm.

Writing the Code

The code you have to write is located inside the src/ directory of the exercise. Elm automatically installs packages dependencies the first time you run the tests so we can start by running the tests from the exercise directory with:

$ elm-test

To automatically run tests again when you save changes:

$ elm-test --watch

As you work your way through the tests suite in the file tests/Tests.elm, be sure to remove the skip <| calls from each test until you get them all passing!

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 is possible to submit an incomplete solution so you can see how others have completed the exercise.

Tests.elm

module Tests exposing (tests)

import Expect exposing (FloatingPointTolerance(..))
import SpaceAge exposing (Planet(..), ageOn)
import Test exposing (..)


tests : Test
tests =
    describe "SpaceAge"
        [ test "31.7 Earth years in Earth years" <|
            \() -> Expect.within (Absolute 0.01) 31.69 (ageOn Earth 1000000000.0)
        , skip <|
            test "2 Earth years in Earth years" <|
                \() -> Expect.within (Absolute 0.01) 2 (ageOn Earth 63115200.0)
        , skip <|
            test "67.6 Earth years in Mercury years" <|
                \() -> Expect.within (Absolute 0.01) 280.88 (ageOn Mercury 2134835688.0)
        , skip <|
            test "2 Earth years in Mercury years" <|
                \() -> Expect.within (Absolute 0.01) 8.3 (ageOn Mercury 63115200.0)
        , skip <|
            test "6 Earth years in Venus years" <|
                \() -> Expect.within (Absolute 0.01) 9.78 (ageOn Venus 189839836.0)
        , skip <|
            test "2 Earth years in Venus years" <|
                \() -> Expect.within (Absolute 0.01) 3.25 (ageOn Venus 63115200.0)
        , skip <|
            test "67.6 Earth years in Mars years" <|
                \() -> Expect.within (Absolute 0.01) 35.88 (ageOn Mars 2129871239.0)
        , skip <|
            test "2 Earth years in Mars years" <|
                \() -> Expect.within (Absolute 0.01) 1.06 (ageOn Mars 63115200.0)
        , skip <|
            test "28.6 Earth years in Jupiter years" <|
                \() -> Expect.within (Absolute 0.01) 2.41 (ageOn Jupiter 901876382.0)
        , skip <|
            test "2 Earth years in Jupiter years" <|
                \() -> Expect.within (Absolute 0.01) 0.17 (ageOn Jupiter 63115200.0)
        , skip <|
            test "63.5 Earth years in Saturn years" <|
                \() -> Expect.within (Absolute 0.01) 2.15 (ageOn Saturn 2000000000.0)
        , skip <|
            test "2 Earth years in saturn years" <|
                \() -> Expect.within (Absolute 0.01) 0.07 (ageOn Saturn 63115200.0)
        , skip <|
            test "38.3 Earth years in Uranus years" <|
                \() -> Expect.within (Absolute 0.01) 0.46 (ageOn Uranus 1210123456.0)
        , skip <|
            test "2 Earth years in uranus years" <|
                \() -> Expect.within (Absolute 0.01) 0.02 (ageOn Uranus 63115200.0)
        , skip <|
            test "57.7 Earth years in Neptune years" <|
                \() -> Expect.within (Absolute 0.01) 0.35 (ageOn Neptune 1821023456.0)
        , skip <|
            test "2 Earth years in Neptune years" <|
                \() -> Expect.within (Absolute 0.01) 0.01 (ageOn Neptune 63115200.0)
        ]
module SpaceAge exposing (Planet(..), ageOn)


type Planet
    = Mercury
    | Venus
    | Earth
    | Mars
    | Jupiter
    | Saturn
    | Uranus
    | Neptune


ageOn : Planet -> Float -> Float
ageOn planet seconds =
    let
        multiplier =
            case planet of
                Mercury ->
                    0.2408467

                Venus ->
                    0.61519726

                Earth ->
                    1.0

                Mars ->
                    1.8808158

                Jupiter ->
                    11.862615

                Saturn ->
                    29.447498

                Uranus ->
                    84.016846

                Neptune ->
                    164.79132
    in
    seconds / (31557600 * multiplier)

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?