From 4dc0b58a63d77381c238ea84ecbf616ebd70bfd8 Mon Sep 17 00:00:00 2001 From: Ian Whitney Date: Wed, 10 Aug 2016 21:43:16 -0500 Subject: [PATCH] Implement Space Age This implementation follows the common test suite. We designed the tests to encourage (force?) students to implement solutions using Traits, and for them to use the From trait to convert numbers to Durations. In a "Real World" implementation of a Planetary Calendar you might not implement this code in this same way. That's fine! Exercism isn't here to model Real Production Code. It's here to help students learn about language, design and tradeoffs. By focusing on Traits this problem will hopefully illustrate how Rust handles design -- no Inheritance, just Composition via Traits -- and minimizing code duplication. We've placed this problem after Queen Attack and Roman Numerals. If the student has gone through the problems in order then they've covered Traits (Roman Numerals, and maybe in Queen Attack). Custom traits may be new, depending on how they handled Queen Attack. I think providing a default implementation for a trait function will be new, but the stub file makes it pretty clear what's going on there. I don't think it's a big leap to add a trait function that will be implemented differently for each implementation of Planet. But we'll see what students make of it. We're also trying something new, adding a topics.md file that will list what parts of Rust the problem & solution focus on Lengthy discussion of how this problem was implemented can be found at https://github.com/exercism/xrust/pull/177 --- config.json | 1 + exercises/space-age/Cargo.lock | 4 ++ exercises/space-age/Cargo.toml | 3 + exercises/space-age/example.rs | 79 ++++++++++++++++++++++++++ exercises/space-age/src/lib.rs | 35 ++++++++++++ exercises/space-age/tests/space-age.rs | 69 ++++++++++++++++++++++ exercises/space-age/topics.md | 6 ++ problems.md | 1 + 8 files changed, 198 insertions(+) create mode 100644 exercises/space-age/Cargo.lock create mode 100644 exercises/space-age/Cargo.toml create mode 100644 exercises/space-age/example.rs create mode 100644 exercises/space-age/src/lib.rs create mode 100644 exercises/space-age/tests/space-age.rs create mode 100644 exercises/space-age/topics.md diff --git a/config.json b/config.json index 7dc0f5ee2..a90b1f05f 100644 --- a/config.json +++ b/config.json @@ -26,6 +26,7 @@ "robot-simulator", "queen-attack", "sublist", + "space-age", "allergies", "variable-length-quantity", "phone-number", diff --git a/exercises/space-age/Cargo.lock b/exercises/space-age/Cargo.lock new file mode 100644 index 000000000..db1f72d3e --- /dev/null +++ b/exercises/space-age/Cargo.lock @@ -0,0 +1,4 @@ +[root] +name = "space-age" +version = "0.0.0" + diff --git a/exercises/space-age/Cargo.toml b/exercises/space-age/Cargo.toml new file mode 100644 index 000000000..f39f06650 --- /dev/null +++ b/exercises/space-age/Cargo.toml @@ -0,0 +1,3 @@ +[package] +name = "space-age" +version = "0.0.0" diff --git a/exercises/space-age/example.rs b/exercises/space-age/example.rs new file mode 100644 index 000000000..94824e59e --- /dev/null +++ b/exercises/space-age/example.rs @@ -0,0 +1,79 @@ +pub struct Duration { + seconds: f64, +} + +impl From for Duration { + fn from(s: u64) -> Self { + Duration { seconds: s as f64 } + } +} + +impl From for Duration { + fn from(s: f64) -> Self { + Duration { seconds: s } + } +} + +pub trait Planet { + fn orbital_duration() -> Duration; + fn years_during(d: &Duration) -> f64 { + d.seconds / Self::orbital_duration().seconds + } +} + +pub struct Mercury; +pub struct Venus; +pub struct Earth; +pub struct Mars; +pub struct Jupiter; +pub struct Saturn; +pub struct Uranus; +pub struct Neptune; + +impl Planet for Mercury { + fn orbital_duration() -> Duration { + Duration::from(7600543.81992) + } +} + +impl Planet for Venus { + fn orbital_duration() -> Duration { + Duration::from(19414149.052176) + } +} + +impl Planet for Earth { + fn orbital_duration() -> Duration { + Duration::from(31557600) + } +} + +impl Planet for Mars { + fn orbital_duration() -> Duration { + Duration::from(59354032.69008) + } +} + +impl Planet for Jupiter { + fn orbital_duration() -> Duration { + Duration::from(374355659.124) + } +} + +impl Planet for Saturn { + fn orbital_duration() -> Duration { + Duration::from(929292362.8848) + } +} + +impl Planet for Uranus { + fn orbital_duration() -> Duration { + Duration::from(2651370019.3296) + } +} + +impl Planet for Neptune { + fn orbital_duration() -> Duration { + Duration::from(5200418560.032) + } +} diff --git a/exercises/space-age/src/lib.rs b/exercises/space-age/src/lib.rs new file mode 100644 index 000000000..896bfab2b --- /dev/null +++ b/exercises/space-age/src/lib.rs @@ -0,0 +1,35 @@ +// The code below is a stub. Just enough to satisfy the compiler. +// In order to pass the tests you can add-to or change any of this code. +#![allow(unused_variables)] + +pub struct Duration; + +impl From for Duration { + fn from(s: u64) -> Self { + unimplemented!() + } +} + +pub trait Planet { + fn years_during(d: &Duration) -> f64 { + unimplemented!(); + } +} + +pub struct Mercury; +pub struct Venus; +pub struct Earth; +pub struct Mars; +pub struct Jupiter; +pub struct Saturn; +pub struct Uranus; +pub struct Neptune; + +impl Planet for Mercury {} +impl Planet for Venus {} +impl Planet for Earth {} +impl Planet for Mars {} +impl Planet for Jupiter {} +impl Planet for Saturn {} +impl Planet for Uranus {} +impl Planet for Neptune {} diff --git a/exercises/space-age/tests/space-age.rs b/exercises/space-age/tests/space-age.rs new file mode 100644 index 000000000..42fb3a0f2 --- /dev/null +++ b/exercises/space-age/tests/space-age.rs @@ -0,0 +1,69 @@ +extern crate space_age; + +use space_age::*; + +fn assert_in_delta(expected: f64, actual: f64) { + let diff: f64 = expected - actual.abs(); + let delta: f64 = 0.01; + if diff > delta { + panic!("Your result of {} should be within {} of the expected result {}", + actual, + delta, + expected) + } +} + +#[test] +fn earth_age() { + let duration = Duration::from(1_000_000_000); + assert_in_delta(31.69, Earth::years_during(&duration)); +} + +#[test] +#[ignore] +fn mercury_age() { + let duration = Duration::from(2_134_835_688); + assert_in_delta(280.88, Mercury::years_during(&duration)); +} + +#[test] +#[ignore] +fn venus_age() { + let duration = Duration::from(189_839_836); + assert_in_delta(9.78, Venus::years_during(&duration)); +} + +#[test] +#[ignore] +fn mars_age() { + let duration = Duration::from(2_329_871_239); + assert_in_delta(39.25, Mars::years_during(&duration)); +} + +#[test] +#[ignore] +fn jupiter_age() { + let duration = Duration::from(901_876_382); + assert_in_delta(2.41, Jupiter::years_during(&duration)); +} + +#[test] +#[ignore] +fn saturn_age() { + let duration = Duration::from(3_000_000_000); + assert_in_delta(3.23, Saturn::years_during(&duration)); +} + +#[test] +#[ignore] +fn uranus_age() { + let duration = Duration::from(3_210_123_456); + assert_in_delta(1.21, Uranus::years_during(&duration)); +} + +#[test] +#[ignore] +fn neptune_age() { + let duration = Duration::from(8_210_123_456); + assert_in_delta(1.58, Neptune::years_during(&duration)); +} diff --git a/exercises/space-age/topics.md b/exercises/space-age/topics.md new file mode 100644 index 000000000..6354fd9db --- /dev/null +++ b/exercises/space-age/topics.md @@ -0,0 +1,6 @@ +# Topics + +Some Rust topics you may want to read about while solving this problem: + +- Traits, both the From trait and implementing your own traits +- Default method implementations for traits diff --git a/problems.md b/problems.md index f46c5bb9f..6fd8a35e9 100644 --- a/problems.md +++ b/problems.md @@ -45,6 +45,7 @@ tournament | enum, sorting, hashmap, structs robot-simulator | Immutability, enum queen-attack | struct, trait (optional), Result sublist | enum, generic over type +space-age | Custom Trait, From Trait, Default Trait implementation allergies | struct, enum, bitwise (probably), vectors, filter variable-length-quantity | Encodings, slices, bitwise, Result phone-number | option, format, unwrap_or, iters, match