Exercism track submissions
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

space age

+328
+39
rust/space-age/.exercism/config.json
··· 1 + { 2 + "blurb": "Given an age in seconds, calculate how old someone is in terms of a given planet's solar years.", 3 + "authors": [ 4 + "IanWhitney" 5 + ], 6 + "contributors": [ 7 + "ashleygwilliams", 8 + "bobahop", 9 + "coriolinus", 10 + "cwhakes", 11 + "durka", 12 + "eddyp", 13 + "efx", 14 + "ErikSchierboom", 15 + "IanWhitney", 16 + "joshgoebel", 17 + "lutostag", 18 + "nfiles", 19 + "ocstl", 20 + "petertseng", 21 + "rofrol", 22 + "stringparser", 23 + "xakon", 24 + "ZapAnton" 25 + ], 26 + "files": { 27 + "solution": [ 28 + "src/lib.rs" 29 + ], 30 + "test": [ 31 + "tests/space-age.rs" 32 + ], 33 + "example": [ 34 + ".meta/example.rs" 35 + ] 36 + }, 37 + "source": "Partially inspired by Chapter 1 in Chris Pine's online Learn to Program tutorial.", 38 + "source_url": "http://pine.fm/LearnToProgram/?Chapter=01" 39 + }
+8
rust/space-age/.gitignore
··· 1 + # Generated by Cargo 2 + # will have compiled files and executables 3 + /target/ 4 + **/*.rs.bk 5 + 6 + # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries 7 + # More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock 8 + Cargo.lock
+4
rust/space-age/Cargo.toml
··· 1 + [package] 2 + edition = "2021" 3 + name = "space-age" 4 + version = "1.2.0"
+85
rust/space-age/HELP.md
··· 1 + # Help 2 + 3 + ## Running the tests 4 + 5 + Execute the tests with: 6 + 7 + ```bash 8 + $ cargo test 9 + ``` 10 + 11 + All but the first test have been ignored. After you get the first test to 12 + pass, open the tests source file which is located in the `tests` directory 13 + and remove the `#[ignore]` flag from the next test and get the tests to pass 14 + again. Each separate test is a function with `#[test]` flag above it. 15 + Continue, until you pass every test. 16 + 17 + If you wish to run _only ignored_ tests without editing the tests source file, use: 18 + 19 + ```bash 20 + $ cargo test -- --ignored 21 + ``` 22 + 23 + If you are using Rust 1.51 or later, you can run _all_ tests with 24 + 25 + ```bash 26 + $ cargo test -- --include-ignored 27 + ``` 28 + 29 + To run a specific test, for example `some_test`, you can use: 30 + 31 + ```bash 32 + $ cargo test some_test 33 + ``` 34 + 35 + If the specific test is ignored, use: 36 + 37 + ```bash 38 + $ cargo test some_test -- --ignored 39 + ``` 40 + 41 + To learn more about Rust tests refer to the online [test documentation][rust-tests]. 42 + 43 + [rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html 44 + 45 + ## Submitting your solution 46 + 47 + You can submit your solution using the `exercism submit src/lib.rs` command. 48 + This command will upload your solution to the Exercism website and print the solution page's URL. 49 + 50 + It's possible to submit an incomplete solution which allows you to: 51 + 52 + - See how others have completed the exercise 53 + - Request help from a mentor 54 + 55 + ## Need to get help? 56 + 57 + If you'd like help solving the exercise, check the following pages: 58 + 59 + - The [Rust track's documentation](https://exercism.org/docs/tracks/rust) 60 + - [Exercism's support channel on gitter](https://gitter.im/exercism/support) 61 + - The [Frequently Asked Questions](https://exercism.org/docs/using/faqs) 62 + 63 + Should those resources not suffice, you could submit your (incomplete) solution to request mentoring. 64 + 65 + ## Rust Installation 66 + 67 + Refer to the [exercism help page][help-page] for Rust installation and learning 68 + resources. 69 + 70 + ## Submitting the solution 71 + 72 + Generally you should submit all files in which you implemented your solution (`src/lib.rs` in most cases). If you are using any external crates, please consider submitting the `Cargo.toml` file. This will make the review process faster and clearer. 73 + 74 + ## Feedback, Issues, Pull Requests 75 + 76 + The GitHub [track repository][github] is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help! 77 + 78 + If you want to know more about Exercism, take a look at the [contribution guide]. 79 + 80 + ## Submitting Incomplete Solutions 81 + It's possible to submit an incomplete solution so you can see how others have completed the exercise. 82 + 83 + [help-page]: https://exercism.io/tracks/rust/learning 84 + [github]: https://github.com/exercism/rust 85 + [contribution guide]: https://exercism.io/docs/community/contributors
+69
rust/space-age/README.md
··· 1 + # Space Age 2 + 3 + Welcome to Space Age on Exercism's Rust Track. 4 + If you need help running the tests or submitting your code, check out `HELP.md`. 5 + 6 + ## Instructions 7 + 8 + Given an age in seconds, calculate how old someone would be on: 9 + 10 + - Mercury: orbital period 0.2408467 Earth years 11 + - Venus: orbital period 0.61519726 Earth years 12 + - Earth: orbital period 1.0 Earth years, 365.25 Earth days, or 31557600 seconds 13 + - Mars: orbital period 1.8808158 Earth years 14 + - Jupiter: orbital period 11.862615 Earth years 15 + - Saturn: orbital period 29.447498 Earth years 16 + - Uranus: orbital period 84.016846 Earth years 17 + - Neptune: orbital period 164.79132 Earth years 18 + 19 + So if you were told someone were 1,000,000,000 seconds old, you should 20 + be able to say that they're 31.69 Earth-years old. 21 + 22 + If you're wondering why Pluto didn't make the cut, go watch [this 23 + youtube video](http://www.youtube.com/watch?v=Z_2gbGXzFbs). 24 + 25 + Some Rust topics you may want to read about while solving this problem: 26 + 27 + - Traits, both the From trait and implementing your own traits 28 + - Default method implementations for traits 29 + - Macros, the use of a macro could reduce boilerplate and increase readability 30 + for this exercise. For instance, 31 + [a macro can implement a trait for multiple types at once](https://stackoverflow.com/questions/39150216/implementing-a-trait-for-multiple-types-at-once), 32 + though it is fine to implement `years_during` in the Planet trait itself. A macro could 33 + define both the structs and their implementations. Info to get started with macros can 34 + be found at: 35 + 36 + - [The Macros chapter in The Rust Programming Language](https://doc.rust-lang.org/stable/book/ch19-06-macros.html) 37 + - [an older version of the Macros chapter with helpful detail](https://doc.rust-lang.org/1.30.0/book/first-edition/macros.html) 38 + - [Rust By Example](https://doc.rust-lang.org/stable/rust-by-example/macros.html) 39 + 40 + ## Source 41 + 42 + ### Created by 43 + 44 + - @IanWhitney 45 + 46 + ### Contributed to by 47 + 48 + - @ashleygwilliams 49 + - @bobahop 50 + - @coriolinus 51 + - @cwhakes 52 + - @durka 53 + - @eddyp 54 + - @efx 55 + - @ErikSchierboom 56 + - @IanWhitney 57 + - @joshgoebel 58 + - @lutostag 59 + - @nfiles 60 + - @ocstl 61 + - @petertseng 62 + - @rofrol 63 + - @stringparser 64 + - @xakon 65 + - @ZapAnton 66 + 67 + ### Based on 68 + 69 + Partially inspired by Chapter 1 in Chris Pine's online Learn to Program tutorial. - http://pine.fm/LearnToProgram/?Chapter=01
+63
rust/space-age/src/lib.rs
··· 1 + // The code below is a stub. Just enough to satisfy the compiler. 2 + // In order to pass the tests you can add-to or change any of this code. 3 + 4 + #[derive(Debug)] 5 + pub struct Duration(f64); 6 + 7 + impl From<u64> for Duration { 8 + fn from(s: u64) -> Self { 9 + Duration(((s as f64 / 31557600_f64) * 100.0).round() / 100.0) 10 + } 11 + } 12 + 13 + pub trait Planet { 14 + fn years_during(d: &Duration) -> f64 { 15 + d.0 16 + } 17 + } 18 + 19 + pub struct Mercury; 20 + pub struct Venus; 21 + pub struct Earth; 22 + pub struct Mars; 23 + pub struct Jupiter; 24 + pub struct Saturn; 25 + pub struct Uranus; 26 + pub struct Neptune; 27 + 28 + impl Planet for Mercury { 29 + fn years_during(d: &Duration) -> f64 { 30 + d.0 / 0.2408467 31 + } 32 + } 33 + impl Planet for Venus { 34 + fn years_during(d: &Duration) -> f64 { 35 + d.0 / 0.61519726 36 + } 37 + } 38 + impl Planet for Earth {} 39 + impl Planet for Mars { 40 + fn years_during(d: &Duration) -> f64 { 41 + d.0 / 1.8808158 42 + } 43 + } 44 + impl Planet for Jupiter { 45 + fn years_during(d: &Duration) -> f64 { 46 + d.0 / 11.862615 47 + } 48 + } 49 + impl Planet for Saturn { 50 + fn years_during(d: &Duration) -> f64 { 51 + d.0 / 29.447498 52 + } 53 + } 54 + impl Planet for Uranus { 55 + fn years_during(d: &Duration) -> f64 { 56 + d.0 / 84.016846 57 + } 58 + } 59 + impl Planet for Neptune { 60 + fn years_during(d: &Duration) -> f64 { 61 + d.0 / 164.79132 62 + } 63 + }
+60
rust/space-age/tests/space-age.rs
··· 1 + use space_age::*; 2 + 3 + fn assert_in_delta(expected: f64, actual: f64) { 4 + let diff: f64 = (expected - actual).abs(); 5 + let delta: f64 = 0.01; 6 + if diff > delta { 7 + panic!( 8 + "Your result of {} should be within {} of the expected result {}", 9 + actual, delta, expected 10 + ) 11 + } 12 + } 13 + 14 + #[test] 15 + fn earth_age() { 16 + let duration = Duration::from(1_000_000_000); 17 + assert_in_delta(31.69, Earth::years_during(&duration)); 18 + } 19 + 20 + #[test] 21 + fn mercury_age() { 22 + let duration = Duration::from(2_134_835_688); 23 + assert_in_delta(280.88, Mercury::years_during(&duration)); 24 + } 25 + 26 + #[test] 27 + fn venus_age() { 28 + let duration = Duration::from(189_839_836); 29 + assert_in_delta(9.78, Venus::years_during(&duration)); 30 + } 31 + 32 + #[test] 33 + fn mars_age() { 34 + let duration = Duration::from(2_129_871_239); 35 + assert_in_delta(35.88, Mars::years_during(&duration)); 36 + } 37 + 38 + #[test] 39 + fn jupiter_age() { 40 + let duration = Duration::from(901_876_382); 41 + assert_in_delta(2.41, Jupiter::years_during(&duration)); 42 + } 43 + 44 + #[test] 45 + fn saturn_age() { 46 + let duration = Duration::from(2_000_000_000); 47 + assert_in_delta(2.15, Saturn::years_during(&duration)); 48 + } 49 + 50 + #[test] 51 + fn uranus_age() { 52 + let duration = Duration::from(1_210_123_456); 53 + assert_in_delta(0.46, Uranus::years_during(&duration)); 54 + } 55 + 56 + #[test] 57 + fn neptune_age() { 58 + let duration = Duration::from(1_821_023_456); 59 + assert_in_delta(0.35, Neptune::years_during(&duration)); 60 + }