From e2641cc46342feda4fb2d2c466555d659bcda325 Mon Sep 17 00:00:00 2001 From: Eric Willigers Date: Sat, 11 May 2024 15:43:46 +1000 Subject: [PATCH] Add space-age exercise (#380) --- config.json | 8 +++ .../practice/space-age/.docs/instructions.md | 28 ++++++++++ .../practice/space-age/.docs/introduction.md | 20 +++++++ .../practice/space-age/.meta/config.json | 19 +++++++ exercises/practice/space-age/.meta/example.el | 26 +++++++++ exercises/practice/space-age/.meta/tests.toml | 37 +++++++++++++ .../practice/space-age/space-age-test.el | 53 +++++++++++++++++++ exercises/practice/space-age/space-age.el | 14 +++++ 8 files changed, 205 insertions(+) create mode 100644 exercises/practice/space-age/.docs/instructions.md create mode 100644 exercises/practice/space-age/.docs/introduction.md create mode 100644 exercises/practice/space-age/.meta/config.json create mode 100644 exercises/practice/space-age/.meta/example.el create mode 100644 exercises/practice/space-age/.meta/tests.toml create mode 100644 exercises/practice/space-age/space-age-test.el create mode 100644 exercises/practice/space-age/space-age.el diff --git a/config.json b/config.json index a0a5c4ce..5a0fdaa5 100644 --- a/config.json +++ b/config.json @@ -393,6 +393,14 @@ "prerequisites": [], "difficulty": 2 }, + { + "slug": "space-age", + "name": "Space Age", + "uuid": "183eb493-2141-4823-8fe6-0f01ecbcb27a", + "practices": [], + "prerequisites": [], + "difficulty": 2 + }, { "slug": "square-root", "name": "Square Root", diff --git a/exercises/practice/space-age/.docs/instructions.md b/exercises/practice/space-age/.docs/instructions.md new file mode 100644 index 00000000..f23b5e2c --- /dev/null +++ b/exercises/practice/space-age/.docs/instructions.md @@ -0,0 +1,28 @@ +# Instructions + +Given an age in seconds, calculate how old someone would be on a planet in our Solar System. + +One Earth year equals 365.25 Earth days, or 31,557,600 seconds. +If you were told someone was 1,000,000,000 seconds old, their age would be 31.69 Earth-years. + +For the other planets, you have to account for their orbital period in Earth Years: + +| Planet | Orbital period in Earth Years | +| ------- | ----------------------------- | +| Mercury | 0.2408467 | +| Venus | 0.61519726 | +| Earth | 1.0 | +| Mars | 1.8808158 | +| Jupiter | 11.862615 | +| Saturn | 29.447498 | +| Uranus | 84.016846 | +| Neptune | 164.79132 | + +~~~~exercism/note +The actual length of one complete orbit of the Earth around the sun is closer to 365.256 days (1 sidereal year). +The Gregorian calendar has, on average, 365.2425 days. +While not entirely accurate, 365.25 is the value used in this exercise. +See [Year on Wikipedia][year] for more ways to measure a year. + +[year]: https://en.wikipedia.org/wiki/Year#Summary +~~~~ diff --git a/exercises/practice/space-age/.docs/introduction.md b/exercises/practice/space-age/.docs/introduction.md new file mode 100644 index 00000000..014d7885 --- /dev/null +++ b/exercises/practice/space-age/.docs/introduction.md @@ -0,0 +1,20 @@ +# Introduction + +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). +The first stop is Mercury, where customs require you to fill out a form (bureaucracy is apparently _not_ Earth-specific). +As you hand over the form to the customs officer, they scrutinize it and frown. +"Do you _really_ expect me to believe you're just 50 years old? +You must be closer to 200 years old!" + +Amused, you wait for the customs officer to start laughing, but they appear to be dead serious. +You realize that you've entered your age in _Earth years_, but the officer expected it in _Mercury years_! +As Mercury's orbital period around the sun is significantly shorter than Earth, you're actually a lot older in Mercury years. +After some quick calculations, you're able to provide your age in Mercury Years. +The customs officer smiles, satisfied, and waves you through. +You make a mental note to pre-calculate your planet-specific age _before_ future customs checks, to avoid such mix-ups. + +~~~~exercism/note +If you're wondering why Pluto didn't make the cut, go watch [this YouTube video][pluto-video]. + +[pluto-video]: https://www.youtube.com/watch?v=Z_2gbGXzFbs +~~~~ diff --git a/exercises/practice/space-age/.meta/config.json b/exercises/practice/space-age/.meta/config.json new file mode 100644 index 00000000..94990597 --- /dev/null +++ b/exercises/practice/space-age/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "keiravillekode" + ], + "files": { + "solution": [ + "space-age.el" + ], + "test": [ + "space-age-test.el" + ], + "example": [ + ".meta/example.el" + ] + }, + "blurb": "Given an age in seconds, calculate how old someone is in terms of a given planet's solar years.", + "source": "Partially inspired by Chapter 1 in Chris Pine's online Learn to Program tutorial.", + "source_url": "https://pine.fm/LearnToProgram/?Chapter=01" +} diff --git a/exercises/practice/space-age/.meta/example.el b/exercises/practice/space-age/.meta/example.el new file mode 100644 index 00000000..86a4835b --- /dev/null +++ b/exercises/practice/space-age/.meta/example.el @@ -0,0 +1,26 @@ +;;; space-age.el --- Space Age (exercism) -*- lexical-binding: t; -*- + +;;; Commentary: + +;;; Code: + + +(defun orbital-period (planet) + (pcase planet + (:mercury 0.2408467) + (:venus 0.61519726) + (:earth 1.0) + (:mars 1.8808158) + (:jupiter 11.862615) + (:saturn 29.447498) + (:uranus 84.016846) + (:neptune 164.79132) + (_ (error "not a planet")))) + +(defun age (planet seconds) + (/ seconds (* (orbital-period planet) 31557600))) + + +(provide 'space-age) +;;; space-age.el ends here + diff --git a/exercises/practice/space-age/.meta/tests.toml b/exercises/practice/space-age/.meta/tests.toml new file mode 100644 index 00000000..7957bb77 --- /dev/null +++ b/exercises/practice/space-age/.meta/tests.toml @@ -0,0 +1,37 @@ +# This is an auto-generated file. +# +# Regenerating this file via `configlet sync` will: +# - Recreate every `description` key/value pair +# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications +# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) +# - Preserve any other key/value pair +# +# As user-added comments (using the # character) will be removed when this file +# is regenerated, comments can be added via a `comment` key. + +[84f609af-5a91-4d68-90a3-9e32d8a5cd34] +description = "age on Earth" + +[ca20c4e9-6054-458c-9312-79679ffab40b] +description = "age on Mercury" + +[502c6529-fd1b-41d3-8fab-65e03082b024] +description = "age on Venus" + +[9ceadf5e-a0d5-4388-9d40-2c459227ceb8] +description = "age on Mars" + +[42927dc3-fe5e-4f76-a5b5-f737fc19bcde] +description = "age on Jupiter" + +[8469b332-7837-4ada-b27c-00ee043ebcad] +description = "age on Saturn" + +[999354c1-76f8-4bb5-a672-f317b6436743] +description = "age on Uranus" + +[80096d30-a0d4-4449-903e-a381178355d8] +description = "age on Neptune" + +[57b96e2a-1178-40b7-b34d-f3c9c34e4bf4] +description = "invalid planet causes error" diff --git a/exercises/practice/space-age/space-age-test.el b/exercises/practice/space-age/space-age-test.el new file mode 100644 index 00000000..8e99d48f --- /dev/null +++ b/exercises/practice/space-age/space-age-test.el @@ -0,0 +1,53 @@ +;;; space-age-test.el --- Tests for Space Age (exercism) -*- lexical-binding: t; -*- + +;;; Commentary: + +;;; Code: + + +(load-file "space-age.el") +(declare-function age "space-age.el" (planet seconds)) + + +(defmacro should-approximate (expected actual) + (list 'should (list '< (- expected 0.01) actual (+ expected 0.01)))) + + +(ert-deftest age-on-earth () + (should-approximate 31.69 (age :earth 1000000000))) + + +(ert-deftest age-on-mercury () + (should-approximate 280.88 (age :mercury 2134835688))) + + +(ert-deftest age-on-venus () + (should-approximate 9.78 (age :venus 189839836))) + + +(ert-deftest age-on-mars () + (should-approximate 35.88 (age :mars 2129871239))) + + +(ert-deftest age-on-jupiter () + (should-approximate 2.41 (age :jupiter 901876382))) + + +(ert-deftest age-on-saturn () + (should-approximate 2.15 (age :saturn 2000000000))) + + +(ert-deftest age-on-uranus () + (should-approximate 0.46 (age :uranus 1210123456))) + + +(ert-deftest age-on-neptune () + (should-approximate 0.35 (age :neptune 1821023456))) + + +(ert-deftest invalid-planet-causes-error () + (should-error (age :sun 680804807))) + + +(provide 'space-age-test) +;;; space-age-test.el ends here diff --git a/exercises/practice/space-age/space-age.el b/exercises/practice/space-age/space-age.el new file mode 100644 index 00000000..502a22ff --- /dev/null +++ b/exercises/practice/space-age/space-age.el @@ -0,0 +1,14 @@ +;;; space-age.el --- Space Age (exercism) -*- lexical-binding: t; -*- + +;;; Commentary: + +;;; Code: + + +(defun age (planet seconds) + (error "Delete this S-Expression and write your own implementation")) + + +(provide 'space-age) +;;; space-age.el ends here +