Skip to content

Commit b3a2920

Browse files
authored
Exercise Space Age from PR #37 (author gvrooyen) (#147)
The 11 exercises in PR #37 have been stuck in limbo since last year. They were mostly complete except for the description tags and some minor changes to match the track convention. This PR just includes the update work.
1 parent 8993771 commit b3a2920

File tree

9 files changed

+247
-2
lines changed

9 files changed

+247
-2
lines changed

bin/check-exercise.sh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ set -eou pipefail
66
die () { echo "$*" >&2; exit 1; }
77

88
expected_number_of_tests () {
9-
num_tests=$(grep -c "description = " "${exercise_path}/.meta/tests.toml")
10-
ignored_tests=$(grep -c "include = false" "${exercise_path}/.meta/tests.toml")
9+
num_tests="0"
10+
ignored_tests="0"
11+
if [ -f "${exercise_path}/.meta/tests.toml" ]; then
12+
num_tests=$(grep -c "description = " "${exercise_path}/.meta/tests.toml")
13+
ignored_tests=$(grep -c "include = false" "${exercise_path}/.meta/tests.toml")
14+
fi
1115
echo $(( num_tests - ignored_tests ))
1216
}
1317

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,14 @@
302302
"prerequisites": [],
303303
"difficulty": 2
304304
},
305+
{
306+
"slug": "space-age",
307+
"name": "Space Age",
308+
"uuid": "031f2fe0-e8b1-48c2-8ce3-f9ab8b76423c",
309+
"practices": [],
310+
"prerequisites": [],
311+
"difficulty": 1
312+
},
305313
{
306314
"slug": "triangle",
307315
"name": "Triangle",
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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"authors": [
3+
"gvrooyen"
4+
],
5+
"contributors": [
6+
"rmonnet"
7+
],
8+
"files": {
9+
"solution": [
10+
"space_age.odin"
11+
],
12+
"test": [
13+
"space_age_test.odin"
14+
],
15+
"example": [
16+
".meta/example.odin"
17+
]
18+
},
19+
"blurb": "Given an age in seconds, calculate how old someone is in terms of a given planet's solar years.",
20+
"source": "Partially inspired by Chapter 1 in Chris Pine's online Learn to Program tutorial.",
21+
"source_url": "https://pine.fm/LearnToProgram/?Chapter=01"
22+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package space_age
2+
3+
Planet :: enum {
4+
Mercury,
5+
Venus,
6+
Earth,
7+
Mars,
8+
Jupiter,
9+
Saturn,
10+
Uranus,
11+
Neptune,
12+
}
13+
14+
seconds_per_earth_year :: 31_557_600
15+
16+
age :: proc(planet: Planet, seconds: int) -> f64 {
17+
return f64(seconds) / seconds_per_earth_year / period(planet)
18+
}
19+
20+
period :: proc(p: Planet) -> f64 {
21+
period: f64
22+
switch p {
23+
case .Mercury:
24+
period = 0.2408467
25+
case .Venus:
26+
period = 0.61519726
27+
case .Earth:
28+
period = 1.0
29+
case .Mars:
30+
period = 1.8808158
31+
case .Jupiter:
32+
period = 11.862615
33+
case .Saturn:
34+
period = 29.447498
35+
case .Uranus:
36+
period = 84.016846
37+
case .Neptune:
38+
period = 164.79132
39+
}
40+
return period
41+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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"
38+
include = false
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package space_age
2+
3+
Planet :: enum {
4+
Mercury,
5+
Venus,
6+
Earth,
7+
Mars,
8+
Jupiter,
9+
Saturn,
10+
Uranus,
11+
Neptune,
12+
}
13+
14+
age :: proc(planet: Planet, seconds: int) -> f64 {
15+
// Implement the procedure.
16+
return 0.0
17+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package space_age
2+
3+
import "base:intrinsics"
4+
import "core:log"
5+
import "core:testing"
6+
7+
expect_almost :: proc(
8+
value, expected: $T,
9+
tolerance := 0.01,
10+
loc := #caller_location,
11+
) -> bool where intrinsics.type_is_float(T) {
12+
ok := abs(expected - value) <= tolerance
13+
// Instead of using testing.expect...(), you can also fail a test
14+
// by logging an error.
15+
if !ok {
16+
log.errorf("expected %.2f, got %.2f", expected, value, location = loc)
17+
}
18+
return ok
19+
}
20+
21+
@(test)
22+
/// description = age on Earth
23+
test_age_on_earth :: proc(t: ^testing.T) {
24+
expect_almost(age(.Earth, 1_000_000_000), 31.69)
25+
}
26+
27+
@(test)
28+
/// description = age on Mercury
29+
test_age_on_mercury :: proc(t: ^testing.T) {
30+
expect_almost(age(.Mercury, 2_134_835_688), 280.88)
31+
}
32+
33+
@(test)
34+
/// description = age on Venus
35+
test_age_on_venus :: proc(t: ^testing.T) {
36+
expect_almost(age(.Venus, 189_839_836), 9.78)
37+
}
38+
39+
@(test)
40+
/// description = age on Mars
41+
test_age_on_mars :: proc(t: ^testing.T) {
42+
expect_almost(age(.Mars, 2_129_871_239), 35.88)
43+
}
44+
45+
@(test)
46+
/// description = age on Jupiter
47+
test_age_on_jupiter :: proc(t: ^testing.T) {
48+
expect_almost(age(.Jupiter, 901_876_382), 2.41)
49+
}
50+
51+
@(test)
52+
/// description = age on Saturn
53+
test_age_on_saturn :: proc(t: ^testing.T) {
54+
expect_almost(age(.Saturn, 2_000_000_000), 2.15)
55+
}
56+
57+
@(test)
58+
/// description = age on Uranus
59+
test_age_on_uranus :: proc(t: ^testing.T) {
60+
expect_almost(age(.Uranus, 1_210_123_456), 0.46)
61+
}
62+
63+
@(test)
64+
/// description = age on Neptune
65+
test_age_on_neptune :: proc(t: ^testing.T) {
66+
expect_almost(age(.Neptune, 1_821_023_456), 0.35)
67+
}

0 commit comments

Comments
 (0)