From 7ba7cf729752f1b1ff11a02fd8de6410aa448898 Mon Sep 17 00:00:00 2001 From: Federico Igne Date: Sat, 26 Dec 2020 15:52:33 +0000 Subject: [rust] Pascal's Triangle --- pascals-triangle/.exercism/metadata.json | 1 + pascals-triangle/.gitignore | 8 +++ pascals-triangle/Cargo.toml | 4 ++ pascals-triangle/README.md | 95 +++++++++++++++++++++++++++++ pascals-triangle/src/lib.rs | 34 +++++++++++ pascals-triangle/tests/pascals-triangle.rs | 96 ++++++++++++++++++++++++++++++ 6 files changed, 238 insertions(+) create mode 100644 pascals-triangle/.exercism/metadata.json create mode 100644 pascals-triangle/.gitignore create mode 100644 pascals-triangle/Cargo.toml create mode 100644 pascals-triangle/README.md create mode 100644 pascals-triangle/src/lib.rs create mode 100644 pascals-triangle/tests/pascals-triangle.rs diff --git a/pascals-triangle/.exercism/metadata.json b/pascals-triangle/.exercism/metadata.json new file mode 100644 index 0000000..7352c3c --- /dev/null +++ b/pascals-triangle/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"rust","exercise":"pascals-triangle","id":"1186776428ea49b494e3cd0bd68308bc","url":"https://exercism.io/my/solutions/1186776428ea49b494e3cd0bd68308bc","handle":"dyamon","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/pascals-triangle/.gitignore b/pascals-triangle/.gitignore new file mode 100644 index 0000000..db7f315 --- /dev/null +++ b/pascals-triangle/.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/pascals-triangle/Cargo.toml b/pascals-triangle/Cargo.toml new file mode 100644 index 0000000..d308b66 --- /dev/null +++ b/pascals-triangle/Cargo.toml @@ -0,0 +1,4 @@ +[package] +edition = "2018" +name = "pascals-triangle" +version = "1.5.0" diff --git a/pascals-triangle/README.md b/pascals-triangle/README.md new file mode 100644 index 0000000..504b0b3 --- /dev/null +++ b/pascals-triangle/README.md @@ -0,0 +1,95 @@ +# Pascal's Triangle + +Compute Pascal's triangle up to a given number of rows. + +In Pascal's Triangle each number is computed by adding the numbers to +the right and left of the current position in the previous row. + +```text + 1 + 1 1 + 1 2 1 + 1 3 3 1 +1 4 6 4 1 +# ... etc +``` + +## Rust Installation + +Refer to the [exercism help page][help-page] for Rust installation and learning +resources. + +## Writing the Code + +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 all ignored tests without editing the tests source file, use: + +```bash +$ cargo test -- --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] + +Make sure to read the [Modules][modules] chapter if you +haven't already, it will help you with organizing your files. + +## Further improvements + +After you have solved the exercise, please consider using the additional utilities, described in the [installation guide](https://exercism.io/tracks/rust/installation), to further refine your final solution. + +To format your solution, inside the solution directory use + +```bash +cargo fmt +``` + +To see, if your solution contains some common ineffective use cases, inside the solution directory use + +```bash +cargo clippy --all-targets +``` + +## 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 [exercism/rust](https://github.com/exercism/rust) repository on 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](https://github.com/exercism/docs/blob/master/contributing-to-language-tracks/README.md). + +[help-page]: https://exercism.io/tracks/rust/learning +[modules]: https://doc.rust-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html +[cargo]: https://doc.rust-lang.org/book/ch14-00-more-about-cargo.html +[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html + +## Source + +Pascal's Triangle at Wolfram Math World [http://mathworld.wolfram.com/PascalsTriangle.html](http://mathworld.wolfram.com/PascalsTriangle.html) + +## Submitting Incomplete Solutions +It's possible to submit an incomplete solution so you can see how others have completed the exercise. diff --git a/pascals-triangle/src/lib.rs b/pascals-triangle/src/lib.rs new file mode 100644 index 0000000..7b2145b --- /dev/null +++ b/pascals-triangle/src/lib.rs @@ -0,0 +1,34 @@ +#[derive(Debug)] +pub struct PascalsTriangle { + rows: Vec>, +} + +impl PascalsTriangle { + fn next_row(row: &[u32]) -> Vec { + let mut next = Vec::new(); + let last = row.iter().fold(0, |p, n| { + next.push(p + n); + *n + }); + next.push(last); + next + } +} + +impl PascalsTriangle { + pub fn new(row_count: u32) -> Self { + let mut rows = Vec::new(); + if row_count > 0 { + rows.push(vec![1]); + (1..row_count).for_each(|i| { + let irow = Self::next_row(&rows[(i as usize) - 1]); + rows.push(irow); + }) + } + PascalsTriangle { rows } + } + + pub fn rows(&self) -> Vec> { + self.rows.clone() + } +} diff --git a/pascals-triangle/tests/pascals-triangle.rs b/pascals-triangle/tests/pascals-triangle.rs new file mode 100644 index 0000000..64d2c2e --- /dev/null +++ b/pascals-triangle/tests/pascals-triangle.rs @@ -0,0 +1,96 @@ +use pascals_triangle::*; + +#[test] +fn no_rows() { + let pt = PascalsTriangle::new(0); + let expected: Vec> = Vec::new(); + assert_eq!(expected, pt.rows()); +} + +#[test] +fn one_row() { + let pt = PascalsTriangle::new(1); + let expected: Vec> = vec![vec![1]]; + assert_eq!(expected, pt.rows()); +} + +#[test] +fn two_rows() { + let pt = PascalsTriangle::new(2); + let expected: Vec> = vec![vec![1], vec![1, 1]]; + assert_eq!(expected, pt.rows()); +} + +#[test] +fn three_rows() { + let pt = PascalsTriangle::new(3); + let expected: Vec> = vec![vec![1], vec![1, 1], vec![1, 2, 1]]; + assert_eq!(expected, pt.rows()); +} + +#[test] +fn last_of_four_rows() { + let pt = PascalsTriangle::new(4); + let expected: Vec = vec![1, 3, 3, 1]; + assert_eq!(Some(expected), pt.rows().pop()); +} + +#[test] +fn five_rows() { + let pt = PascalsTriangle::new(5); + let expected: Vec> = vec![ + vec![1], + vec![1, 1], + vec![1, 2, 1], + vec![1, 3, 3, 1], + vec![1, 4, 6, 4, 1], + ]; + assert_eq!(expected, pt.rows()); +} + +#[test] +fn six_rows() { + let pt = PascalsTriangle::new(6); + let expected: Vec> = vec![ + vec![1], + vec![1, 1], + vec![1, 2, 1], + vec![1, 3, 3, 1], + vec![1, 4, 6, 4, 1], + vec![1, 5, 10, 10, 5, 1], + ]; + assert_eq!(expected, pt.rows()); +} + +#[test] +fn seven_rows() { + let pt = PascalsTriangle::new(7); + let expected: Vec> = vec![ + vec![1], + vec![1, 1], + vec![1, 2, 1], + vec![1, 3, 3, 1], + vec![1, 4, 6, 4, 1], + vec![1, 5, 10, 10, 5, 1], + vec![1, 6, 15, 20, 15, 6, 1], + ]; + assert_eq!(expected, pt.rows()); +} + +#[test] +fn ten_rows() { + let pt = PascalsTriangle::new(10); + let expected: Vec> = vec![ + vec![1], + vec![1, 1], + vec![1, 2, 1], + vec![1, 3, 3, 1], + vec![1, 4, 6, 4, 1], + vec![1, 5, 10, 10, 5, 1], + vec![1, 6, 15, 20, 15, 6, 1], + vec![1, 7, 21, 35, 35, 21, 7, 1], + vec![1, 8, 28, 56, 70, 56, 28, 8, 1], + vec![1, 9, 36, 84, 126, 126, 84, 36, 9, 1], + ]; + assert_eq!(expected, pt.rows()); +} -- cgit v1.2.3