Space Age

1. Readme

地球年

给定年龄(以秒为单位),计算某人的年龄:

  • 地球:轨道周期 365.25 地球日,或 31557600 秒
  • 水星:轨道周期 0.2408467 地球年
  • 金星:轨道周期 0.61519726 地球年
  • 火星:轨道周期 1.8808158 地球年
  • 木星:轨道周期 11.862615 地球年
  • 土星:轨道周期 29.447498 地球年
  • 天王星:轨道周期 84.016846 地球年
  • 海王星:轨道周期 164.79132 地球年

因此,如果你被告知某人的年龄为 1,000,000,000 秒,你应该可以说它们的年龄为 31.69 地球年.

如果你想知道为什么冥王星没有加入进来,请去观看this youtube video.

主题

在解决此问题时,您可能想要阅读的一些 Rust 主题:

  • Trait,用 From trait 实现,或实现自己的 trait
  • trait 的默认方法实现

资源

部分灵感来自 Chris Pine 在线学习编程教程的第 1 章.http://pine.fm/LearnToProgram/?Chapter=01

2. 开始你的表演

// 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.

#[derive(Debug)]
pub struct Duration;

impl From<u64> for Duration {
   fn from(s: u64) -> Self {
       unimplemented!("s, measured in seconds: {}", s)
   }
}

pub trait Planet {
   fn years_during(d: &Duration) -> f64 {
       unimplemented!(
           "convert a duration ({:?}) to the number of years on this planet for that duration",
           d,
       );
   }
}

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 {}

3. 测试代码查看


# #![allow(unused_variables)]
#fn main() {
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));
}

#}

4. 答案


# #![allow(unused_variables)]
#fn main() {
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)
   }
}

#}



填充/相关