Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"robot-simulator",
"queen-attack",
"sublist",
"space-age",
"allergies",
"variable-length-quantity",
"phone-number",
Expand Down
4 changes: 4 additions & 0 deletions exercises/space-age/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions exercises/space-age/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[package]
name = "space-age"
version = "0.0.0"
79 changes: 79 additions & 0 deletions exercises/space-age/example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
pub struct Duration {
seconds: f64,
}

impl From<u64> for Duration {
fn from(s: u64) -> Self {
Duration { seconds: s as f64 }
}
}

impl From<f64> 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)
}
}
35 changes: 35 additions & 0 deletions exercises/space-age/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<u64> 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 {}
69 changes: 69 additions & 0 deletions exercises/space-age/tests/space-age.rs
Original file line number Diff line number Diff line change
@@ -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));
}
6 changes: 6 additions & 0 deletions exercises/space-age/topics.md
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions problems.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down