Skip to content
This repository has been archived by the owner on May 30, 2023. It is now read-only.

Commit

Permalink
Add space-age exercise (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom authored May 7, 2023
1 parent 1a32636 commit 6ed4087
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 0 deletions.
13 changes: 13 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,19 @@
],
"difficulty": 2
},
{
"slug": "space-age",
"name": "Space Age",
"uuid": "f43676b5-ab9c-4cc8-9159-a30401deaa29",
"practices": [
"floating-point-numbers"
],
"prerequisites": [
"numbers",
"floating-point-numbers"
],
"difficulty": 2
},
{
"slug": "collatz-conjecture",
"name": "Collatz Conjecture",
Expand Down
25 changes: 25 additions & 0 deletions exercises/practice/space-age/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Instructions

Given an age in seconds, calculate how old someone would be on:

- Mercury: orbital period 0.2408467 Earth years
- Venus: orbital period 0.61519726 Earth years
- Earth: orbital period 1.0 Earth years, 365.25 Earth days, or 31557600 seconds
- Mars: orbital period 1.8808158 Earth years
- Jupiter: orbital period 11.862615 Earth years
- Saturn: orbital period 29.447498 Earth years
- Uranus: orbital period 84.016846 Earth years
- Neptune: orbital period 164.79132 Earth years

So if you were told someone were 1,000,000,000 seconds old, you should
be able to say that they're 31.69 Earth-years old.

If you're wondering why Pluto didn't make the cut, go watch [this YouTube video][pluto-video].

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.

[pluto-video]: https://www.youtube.com/watch?v=Z_2gbGXzFbs
[year]: https://en.wikipedia.org/wiki/Year#Summary
19 changes: 19 additions & 0 deletions exercises/practice/space-age/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"ErikSchierboom"
],
"files": {
"solution": [
"src/space_age.cljs"
],
"test": [
"test/space_age_test.cljs"
],
"example": [
".meta/src/example.cljs"
]
},
"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"
}
15 changes: 15 additions & 0 deletions exercises/practice/space-age/.meta/src/example.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(ns space-age)

(def earth-year-seconds (* 365.25 24 60 60))

(defn- on-planet [seconds orbital-period]
(/ seconds earth-year-seconds orbital-period))

(defn on-earth [seconds] (on-planet seconds 1.0))
(defn on-mercury [seconds] (on-planet seconds 0.2408467))
(defn on-venus [seconds] (on-planet seconds 0.61519726))
(defn on-mars [seconds] (on-planet seconds 1.8808158))
(defn on-jupiter [seconds] (on-planet seconds 11.862615))
(defn on-saturn [seconds] (on-planet seconds 29.447498))
(defn on-uranus [seconds] (on-planet seconds 84.016846))
(defn on-neptune [seconds] (on-planet seconds 164.79132))
27 changes: 27 additions & 0 deletions exercises/practice/space-age/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# This is an auto-generated file. Regular comments will be removed when this
# file is regenerated. Regenerating will not touch any manually added keys,
# so comments can be added in 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"
10 changes: 10 additions & 0 deletions exercises/practice/space-age/deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{:deps
{org.clojure/clojure {:mvn/version "1.10.1"}
org.clojure/clojurescript {:mvn/version "1.10.773"}}

:aliases
{:test
{:extra-paths ["test"]
:extra-deps
{olical/cljs-test-runner {:mvn/version "3.8.0"}}
:main-opts ["-m" "cljs-test-runner.main"]}}}
33 changes: 33 additions & 0 deletions exercises/practice/space-age/src/space_age.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
(ns space-age)

(defn on-mercury [] ;; <- arglist goes here
;; your code goes here
)

(defn on-venus [] ;; <- arglist goes here
;; your code goes here
)

(defn on-earth [] ;; <- arglist goes here
;; your code goes here
)

(defn on-mars [] ;; <- arglist goes here
;; your code goes here
)

(defn on-jupiter [] ;; <- arglist goes here
;; your code goes here
)

(defn on-saturn [] ;; <- arglist goes here
;; your code goes here
)

(defn on-uranus [] ;; <- arglist goes here
;; your code goes here
)

(defn on-neptune [] ;; <- arglist goes here
;; your code goes here
)
46 changes: 46 additions & 0 deletions exercises/practice/space-age/test/space_age_test.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
(ns space-age-test
(:require [clojure.test :refer [deftest is]]
space-age))

(defn- rounds-to
[expected actual]
(is (= (Math/round (* 100.0 expected))
(Math/round (* 100.0 actual)))))

(deftest age-in-earth-years
(rounds-to 31.69 (space-age/on-earth 1000000000)))

(deftest age-in-mercury-years
(let [seconds 2134835688]
(rounds-to 67.65 (space-age/on-earth seconds))
(rounds-to 280.88 (space-age/on-mercury seconds))))

(deftest age-in-venus-years
(let [seconds 189839836]
(rounds-to 6.02 (space-age/on-earth seconds))
(rounds-to 9.78 (space-age/on-venus seconds))))

(deftest age-on-mars
(let [seconds 2329871239]
(rounds-to 73.83 (space-age/on-earth seconds))
(rounds-to 39.25 (space-age/on-mars seconds))))

(deftest age-on-jupiter
(let [seconds 901876382]
(rounds-to 28.58 (space-age/on-earth seconds))
(rounds-to 2.41 (space-age/on-jupiter seconds))))

(deftest age-on-saturn
(let [seconds 3000000000]
(rounds-to 95.06 (space-age/on-earth seconds))
(rounds-to 3.23 (space-age/on-saturn seconds))))

(deftest age-on-uranus
(let [seconds 3210123456]
(rounds-to 101.72 (space-age/on-earth seconds))
(rounds-to 1.21 (space-age/on-uranus seconds))))

(deftest age-on-neptune
(let [seconds 8210123456]
(rounds-to 260.16 (space-age/on-earth seconds))
(rounds-to 1.58 (space-age/on-neptune seconds))))

0 comments on commit 6ed4087

Please sign in to comment.