Skip to content

Commit c88a1ab

Browse files
authored
Add space-age (#200)
1 parent f49e157 commit c88a1ab

File tree

7 files changed

+177
-0
lines changed

7 files changed

+177
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,14 @@
242242
"strings"
243243
]
244244
},
245+
{
246+
"slug": "space-age",
247+
"name": "Space Age",
248+
"uuid": "b672d6aa-0b3c-4aa9-8959-dd6f65049508",
249+
"practices": [],
250+
"prerequisites": [],
251+
"difficulty": 3
252+
},
245253
{
246254
"slug": "triangle",
247255
"name": "Triangle",
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Instructions
2+
3+
Given an age in seconds, calculate how old someone would be on:
4+
5+
- Mercury: orbital period 0.2408467 Earth years
6+
- Venus: orbital period 0.61519726 Earth years
7+
- Earth: orbital period 1.0 Earth years, 365.25 Earth days, or 31557600 seconds
8+
- Mars: orbital period 1.8808158 Earth years
9+
- Jupiter: orbital period 11.862615 Earth years
10+
- Saturn: orbital period 29.447498 Earth years
11+
- Uranus: orbital period 84.016846 Earth years
12+
- Neptune: orbital period 164.79132 Earth years
13+
14+
So if you were told someone were 1,000,000,000 seconds old, you should
15+
be able to say that they're 31.69 Earth-years old.
16+
17+
If you're wondering why Pluto didn't make the cut, go watch [this YouTube video][pluto-video].
18+
19+
Note: The actual length of one complete orbit of the Earth around the sun is closer to 365.256 days (1 sidereal year).
20+
The Gregorian calendar has, on average, 365.2425 days.
21+
While not entirely accurate, 365.25 is the value used in this exercise.
22+
See [Year on Wikipedia][year] for more ways to measure a year.
23+
24+
[pluto-video]: https://www.youtube.com/watch?v=Z_2gbGXzFbs
25+
[year]: https://en.wikipedia.org/wiki/Year#Summary
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"BNAndras"
4+
],
5+
"files": {
6+
"solution": [
7+
"space_age.vim"
8+
],
9+
"test": [
10+
"space_age.vader"
11+
],
12+
"example": [
13+
".meta/example.vim"
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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
let s:SecondsInEarthYear = 31557600.0
2+
3+
let s:OrbitalRatios = {
4+
\ 'Mercury': 0.2408467,
5+
\ 'Venus': 0.61519726,
6+
\ 'Earth': 1,
7+
\ 'Mars': 1.8808158,
8+
\ 'Jupiter': 11.862615,
9+
\ 'Saturn': 29.447498,
10+
\ 'Uranus': 84.016846,
11+
\ 'Neptune': 164.79132
12+
\ }
13+
14+
function! Age(planet, seconds) abort
15+
if !has_key(s:OrbitalRatios, a:planet)
16+
throw 'not a planet'
17+
endif
18+
19+
let l:years = a:seconds / s:SecondsInEarthYear
20+
let l:ratio = get(s:OrbitalRatios, a:planet)
21+
return l:years / l:ratio
22+
endfunction
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: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
Before (helper to compare floats):
2+
function! ApproximatelyEqual(a, b) abort
3+
return abs(a:a - a:b) <= 0.01
4+
endfunction
5+
6+
Execute (age on Earth):
7+
let g:planet = "Earth"
8+
let g:seconds = 1000000000
9+
let g:expected = 31.69
10+
Assert ApproximatelyEqual(g:expected, Age(g:planet, g:seconds))
11+
12+
Execute (age on Mercury):
13+
let g:planet = "Mercury"
14+
let g:seconds = 2134835688
15+
let g:expected = 280.88
16+
Assert ApproximatelyEqual(g:expected, Age(g:planet, g:seconds))
17+
18+
Execute (age on Venus):
19+
let g:planet = "Venus"
20+
let g:seconds = 189839836
21+
let g:expected = 9.78
22+
Assert ApproximatelyEqual(g:expected, Age(g:planet, g:seconds))
23+
24+
Execute (age on Mars):
25+
let g:planet = "Mars"
26+
let g:seconds = 2129871239
27+
let g:expected = 35.88
28+
Assert ApproximatelyEqual(g:expected, Age(g:planet, g:seconds))
29+
30+
Execute (age on Jupiter):
31+
let g:planet = "Jupiter"
32+
let g:seconds = 901876382
33+
let g:expected = 2.41
34+
Assert ApproximatelyEqual(g:expected, Age(g:planet, g:seconds))
35+
36+
Execute (age on Saturn):
37+
let g:planet = "Saturn"
38+
let g:seconds = 2000000000
39+
let g:expected = 2.15
40+
Assert ApproximatelyEqual(g:expected, Age(g:planet, g:seconds))
41+
42+
Execute (age on Uranus):
43+
let g:planet = "Uranus"
44+
let g:seconds = 1210123456
45+
let g:expected = 0.46
46+
Assert ApproximatelyEqual(g:expected, Age(g:planet, g:seconds))
47+
48+
Execute (age on Neptune):
49+
let g:planet = "Neptune"
50+
let g:seconds = 1821023456
51+
let g:expected = 0.35
52+
Assert ApproximatelyEqual(g:expected, Age(g:planet, g:seconds))
53+
54+
Execute (invalid planet causes error):
55+
let g:planet = "Sun"
56+
let g:seconds = 680804807
57+
let g:expected = "not a planet"
58+
AssertThrows call Age(g:planet, g:seconds)
59+
AssertEqual g:expected, g:vader_exception
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"
2+
" Returns an age in local years, given a planet name and number of seconds on Earth
3+
" Raises an exception if the name is not a valid planet in the solar system
4+
"
5+
function! Age(planet, seconds) abort
6+
" your code goes here
7+
endfunction

0 commit comments

Comments
 (0)