From 93084c4cc6a155e1d5346ab1b0aa0e1eea7a3a8b Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Sat, 25 Sep 2021 19:45:53 -0300 Subject: [PATCH] Exercism: Short Fibonacci --- rust/short-fibonacci/.exercism/config.json | 18 ++++ rust/short-fibonacci/.exercism/metadata.json | 1 + rust/short-fibonacci/Cargo.lock | 7 ++ rust/short-fibonacci/Cargo.toml | 5 ++ rust/short-fibonacci/HELP.md | 85 +++++++++++++++++++ rust/short-fibonacci/HINTS.md | 5 ++ rust/short-fibonacci/README.md | 40 +++++++++ rust/short-fibonacci/src/lib.rs | 22 +++++ rust/short-fibonacci/tests/short-fibonacci.rs | 24 ++++++ 9 files changed, 207 insertions(+) create mode 100644 rust/short-fibonacci/.exercism/config.json create mode 100644 rust/short-fibonacci/.exercism/metadata.json create mode 100644 rust/short-fibonacci/Cargo.lock create mode 100644 rust/short-fibonacci/Cargo.toml create mode 100644 rust/short-fibonacci/HELP.md create mode 100644 rust/short-fibonacci/HINTS.md create mode 100644 rust/short-fibonacci/README.md create mode 100644 rust/short-fibonacci/src/lib.rs create mode 100644 rust/short-fibonacci/tests/short-fibonacci.rs diff --git a/rust/short-fibonacci/.exercism/config.json b/rust/short-fibonacci/.exercism/config.json new file mode 100644 index 0000000..719f639 --- /dev/null +++ b/rust/short-fibonacci/.exercism/config.json @@ -0,0 +1,18 @@ +{ + "blurb": "Learn the `vec!` macro to build a short Fibonacci Sequence.", + "authors": [ + "efx", + "coriolinus" + ], + "files": { + "solution": [ + "src/lib.rs" + ], + "test": [ + "tests/short-fibonacci.rs" + ], + "exemplar": [ + ".meta/exemplar.rs" + ] + } +} diff --git a/rust/short-fibonacci/.exercism/metadata.json b/rust/short-fibonacci/.exercism/metadata.json new file mode 100644 index 0000000..343f08c --- /dev/null +++ b/rust/short-fibonacci/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"rust","exercise":"short-fibonacci","id":"f63506ee1b994de9b6f5dc3d2b70b171","url":"https://exercism.org/tracks/rust/exercises/short-fibonacci","handle":"JBiason","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/rust/short-fibonacci/Cargo.lock b/rust/short-fibonacci/Cargo.lock new file mode 100644 index 0000000..4dc22fa --- /dev/null +++ b/rust/short-fibonacci/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "short_fibonacci" +version = "0.1.0" diff --git a/rust/short-fibonacci/Cargo.toml b/rust/short-fibonacci/Cargo.toml new file mode 100644 index 0000000..7685704 --- /dev/null +++ b/rust/short-fibonacci/Cargo.toml @@ -0,0 +1,5 @@ +[package] +name = "short_fibonacci" +version = "0.1.0" +edition = "2018" + diff --git a/rust/short-fibonacci/HELP.md b/rust/short-fibonacci/HELP.md new file mode 100644 index 0000000..b4252f8 --- /dev/null +++ b/rust/short-fibonacci/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/short-fibonacci/HINTS.md b/rust/short-fibonacci/HINTS.md new file mode 100644 index 0000000..deb22bb --- /dev/null +++ b/rust/short-fibonacci/HINTS.md @@ -0,0 +1,5 @@ +# Hints + +## General + +- Check out the Rust documentation on [the `vec!` macro](https://doc.rust-lang.org/std/macro.vec.html). \ No newline at end of file diff --git a/rust/short-fibonacci/README.md b/rust/short-fibonacci/README.md new file mode 100644 index 0000000..ec045a2 --- /dev/null +++ b/rust/short-fibonacci/README.md @@ -0,0 +1,40 @@ +# A Short Fibonacci Sequence + +Welcome to A Short Fibonacci Sequence on Exercism's Rust Track. +If you need help running the tests or submitting your code, check out `HELP.md`. +If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :) + +## Introduction + +Rust provides a macro `vec![]` to help you create Vectors. +This comes in quite handy when you need to initialize lists. + +## Instructions + +You are going to initialize empty buffers and list the first five numbers, or elements, of the Fibonacci sequence. + +The Fibonacci sequence is a set of numbers where the next element is the sum of the prior two. We start the sequence at one. So the first two elements are 1 and 1. + +## 1. Create a buffer of `count` zeroes. + +Create a function that creates a buffer of `count` zeroes. +```rust +let my_buffer = create_buffer(5); +// [0, 0, 0, 0, 0] +``` + +## 2. List the first five elements of the Fibonacci sequence + +Create a function that returns the first five numbers of the Fibonacci sequence. +Its first five elements are `1, 1, 2, 3, 5` +```rust +let first_five = fibonacci(); +// [1, 1, 2, 3, 5] +``` + +## Source + +### Created by + +- @efx +- @coriolinus \ No newline at end of file diff --git a/rust/short-fibonacci/src/lib.rs b/rust/short-fibonacci/src/lib.rs new file mode 100644 index 0000000..ec51089 --- /dev/null +++ b/rust/short-fibonacci/src/lib.rs @@ -0,0 +1,22 @@ +use std::iter; + +/// Create an empty vector +pub fn create_empty() -> Vec { + Vec::new() +} + +/// Create a buffer of `count` zeroes. +/// +/// Applications often use buffers when serializing data to send over the +/// network. +pub fn create_buffer(count: usize) -> Vec { + iter::repeat(0).take(count).collect() +} + +/// Create a vector containing the first five elements of the Fibonacci sequence. +/// +/// Fibonacci's sequence is the list of numbers where the next number is a sum of the previous two. +/// Its first five elements are `1, 1, 2, 3, 5`. +pub fn fibonacci() -> Vec { + vec![1, 1, 2, 3, 5] +} diff --git a/rust/short-fibonacci/tests/short-fibonacci.rs b/rust/short-fibonacci/tests/short-fibonacci.rs new file mode 100644 index 0000000..fc25643 --- /dev/null +++ b/rust/short-fibonacci/tests/short-fibonacci.rs @@ -0,0 +1,24 @@ +use short_fibonacci::*; + +#[test] +fn test_empty() { + assert_eq!(create_empty(), Vec::new()); +} +#[test] +fn test_buffer() { + for n in 0..10 { + let zeroized = create_buffer(n); + assert_eq!(zeroized.len(), n); + assert!(zeroized.iter().all(|&v| v == 0)); + } +} +#[test] +fn test_fibonacci() { + let fibb = fibonacci(); + assert_eq!(fibb.len(), 5); + assert_eq!(fibb[0], 1); + assert_eq!(fibb[1], 1); + for window in fibb.windows(3) { + assert_eq!(window[0] + window[1], window[2]); + } +}