ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# jaeyson's solution

## to Space Age in the Elixir Track

Published at Feb 07 2021 · 0 comments
Instructions
Test suite
Solution

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.

## Running tests

Execute the tests with:

``````\$ mix test
``````

### Pending tests

In the test suites, all but the first test have been skipped.

Once you get a test passing, you can unskip the next one by commenting out the relevant `@tag :pending` with a `#` symbol.

For example:

``````# @tag :pending
test "shouting" do
assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
end
``````

Or, you can enable all the tests by commenting out the `ExUnit.configure` line in the test suite.

``````# ExUnit.configure exclude: :pending, trace: true
``````

If you're stuck on something, it may help to look at some of the available resources out there where answers might be found.

## 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.

### space_age_test.exs

``````defmodule SpageAgeTest do
use ExUnit.Case

# @tag :pending
test "age on Earth" do
input = 1_000_000_000
assert_in_delta 31.69, SpaceAge.age_on(:earth, input), 0.005
end

@tag :pending
test "age on Mercury" do
input = 2_134_835_688
assert_in_delta 67.65, SpaceAge.age_on(:earth, input), 0.005
assert_in_delta 280.88, SpaceAge.age_on(:mercury, input), 0.005
end

@tag :pending
test "age on Venus" do
input = 189_839_836
assert_in_delta 6.02, SpaceAge.age_on(:earth, input), 0.005
assert_in_delta 9.78, SpaceAge.age_on(:venus, input), 0.005
end

@tag :pending
test "age on Mars" do
input = 2_329_871_239
assert_in_delta 73.83, SpaceAge.age_on(:earth, input), 0.005
assert_in_delta 39.25, SpaceAge.age_on(:mars, input), 0.005
end

@tag :pending
test "age on Jupiter" do
input = 901_876_382
assert_in_delta 28.58, SpaceAge.age_on(:earth, input), 0.005
assert_in_delta 2.41, SpaceAge.age_on(:jupiter, input), 0.005
end

@tag :pending
test "age on Saturn" do
input = 3_000_000_000
assert_in_delta 95.06, SpaceAge.age_on(:earth, input), 0.005
assert_in_delta 3.23, SpaceAge.age_on(:saturn, input), 0.005
end

@tag :pending
test "age on Uranus" do
input = 3_210_123_456
assert_in_delta 101.72, SpaceAge.age_on(:earth, input), 0.005
assert_in_delta 1.21, SpaceAge.age_on(:uranus, input), 0.005
end

@tag :pending
test "age on Neptune" do
input = 8_210_123_456
assert_in_delta 260.16, SpaceAge.age_on(:earth, input), 0.005
assert_in_delta 1.58, SpaceAge.age_on(:neptune, input), 0.005
end
end``````

### test_helper.exs

``````ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)``````

### lib/space_age.ex

``````defmodule SpaceAge do
@type planet ::
:mercury
| :venus
| :earth
| :mars
| :jupiter
| :saturn
| :uranus
| :neptune

@orb_period 31_557_600

@planets %{
mercury: 0.2408467,
venus:   0.61519726,
earth:   1,
mars:    1.8808158,
jupiter: 11.862615,
saturn:  29.447498,
uranus:  84.016846,
neptune: 164.79132
}

@doc """
Return the number of years a person that has lived for 'seconds' seconds is
aged on 'planet'.
"""
@spec age_on(planet, pos_integer) :: float
def age_on(planet, seconds),
do: seconds / (@planets[planet] * @orb_period)
end``````

### test/space_age_test.exs

``````defmodule SpageAgeTest do
use ExUnit.Case

# @tag :pending
test "age on Earth" do
input = 1_000_000_000
assert_in_delta 31.69, SpaceAge.age_on(:earth, input), 0.005
end

# @tag :pending
test "age on Mercury" do
input = 2_134_835_688
assert_in_delta 67.65, SpaceAge.age_on(:earth, input), 0.005
assert_in_delta 280.88, SpaceAge.age_on(:mercury, input), 0.005
end

# @tag :pending
test "age on Venus" do
input = 189_839_836
assert_in_delta 6.02, SpaceAge.age_on(:earth, input), 0.005
assert_in_delta 9.78, SpaceAge.age_on(:venus, input), 0.005
end

# @tag :pending
test "age on Mars" do
input = 2_329_871_239
assert_in_delta 73.83, SpaceAge.age_on(:earth, input), 0.005
assert_in_delta 39.25, SpaceAge.age_on(:mars, input), 0.005
end

# @tag :pending
test "age on Jupiter" do
input = 901_876_382
assert_in_delta 28.58, SpaceAge.age_on(:earth, input), 0.005
assert_in_delta 2.41, SpaceAge.age_on(:jupiter, input), 0.005
end

# @tag :pending
test "age on Saturn" do
input = 3_000_000_000
assert_in_delta 95.06, SpaceAge.age_on(:earth, input), 0.005
assert_in_delta 3.23, SpaceAge.age_on(:saturn, input), 0.005
end

# @tag :pending
test "age on Uranus" do
input = 3_210_123_456
assert_in_delta 101.72, SpaceAge.age_on(:earth, input), 0.005
assert_in_delta 1.21, SpaceAge.age_on(:uranus, input), 0.005
end

# @tag :pending
test "age on Neptune" do
input = 8_210_123_456
assert_in_delta 260.16, SpaceAge.age_on(:earth, input), 0.005
assert_in_delta 1.58, SpaceAge.age_on(:neptune, input), 0.005
end
end``````