summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2021-09-29 20:18:42 +0530
committerAndinus <andinus@nand.sh>2021-09-29 20:18:42 +0530
commit6cb0e630baeb9a5acddac65a627bd2f9bf291312 (patch)
tree9a95f52c2570b063a7cc40d30b883cbacc812b37
parentb7f76db2a02aba793a68ba35c2b4b51d5e069bdc (diff)
downloadexercism-6cb0e630baeb9a5acddac65a627bd2f9bf291312.tar.gz
Rust: Space Age: Add exercise
-rw-r--r--README.org3
-rw-r--r--rust/space-age/.gitignore8
-rw-r--r--rust/space-age/Cargo.toml4
-rw-r--r--rust/space-age/HELP.md85
-rw-r--r--rust/space-age/README.md69
-rw-r--r--rust/space-age/src/lib.rs35
-rw-r--r--rust/space-age/tests/space-age.rs67
7 files changed, 270 insertions, 1 deletions
diff --git a/README.org b/README.org
index a457ed8..75df28a 100644
--- a/README.org
+++ b/README.org
@@ -91,10 +91,11 @@ My solutions for [[https://exercism.io][Exercism]] exercises.
 - [ ] Acronym
 - [ ] Allergies
 
-* Rust [5/5]
+* Rust [5/6]
 
 - [X] Hello World
 - [X] Lucian's Luscious Lasagna
 - [X] Armstrong Numbers
 - [X] Assembly Line
 - [X] Semi Structured Logs
+- [ ] Space Age
diff --git a/rust/space-age/.gitignore b/rust/space-age/.gitignore
new file mode 100644
index 0000000..db7f315
--- /dev/null
+++ b/rust/space-age/.gitignore
@@ -0,0 +1,8 @@
+# Generated by Cargo
+# will have compiled files and executables
+/target/
+**/*.rs.bk
+
+# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
+# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
+Cargo.lock
diff --git a/rust/space-age/Cargo.toml b/rust/space-age/Cargo.toml
new file mode 100644
index 0000000..a9e9332
--- /dev/null
+++ b/rust/space-age/Cargo.toml
@@ -0,0 +1,4 @@
+[package]
+edition = "2018"
+name = "space-age"
+version = "1.2.0"
diff --git a/rust/space-age/HELP.md b/rust/space-age/HELP.md
new file mode 100644
index 0000000..b4252f8
--- /dev/null
+++ b/rust/space-age/HELP.md
@@ -0,0 +1,85 @@
+# Help
+
+## Running the tests
+
+Execute the tests with:
+
+```bash
+$ cargo test
+```
+
+All but the first test have been ignored. After you get the first test to
+pass, open the tests source file which is located in the `tests` directory
+and remove the `#[ignore]` flag from the next test and get the tests to pass
+again. Each separate test is a function with `#[test]` flag above it.
+Continue, until you pass every test.
+
+If you wish to run _only ignored_ tests without editing the tests source file, use:
+
+```bash
+$ cargo test -- --ignored
+```
+
+If you are using Rust 1.51 or later, you can run _all_ tests with
+
+```bash
+$ cargo test -- --include-ignored
+```
+
+To run a specific test, for example `some_test`, you can use:
+
+```bash
+$ cargo test some_test
+```
+
+If the specific test is ignored, use:
+
+```bash
+$ cargo test some_test -- --ignored
+```
+
+To learn more about Rust tests refer to the online [test documentation][rust-tests].
+
+[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
+
+## Submitting your solution
+
+You can submit your solution using the `exercism submit src/lib.rs` command.
+This command will upload your solution to the Exercism website and print the solution page's URL.
+
+It's possible to submit an incomplete solution which allows you to:
+
+- See how others have completed the exercise
+- Request help from a mentor
+
+## Need to get help?
+
+If you'd like help solving the exercise, check the following pages:
+
+- The [Rust track's documentation](https://exercism.org/docs/tracks/rust)
+- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
+- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
+
+Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
+
+## Rust Installation
+
+Refer to the [exercism help page][help-page] for Rust installation and learning
+resources.
+
+## Submitting the solution
+
+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.
+
+## Feedback, Issues, Pull Requests
+
+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!
+
+If you want to know more about Exercism, take a look at the [contribution guide].
+
+## Submitting Incomplete Solutions
+It's possible to submit an incomplete solution so you can see how others have completed the exercise.
+
+[help-page]: https://exercism.io/tracks/rust/learning
+[github]: https://github.com/exercism/rust
+[contribution guide]: https://exercism.io/docs/community/contributors
\ No newline at end of file
diff --git a/rust/space-age/README.md b/rust/space-age/README.md
new file mode 100644
index 0000000..9724784
--- /dev/null
+++ b/rust/space-age/README.md
@@ -0,0 +1,69 @@
+# Space Age
+
+Welcome to Space Age on Exercism's Rust Track.
+If you need help running the tests or submitting your code, check out `HELP.md`.
+
+## Instructions
+
+Given an age in seconds, calculate how old someone would be on:
+
+   - Mercury: orbital period 0.2408467 Earth years
+   - Venus: orbital period 0.61519726 Earth years
+   - Earth: orbital period 1.0 Earth years, 365.25 Earth days, or 31557600 seconds
+   - Mars: orbital period 1.8808158 Earth years
+   - Jupiter: orbital period 11.862615 Earth years
+   - Saturn: orbital period 29.447498 Earth years
+   - Uranus: orbital period 84.016846 Earth years
+   - Neptune: orbital period 164.79132 Earth years
+
+So if you were told someone were 1,000,000,000 seconds old, you should
+be able to say that they're 31.69 Earth-years old.
+
+If you're wondering why Pluto didn't make the cut, go watch [this
+youtube video](http://www.youtube.com/watch?v=Z_2gbGXzFbs).
+
+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
+- Macros, the use of a macro could reduce boilerplate and increase readability 
+  for this exercise. For instance, 
+  [a macro can implement a trait for multiple types at once](https://stackoverflow.com/questions/39150216/implementing-a-trait-for-multiple-types-at-once),
+  though it is fine to implement `years_during` in the Planet trait itself. A macro could
+  define both the structs and their implementations. Info to get started with macros can 
+  be found at:
+  
+  - [The Macros chapter in The Rust Programming Language](https://doc.rust-lang.org/stable/book/ch19-06-macros.html)
+  - [an older version of the Macros chapter with helpful detail](https://doc.rust-lang.org/1.30.0/book/first-edition/macros.html)
+  - [Rust By Example](https://doc.rust-lang.org/stable/rust-by-example/macros.html)
+
+## Source
+
+### Created by
+
+- @IanWhitney
+
+### Contributed to by
+
+- @ashleygwilliams
+- @bobahop
+- @coriolinus
+- @cwhakes
+- @durka
+- @eddyp
+- @efx
+- @ErikSchierboom
+- @IanWhitney
+- @joshgoebel
+- @lutostag
+- @nfiles
+- @ocstl
+- @petertseng
+- @rofrol
+- @stringparser
+- @xakon
+- @ZapAnton
+
+### Based on
+
+Partially inspired by Chapter 1 in Chris Pine's online Learn to Program tutorial. - http://pine.fm/LearnToProgram/?Chapter=01
\ No newline at end of file
diff --git a/rust/space-age/src/lib.rs b/rust/space-age/src/lib.rs
new file mode 100644
index 0000000..fec643f
--- /dev/null
+++ b/rust/space-age/src/lib.rs
@@ -0,0 +1,35 @@
+#[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 {}
diff --git a/rust/space-age/tests/space-age.rs b/rust/space-age/tests/space-age.rs
new file mode 100644
index 0000000..b54e2f4
--- /dev/null
+++ b/rust/space-age/tests/space-age.rs
@@ -0,0 +1,67 @@
+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_129_871_239);
+    assert_in_delta(35.88, 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(2_000_000_000);
+    assert_in_delta(2.15, Saturn::years_during(&duration));
+}
+
+#[test]
+#[ignore]
+fn uranus_age() {
+    let duration = Duration::from(1_210_123_456);
+    assert_in_delta(0.46, Uranus::years_during(&duration));
+}
+
+#[test]
+#[ignore]
+fn neptune_age() {
+    let duration = Duration::from(1_821_023_456);
+    assert_in_delta(0.35, Neptune::years_during(&duration));
+}