Skip to content

Commit

Permalink
Update space-age with units and quantities
Browse files Browse the repository at this point in the history
  • Loading branch information
BNAndras committed Dec 10, 2024
1 parent f39b945 commit ea3e136
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 44 deletions.
40 changes: 21 additions & 19 deletions exercises/practice/space-age/.meta/src/example.art
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
earthOrbitLength: 31557600.0
specify 'earthYear 31557600`s
specify 'mercuryYear 0.2408467`earthYear
specify 'venusYear 0.61519726`earthYear
specify 'marsYear 1.8808158`earthYear
specify 'jupiterYear 11.862615`earthYear
specify 'saturnYear 29.447498`earthYear
specify 'uranusYear 84.016846`earthYear
specify 'neptuneYear 164.79132`earthYear

orbitRatios: #[
earth: 1.0
mercury: 0.2408467
venus: 0.61519726
mars: 1.8808158
jupiter: 11.862615
saturn: 29.447498
uranus: 84.016846
neptune: 164.79132
ageOn: function [planet :literal seconds :quantity][
earthYears: in`earthYear seconds
case planet [
'earth -> earthYears
'mercury -> convert earthYears `mercuryYear
'venus -> convert earthYears `venusYear
'mars -> convert earthYears `marsYear
'jupiter -> convert earthYears `jupiterYear
'saturn -> convert earthYears `saturnYear
'uranus -> convert earthYears `uranusYear
'neptune -> convert earthYears `neptuneYear
any -> null
]
]

ageOn: function [planet seconds][
planet: lower planet
earthAge: seconds / earthOrbitLength
switch key? orbitRatios planet
-> earthAge / orbitRatios\[planet]
-> null
]

7 changes: 5 additions & 2 deletions exercises/practice/space-age/src/space-age.art
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
ageOn: function [planet seconds][
; Define your custom units here to represent each planet's year
; Be careful. We're defining a year on Earth as 31557600 seconds,
; but the built-in `yr unit uses 31556952 seconds.

ageOn: function [planet :literal seconds :quantity][
panic "Please implement the ageOn function"
]

54 changes: 31 additions & 23 deletions exercises/practice/space-age/tests/test-space-age.art
Original file line number Diff line number Diff line change
@@ -1,56 +1,64 @@
import {unitt}!
import {src/space-age}!

approximately?: function [value target][
lower: value - 0.01
upper: value + 0.01
between? target lower upper
]
approximatelyEqual?: function [value :floating target :floating][
lower: value - 0.01
upper: value + 0.01

between? target lower upper
]

suite "Space Age" [
test "age on Earth" [
result: ageOn "Earth" 1000000000
assert -> approximately? result 31.69
result: to :floating ageOn 'earth 1000000000`s
expected: to :floating 31.69`earthYear
assert -> approximatelyEqual? result expected
]

test.skip "age on Mercury" [
result: ageOn "Mercury" 2134835688
assert -> approximately? result 280.88
result: to :floating ageOn 'mercury 2134835688`s
expected: to :floating 280.88`mercuryYear
assert -> approximatelyEqual? result expected
]

test.skip "age on Venus" [
result: ageOn "Venus" 189839836
assert -> approximately? result 9.78
result: to :floating ageOn 'venus 189839836`s
expected: to :floating 9.78`venusYear
assert -> approximatelyEqual? result expected
]

test.skip "age on Mars" [
result: ageOn "Mars" 2129871239
assert -> approximately? result 35.88
result: to :floating ageOn 'mars 2129871239`s
expected: to :floating 35.88`marsYear
assert -> approximatelyEqual? result expected
]

test.skip "age on Jupiter" [
result: ageOn "Jupiter" 901876382
assert -> approximately? result 2.41
result: to :floating ageOn 'jupiter 901876382`s
expected: to :floating 2.41`jupiterYear
assert -> approximatelyEqual? result expected
]

test.skip "age on Saturn" [
result: ageOn "Saturn" 2000000000
assert -> approximately? result 2.15
result: to :floating ageOn 'saturn 2000000000`s
expected: to :floating 2.15`saturnYear
assert -> approximatelyEqual? result expected
]

test.skip "age on Uranus" [
result: ageOn "Uranus" 1210123456
assert -> approximately? result 0.46
result: to :floating ageOn 'uranus 1210123456`s
expected: to :floating 0.46`uranusYear
assert -> approximatelyEqual? result expected
]

test.skip "age on Neptune" [
result: ageOn "Neptune" 1821023456
assert -> approximately? result 0.35
result: to :floating ageOn 'neptune 1821023456`s
expected: to :floating 0.35`neptuneYear
assert -> approximatelyEqual? result expected
]

test.skip "invalid planet returns null" [
result: ageOn "Sun" 680804807
result: ageOn 'sun 680804807`s
assert -> null? result
]
]

0 comments on commit ea3e136

Please sign in to comment.