Skip to content

Commit 70c3a63

Browse files
committed
Add Space Age
1 parent 0bd686d commit 70c3a63

8 files changed

Lines changed: 179 additions & 0 deletions

File tree

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,14 @@
418418
"prerequisites": [],
419419
"difficulty": 3
420420
},
421+
{
422+
"slug": "space-age",
423+
"name": "Space Age",
424+
"uuid": "d7f34974-403b-43c4-8882-48fa710f0983",
425+
"practices": [],
426+
"prerequisites": [],
427+
"difficulty": 2
428+
},
421429
{
422430
"slug": "square-root",
423431
"name": "Square Root",
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Instructions
2+
3+
Given an age in seconds, calculate how old someone would be on a planet in our Solar System.
4+
5+
One Earth year equals 365.25 Earth days, or 31,557,600 seconds.
6+
If you were told someone was 1,000,000,000 seconds old, their age would be 31.69 Earth-years.
7+
8+
For the other planets, you have to account for their orbital period in Earth Years:
9+
10+
| Planet | Orbital period in Earth Years |
11+
| ------- | ----------------------------- |
12+
| Mercury | 0.2408467 |
13+
| Venus | 0.61519726 |
14+
| Earth | 1.0 |
15+
| Mars | 1.8808158 |
16+
| Jupiter | 11.862615 |
17+
| Saturn | 29.447498 |
18+
| Uranus | 84.016846 |
19+
| Neptune | 164.79132 |
20+
21+
~~~~exercism/note
22+
The actual length of one complete orbit of the Earth around the sun is closer to 365.256 days (1 sidereal year).
23+
The Gregorian calendar has, on average, 365.2425 days.
24+
While not entirely accurate, 365.25 is the value used in this exercise.
25+
See [Year on Wikipedia][year] for more ways to measure a year.
26+
27+
[year]: https://en.wikipedia.org/wiki/Year#Summary
28+
~~~~
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Introduction
2+
3+
The year is 2525 and you've just embarked on a journey to visit all planets in the Solar System (Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus and Neptune).
4+
The first stop is Mercury, where customs require you to fill out a form (bureaucracy is apparently _not_ Earth-specific).
5+
As you hand over the form to the customs officer, they scrutinize it and frown.
6+
"Do you _really_ expect me to believe you're just 50 years old?
7+
You must be closer to 200 years old!"
8+
9+
Amused, you wait for the customs officer to start laughing, but they appear to be dead serious.
10+
You realize that you've entered your age in _Earth years_, but the officer expected it in _Mercury years_!
11+
As Mercury's orbital period around the sun is significantly shorter than Earth, you're actually a lot older in Mercury years.
12+
After some quick calculations, you're able to provide your age in Mercury Years.
13+
The customs officer smiles, satisfied, and waves you through.
14+
You make a mental note to pre-calculate your planet-specific age _before_ future customs checks, to avoid such mix-ups.
15+
16+
~~~~exercism/note
17+
If you're wondering why Pluto didn't make the cut, go watch [this YouTube video][pluto-video].
18+
19+
[pluto-video]: https://www.youtube.com/watch?v=Z_2gbGXzFbs
20+
~~~~
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"ricardomaps"
4+
],
5+
"files": {
6+
"solution": [
7+
"space-age.nix"
8+
],
9+
"test": [
10+
"space-age-test.nix"
11+
],
12+
"example": [
13+
".meta/example.nix"
14+
]
15+
},
16+
"blurb": "Given an age in seconds, calculate how old someone is in terms of a given planet's solar years.",
17+
"source": "Partially inspired by Chapter 1 in Chris Pine's online Learn to Program tutorial.",
18+
"source_url": "https://pine.fm/LearnToProgram/chap_01.html"
19+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
let
2+
orbitalPeriods = {
3+
Mercury = 0.2408467;
4+
Venus = 0.61519726;
5+
Earth = 1.0;
6+
Mars = 1.8808158;
7+
Jupiter = 11.862615;
8+
Saturn = 29.447498;
9+
Uranus = 84.016846;
10+
Neptune = 164.79132;
11+
};
12+
in
13+
{
14+
age =
15+
planet: seconds:
16+
if (orbitalPeriods ? ${planet}) then
17+
seconds / (31557600 * orbitalPeriods.${planet})
18+
else
19+
throw "not a planet";
20+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[84f609af-5a91-4d68-90a3-9e32d8a5cd34]
13+
description = "age on Earth"
14+
15+
[ca20c4e9-6054-458c-9312-79679ffab40b]
16+
description = "age on Mercury"
17+
18+
[502c6529-fd1b-41d3-8fab-65e03082b024]
19+
description = "age on Venus"
20+
21+
[9ceadf5e-a0d5-4388-9d40-2c459227ceb8]
22+
description = "age on Mars"
23+
24+
[42927dc3-fe5e-4f76-a5b5-f737fc19bcde]
25+
description = "age on Jupiter"
26+
27+
[8469b332-7837-4ada-b27c-00ee043ebcad]
28+
description = "age on Saturn"
29+
30+
[999354c1-76f8-4bb5-a672-f317b6436743]
31+
description = "age on Uranus"
32+
33+
[80096d30-a0d4-4449-903e-a381178355d8]
34+
description = "age on Neptune"
35+
36+
[57b96e2a-1178-40b7-b34d-f3c9c34e4bf4]
37+
description = "invalid planet causes error"
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
let
2+
inherit (import ./space-age.nix) age;
3+
abs = val: if val < 0 then -val else val;
4+
inDelta = actual: expected: abs (actual - expected) < 0.005;
5+
in
6+
{
7+
"test age on Earth" = {
8+
expr = inDelta (age "Earth" 1000000000) 31.69;
9+
expected = true;
10+
};
11+
"test age on Mercury" = {
12+
expr = inDelta (age "Mercury" 2134835688) 280.88;
13+
expected = true;
14+
};
15+
"test age on Venus" = {
16+
expr = inDelta (age "Venus" 189839836) 9.78;
17+
expected = true;
18+
};
19+
"test age on Mars" = {
20+
expr = inDelta (age "Mars" 2129871239) 35.88;
21+
expected = true;
22+
};
23+
"test age on Jupiter" = {
24+
expr = inDelta (age "Jupiter" 901876382) 2.41;
25+
expected = true;
26+
};
27+
"test age on Saturn" = {
28+
expr = inDelta (age "Saturn" 2000000000) 2.15;
29+
expected = true;
30+
};
31+
"test age on Uranus" = {
32+
expr = inDelta (age "Uranus" 1210123456) 0.46;
33+
expected = true;
34+
};
35+
"test age on Neptune" = {
36+
expr = inDelta (age "Neptune" 1821023456) 0.35;
37+
expected = true;
38+
};
39+
"test invalid planet causes error" = {
40+
expr = age "Sun" 680804807;
41+
expectedError.type = "ThrownError";
42+
expectedError.msg = "not a planet";
43+
};
44+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
age = planet: seconds: throw "You need to implement this function.";
3+
}

0 commit comments

Comments
 (0)