Skip to content

Commit 031ecb1

Browse files
authored
Add practice exercise: space-age (#107)
1 parent 943b42b commit 031ecb1

File tree

11 files changed

+232
-0
lines changed

11 files changed

+232
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,14 @@
122122
"prerequisites": [],
123123
"difficulty": 2
124124
},
125+
{
126+
"slug": "space-age",
127+
"name": "Space Age",
128+
"uuid": "09b96007-c782-4b5e-b929-48bab73b5f69",
129+
"practices": [],
130+
"prerequisites": [],
131+
"difficulty": 2
132+
},
125133
{
126134
"slug": "allergies",
127135
"name": "Allergies",
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+
"jimmytty"
4+
],
5+
"files": {
6+
"solution": [
7+
"space-age.sql"
8+
],
9+
"test": [
10+
"space-age_test.sql"
11+
],
12+
"example": [
13+
".meta/example.sql"
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/?Chapter=01"
19+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
DROP TABLE IF EXISTS planets;
2+
CREATE TEMPORARY TABLE planets (
3+
name TEXT UNIQUE NOT NULL,
4+
period REAL NOT NULL
5+
);
6+
INSERT INTO planets (name, period)
7+
VALUES ('Mercury', 0.2408467 ),
8+
('Venus' , 0.61519726),
9+
('Earth' , 1.0 ),
10+
('Mars' , 1.8808158 ),
11+
('Jupiter', 11.862615 ),
12+
('Saturn' , 29.447498 ),
13+
('Uranus' , 84.016846 ),
14+
('Neptune', 164.79132 );
15+
16+
UPDATE "space-age"
17+
SET result = ROUND("space-age".seconds / 31557600.0 / planets.period, 2)
18+
FROM planets
19+
WHERE "space-age".planet = planets.name
20+
AND "space-age".result ISNULL
21+
;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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
39+
comment = "error testing omitted on purpose"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
DROP TABLE IF EXISTS "space-age";
2+
CREATE TABLE "space-age" (
3+
planet TEXT NOT NULL,
4+
seconds INTEGER NOT NULL,
5+
result REAL
6+
);
7+
8+
.mode csv
9+
.import ./data.csv "space-age"
10+
11+
UPDATE "space-age" SET result = NULL;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
DROP TABLE IF EXISTS tests;
2+
CREATE TABLE IF NOT EXISTS tests (
3+
-- uuid and description are taken from the test.toml file
4+
uuid TEXT PRIMARY KEY,
5+
description TEXT NOT NULL,
6+
-- The following section is needed by the online test-runner
7+
status TEXT DEFAULT 'fail',
8+
message TEXT,
9+
output TEXT,
10+
test_code TEXT,
11+
task_id INTEGER DEFAULT NULL,
12+
-- Here are columns for the actual tests
13+
planet TEXT NOT NULL,
14+
seconds INTEGER NOT NULL,
15+
expected REAL
16+
);
17+
18+
INSERT INTO tests (uuid, description, planet, seconds, expected)
19+
VALUES
20+
('84f609af-5a91-4d68-90a3-9e32d8a5cd34','age on Earth','Earth',1000000000,31.69),
21+
('ca20c4e9-6054-458c-9312-79679ffab40b','age on Mercury','Mercury',2134835688,280.88),
22+
('502c6529-fd1b-41d3-8fab-65e03082b024','age on Venus','Venus',189839836,9.78),
23+
('9ceadf5e-a0d5-4388-9d40-2c459227ceb8','age on Mars','Mars',2129871239,35.88),
24+
('42927dc3-fe5e-4f76-a5b5-f737fc19bcde','age on Jupiter','Jupiter',901876382,2.41),
25+
('8469b332-7837-4ada-b27c-00ee043ebcad','age on Saturn','Saturn',2000000000,2.15),
26+
('999354c1-76f8-4bb5-a672-f317b6436743','age on Uranus','Uranus',1210123456,0.46),
27+
('80096d30-a0d4-4449-903e-a381178355d8','age on Neptune','Neptune',1821023456,0.35);
28+

exercises/practice/space-age/data.csv

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"Earth",1000000000,""
2+
"Mercury",2134835688,""
3+
"Venus",189839836,""
4+
"Mars",2129871239,""
5+
"Jupiter",901876382,""
6+
"Saturn",2000000000,""
7+
"Uranus",1210123456,""
8+
"Neptune",1821023456,""
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- Schema: CREATE TABLE "space-age" (
2+
-- planet TEXT NOT NULL,
3+
-- seconds INTEGER NOT NULL,
4+
-- result REAL
5+
-- );
6+
-- Task: update the space-age table and set the result based on planet
7+
-- and seconds.

0 commit comments

Comments
 (0)