aboutsummaryrefslogtreecommitdiff
path: root/pascals-triangle
diff options
context:
space:
mode:
authorFederico Igne <git@federicoigne.com>2020-12-26 15:52:33 +0000
committerFederico Igne <git@federicoigne.com>2021-11-03 18:55:08 +0000
commit7ba7cf729752f1b1ff11a02fd8de6410aa448898 (patch)
tree6bb2d5f3a906512c26d355281ee1341af8269cf2 /pascals-triangle
parent727dd03e5e9d2fd62d00fab36c7d277a75849f9b (diff)
downloadexercism-7ba7cf729752f1b1ff11a02fd8de6410aa448898.tar.gz
exercism-7ba7cf729752f1b1ff11a02fd8de6410aa448898.zip
[rust] Pascal's Triangle
Diffstat (limited to 'pascals-triangle')
-rw-r--r--pascals-triangle/.exercism/metadata.json1
-rw-r--r--pascals-triangle/.gitignore8
-rw-r--r--pascals-triangle/Cargo.toml4
-rw-r--r--pascals-triangle/README.md95
-rw-r--r--pascals-triangle/src/lib.rs34
-rw-r--r--pascals-triangle/tests/pascals-triangle.rs96
6 files changed, 238 insertions, 0 deletions
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 @@
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
8Cargo.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 @@
1[package]
2edition = "2018"
3name = "pascals-triangle"
4version = "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 @@
1# Pascal's Triangle
2
3Compute Pascal's triangle up to a given number of rows.
4
5In Pascal's Triangle each number is computed by adding the numbers to
6the right and left of the current position in the previous row.
7
8```text
9 1
10 1 1
11 1 2 1
12 1 3 3 1
131 4 6 4 1
14# ... etc
15```
16
17## Rust Installation
18
19Refer to the [exercism help page][help-page] for Rust installation and learning
20resources.
21
22## Writing the Code
23
24Execute the tests with:
25
26```bash
27$ cargo test
28```
29
30All but the first test have been ignored. After you get the first test to
31pass, open the tests source file which is located in the `tests` directory
32and remove the `#[ignore]` flag from the next test and get the tests to pass
33again. Each separate test is a function with `#[test]` flag above it.
34Continue, until you pass every test.
35
36If you wish to run all ignored tests without editing the tests source file, use:
37
38```bash
39$ cargo test -- --ignored
40```
41
42To run a specific test, for example `some_test`, you can use:
43
44```bash
45$ cargo test some_test
46```
47
48If the specific test is ignored use:
49
50```bash
51$ cargo test some_test -- --ignored
52```
53
54To learn more about Rust tests refer to the [online test documentation][rust-tests]
55
56Make sure to read the [Modules][modules] chapter if you
57haven't already, it will help you with organizing your files.
58
59## Further improvements
60
61After 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.
62
63To format your solution, inside the solution directory use
64
65```bash
66cargo fmt
67```
68
69To see, if your solution contains some common ineffective use cases, inside the solution directory use
70
71```bash
72cargo clippy --all-targets
73```
74
75## Submitting the solution
76
77Generally 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.
78
79## Feedback, Issues, Pull Requests
80
81The [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!
82
83If 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).
84
85[help-page]: https://exercism.io/tracks/rust/learning
86[modules]: https://doc.rust-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html
87[cargo]: https://doc.rust-lang.org/book/ch14-00-more-about-cargo.html
88[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
89
90## Source
91
92Pascal's Triangle at Wolfram Math World [http://mathworld.wolfram.com/PascalsTriangle.html](http://mathworld.wolfram.com/PascalsTriangle.html)
93
94## Submitting Incomplete Solutions
95It'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 @@
1#[derive(Debug)]
2pub struct PascalsTriangle {
3 rows: Vec<Vec<u32>>,
4}
5
6impl PascalsTriangle {
7 fn next_row(row: &[u32]) -> Vec<u32> {
8 let mut next = Vec::new();
9 let last = row.iter().fold(0, |p, n| {
10 next.push(p + n);
11 *n
12 });
13 next.push(last);
14 next
15 }
16}
17
18impl PascalsTriangle {
19 pub fn new(row_count: u32) -> Self {
20 let mut rows = Vec::new();
21 if row_count > 0 {
22 rows.push(vec![1]);
23 (1..row_count).for_each(|i| {
24 let irow = Self::next_row(&rows[(i as usize) - 1]);
25 rows.push(irow);
26 })
27 }
28 PascalsTriangle { rows }
29 }
30
31 pub fn rows(&self) -> Vec<Vec<u32>> {
32 self.rows.clone()
33 }
34}
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 @@
1use pascals_triangle::*;
2
3#[test]
4fn no_rows() {
5 let pt = PascalsTriangle::new(0);
6 let expected: Vec<Vec<u32>> = Vec::new();
7 assert_eq!(expected, pt.rows());
8}
9
10#[test]
11fn one_row() {
12 let pt = PascalsTriangle::new(1);
13 let expected: Vec<Vec<u32>> = vec![vec![1]];
14 assert_eq!(expected, pt.rows());
15}
16
17#[test]
18fn two_rows() {
19 let pt = PascalsTriangle::new(2);
20 let expected: Vec<Vec<u32>> = vec![vec![1], vec![1, 1]];
21 assert_eq!(expected, pt.rows());
22}
23
24#[test]
25fn three_rows() {
26 let pt = PascalsTriangle::new(3);
27 let expected: Vec<Vec<u32>> = vec![vec![1], vec![1, 1], vec![1, 2, 1]];
28 assert_eq!(expected, pt.rows());
29}
30
31#[test]
32fn last_of_four_rows() {
33 let pt = PascalsTriangle::new(4);
34 let expected: Vec<u32> = vec![1, 3, 3, 1];
35 assert_eq!(Some(expected), pt.rows().pop());
36}
37
38#[test]
39fn five_rows() {
40 let pt = PascalsTriangle::new(5);
41 let expected: Vec<Vec<u32>> = vec![
42 vec![1],
43 vec![1, 1],
44 vec![1, 2, 1],
45 vec![1, 3, 3, 1],
46 vec![1, 4, 6, 4, 1],
47 ];
48 assert_eq!(expected, pt.rows());
49}
50
51#[test]
52fn six_rows() {
53 let pt = PascalsTriangle::new(6);
54 let expected: Vec<Vec<u32>> = vec![
55 vec![1],
56 vec![1, 1],
57 vec![1, 2, 1],
58 vec![1, 3, 3, 1],
59 vec![1, 4, 6, 4, 1],
60 vec![1, 5, 10, 10, 5, 1],
61 ];
62 assert_eq!(expected, pt.rows());
63}
64
65#[test]
66fn seven_rows() {
67 let pt = PascalsTriangle::new(7);
68 let expected: Vec<Vec<u32>> = vec![
69 vec![1],
70 vec![1, 1],
71 vec![1, 2, 1],
72 vec![1, 3, 3, 1],
73 vec![1, 4, 6, 4, 1],
74 vec![1, 5, 10, 10, 5, 1],
75 vec![1, 6, 15, 20, 15, 6, 1],
76 ];
77 assert_eq!(expected, pt.rows());
78}
79
80#[test]
81fn ten_rows() {
82 let pt = PascalsTriangle::new(10);
83 let expected: Vec<Vec<u32>> = vec![
84 vec![1],
85 vec![1, 1],
86 vec![1, 2, 1],
87 vec![1, 3, 3, 1],
88 vec![1, 4, 6, 4, 1],
89 vec![1, 5, 10, 10, 5, 1],
90 vec![1, 6, 15, 20, 15, 6, 1],
91 vec![1, 7, 21, 35, 35, 21, 7, 1],
92 vec![1, 8, 28, 56, 70, 56, 28, 8, 1],
93 vec![1, 9, 36, 84, 126, 126, 84, 36, 9, 1],
94 ];
95 assert_eq!(expected, pt.rows());
96}