Skip to content

Commit

Permalink
exercises(space-age): overhaul to use enum (exercism#435)
Browse files Browse the repository at this point in the history
Closes: exercism#405
  • Loading branch information
ee7 authored Jan 11, 2023
1 parent a8ab9c8 commit b770d96
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 26 deletions.
34 changes: 21 additions & 13 deletions exercises/practice/space-age/.meta/example.nim
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import tables
type
Planet* = enum
Mercury
Venus
Earth
Mars
Jupiter
Saturn
Uranus
Neptune

let
times = {
"Earth": 31558149.76,
"Mercury": 7600530.24,
"Venus": 19413907.2,
"Mars": 59354294.4,
"Jupiter": 374335776.0,
"Saturn": 929596608.0,
"Uranus": 2661041808.0,
"Neptune": 5200418592.0
}.toTable
const times = [
Mercury: 7600530.24,
Venus: 19413907.2,
Earth: 31558149.76,
Mars: 59354294.4,
Jupiter: 374335776.0,
Saturn: 929596608.0,
Uranus: 2661041808.0,
Neptune: 5200418592.0
]

proc age*(planet: string, seconds: int64): float =
proc age*(planet: Planet, seconds: int64): float =
seconds.float / times[planet]
1 change: 1 addition & 0 deletions exercises/practice/space-age/.meta/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ description = "age on Neptune"

[57b96e2a-1178-40b7-b34d-f3c9c34e4bf4]
description = "invalid planet causes error"
include = false
22 changes: 9 additions & 13 deletions exercises/practice/space-age/test_space_age.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,29 @@ suite "Space Age":
abs(x - y) < 0.01

test "age on Earth":
check age("Earth", 1_000_000_000) ~= 31.69
check age(Earth, 1_000_000_000) ~= 31.69

test "age on Mercury":
check age("Mercury", 2_134_835_688) ~= 280.88
check age(Mercury, 2_134_835_688) ~= 280.88

test "age on Venus":
check age("Venus", 189_839_836) ~= 9.78
check age(Venus, 189_839_836) ~= 9.78

test "age on Mars":
check age("Mars", 2_129_871_239) ~= 35.88
check age(Mars, 2_129_871_239) ~= 35.88

test "age on Jupiter":
check age("Jupiter", 901_876_382) ~= 2.41
check age(Jupiter, 901_876_382) ~= 2.41

test "age on Saturn":
check age("Saturn", 2_000_000_000) ~= 2.15
check age(Saturn, 2_000_000_000) ~= 2.15

test "age on Uranus":
check age("Uranus", 1_210_123_456) ~= 0.46
check age(Uranus, 1_210_123_456) ~= 0.46

test "age on Neptune":
check age("Neptune", 1_821_023_456) ~= 0.35
check age(Neptune, 1_821_023_456) ~= 0.35

# Bonus
test "age that requires int64":
check age("Saturn", 3_000_000_000) ~= 3.23

test "invalid planet causes error":
expect ValueError:
discard age("Sun", 680_804_807)
check age(Saturn, 3_000_000_000) ~= 3.23

0 comments on commit b770d96

Please sign in to comment.