Skip to content

Commit 0c8477b

Browse files
committed
Solution for space age.
1 parent 1ad0861 commit 0c8477b

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
"rna-transcription",
99
"accumulate",
1010
"bob",
11-
"sum-of-multiples",
11+
"sum-of-multiples",
1212
"strain",
1313
"point-mutations",
14+
"space-age",
1415
"anagram",
1516
"nucleotide-count",
1617
"phone-number",

space-age/example.erl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
-module(space_age).
2+
3+
-export([ageOn/2]).
4+
5+
-type planets() :: mercury | venus | earth | mars | jupiter | saturn | uranus | neptune.
6+
-spec ageOn(planets(), integer()) -> float().
7+
ageOn(Planet, Seconds) ->
8+
Seconds / secondsPerYear(Planet).
9+
10+
secondsPerYear (mercury) ->
11+
earthYear() * 0.2408467;
12+
secondsPerYear (venus) ->
13+
earthYear() * 0.61519726;
14+
secondsPerYear (earth) ->
15+
earthYear();
16+
secondsPerYear (mars) ->
17+
earthYear() * 1.8808158;
18+
secondsPerYear (jupiter) ->
19+
earthYear() * 11.862615;
20+
secondsPerYear (saturn) ->
21+
earthYear() * 29.447498;
22+
secondsPerYear (uranus) ->
23+
earthYear() * 84.016846;
24+
secondsPerYear (neptune) ->
25+
earthYear() * 164.79132.
26+
27+
earthYear() ->
28+
365.25 * 24 * 60 * 60.

space-age/space_age_test.erl

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
-module(space_age_test).
2+
3+
-include_lib("eunit/include/eunit.hrl").
4+
5+
age_in_earth_years_test() ->
6+
equalFloat(space_age:ageOn(earth, 1000000000), 31.69).
7+
8+
age_in_mercury_years_test() ->
9+
Seconds = 2134835688,
10+
equalFloat(space_age:ageOn(earth, Seconds), 67.65),
11+
equalFloat(space_age:ageOn(mercury, Seconds), 280.88).
12+
13+
age_in_venus_years_test() ->
14+
Seconds = 189839836,
15+
equalFloat(space_age:ageOn(earth, Seconds), 6.02),
16+
equalFloat(space_age:ageOn(venus, Seconds), 9.78).
17+
18+
age_in_mars_years_test() ->
19+
Seconds = 2329871239,
20+
equalFloat(space_age:ageOn(earth, Seconds), 73.83),
21+
equalFloat(space_age:ageOn(mars, Seconds), 39.25).
22+
23+
age_in_jupiter_years_test() ->
24+
Seconds = 901876382,
25+
equalFloat(space_age:ageOn(earth, Seconds), 28.58),
26+
equalFloat(space_age:ageOn(jupiter, Seconds), 2.41).
27+
28+
age_in_saturn_years_test() ->
29+
Seconds = 3000000000,
30+
equalFloat(space_age:ageOn(earth, Seconds), 95.06),
31+
equalFloat(space_age:ageOn(saturn, Seconds), 3.23).
32+
33+
age_in_uranus_years_test() ->
34+
Seconds = 3210123456,
35+
equalFloat(space_age:ageOn(earth, Seconds), 101.72),
36+
equalFloat(space_age:ageOn(uranus, Seconds), 1.21).
37+
38+
age_in_neptune_years_test() ->
39+
Seconds = 8210123456,
40+
equalFloat(space_age:ageOn(earth, Seconds), 260.16),
41+
equalFloat(space_age:ageOn(neptune, Seconds), 1.58).
42+
43+
equalFloat(A, B) ->
44+
?assertEqual(round(A,2), B).
45+
46+
round(Number, Precision) ->
47+
P = math:pow(10, Precision),
48+
round(Number * P) / P.

0 commit comments

Comments
 (0)