Browse Source

Exercism: Short Fibonacci

master
Julio Biason 3 years ago
parent
commit
93084c4cc6
  1. 18
      rust/short-fibonacci/.exercism/config.json
  2. 1
      rust/short-fibonacci/.exercism/metadata.json
  3. 7
      rust/short-fibonacci/Cargo.lock
  4. 5
      rust/short-fibonacci/Cargo.toml
  5. 85
      rust/short-fibonacci/HELP.md
  6. 5
      rust/short-fibonacci/HINTS.md
  7. 40
      rust/short-fibonacci/README.md
  8. 22
      rust/short-fibonacci/src/lib.rs
  9. 24
      rust/short-fibonacci/tests/short-fibonacci.rs

18
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"
]
}
}

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

7
rust/short-fibonacci/Cargo.lock generated

@ -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"

5
rust/short-fibonacci/Cargo.toml

@ -0,0 +1,5 @@
[package]
name = "short_fibonacci"
version = "0.1.0"
edition = "2018"

85
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

5
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).

40
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

22
rust/short-fibonacci/src/lib.rs

@ -0,0 +1,22 @@
use std::iter;
/// Create an empty vector
pub fn create_empty() -> Vec<u8> {
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<u8> {
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<u8> {
vec![1, 1, 2, 3, 5]
}

24
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]);
}
}
Loading…
Cancel
Save