Skip to content

Commit a9d15b2

Browse files
committed
start pascals
1 parent 39aeadf commit a9d15b2

File tree

9 files changed

+323
-0
lines changed

9 files changed

+323
-0
lines changed

rust/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ members = [
3737
"nucleotide-count",
3838
"pangram",
3939
"palindrome-products",
40+
"pascals-triangle",
4041
"perfect-numbers",
4142
"prime-factors",
4243
"proverb",
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"authors": [
3+
"IanWhitney"
4+
],
5+
"contributors": [
6+
"coriolinus",
7+
"cwhakes",
8+
"efx",
9+
"ErikSchierboom",
10+
"hekrause",
11+
"lutostag",
12+
"navossoc",
13+
"nfiles",
14+
"petertseng",
15+
"rofrol",
16+
"stringparser",
17+
"xakon",
18+
"ZapAnton"
19+
],
20+
"files": {
21+
"solution": [
22+
"src/lib.rs",
23+
"Cargo.toml"
24+
],
25+
"test": [
26+
"tests/pascals-triangle.rs"
27+
],
28+
"example": [
29+
".meta/example.rs"
30+
]
31+
},
32+
"blurb": "Compute Pascal's triangle up to a given number of rows.",
33+
"source": "Pascal's Triangle at Wolfram Math World",
34+
"source_url": "https://www.wolframalpha.com/input/?i=Pascal%27s+triangle"
35+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"track":"rust","exercise":"pascals-triangle","id":"f0f294b2e8e949d6b03a431d02afba46","url":"https://exercism.org/tracks/rust/exercises/pascals-triangle","handle":"rootulp","is_requester":true,"auto_approve":false}

rust/pascals-triangle/.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
8+
Cargo.lock

rust/pascals-triangle/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[package]
2+
edition = "2021"
3+
name = "pascals-triangle"
4+
version = "1.5.0"

rust/pascals-triangle/HELP.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Help
2+
3+
## Running the tests
4+
5+
Execute the tests with:
6+
7+
```bash
8+
$ cargo test
9+
```
10+
11+
All but the first test have been ignored. After you get the first test to
12+
pass, open the tests source file which is located in the `tests` directory
13+
and remove the `#[ignore]` flag from the next test and get the tests to pass
14+
again. Each separate test is a function with `#[test]` flag above it.
15+
Continue, until you pass every test.
16+
17+
If you wish to run _only ignored_ tests without editing the tests source file, use:
18+
19+
```bash
20+
$ cargo test -- --ignored
21+
```
22+
23+
If you are using Rust 1.51 or later, you can run _all_ tests with
24+
25+
```bash
26+
$ cargo test -- --include-ignored
27+
```
28+
29+
To run a specific test, for example `some_test`, you can use:
30+
31+
```bash
32+
$ cargo test some_test
33+
```
34+
35+
If the specific test is ignored, use:
36+
37+
```bash
38+
$ cargo test some_test -- --ignored
39+
```
40+
41+
To learn more about Rust tests refer to the online [test documentation][rust-tests].
42+
43+
[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
44+
45+
## Submitting your solution
46+
47+
You can submit your solution using the `exercism submit src/lib.rs Cargo.toml` command.
48+
This command will upload your solution to the Exercism website and print the solution page's URL.
49+
50+
It's possible to submit an incomplete solution which allows you to:
51+
52+
- See how others have completed the exercise
53+
- Request help from a mentor
54+
55+
## Need to get help?
56+
57+
If you'd like help solving the exercise, check the following pages:
58+
59+
- The [Rust track's documentation](https://exercism.org/docs/tracks/rust)
60+
- The [Rust track's programming category on the forum](https://forum.exercism.org/c/programming/rust)
61+
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
62+
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
63+
64+
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
65+
66+
## Rust Installation
67+
68+
Refer to the [exercism help page][help-page] for Rust installation and learning
69+
resources.
70+
71+
## Submitting the solution
72+
73+
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.
74+
75+
## Feedback, Issues, Pull Requests
76+
77+
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!
78+
79+
If you want to know more about Exercism, take a look at the [contribution guide].
80+
81+
## Submitting Incomplete Solutions
82+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
83+
84+
[help-page]: https://exercism.org/tracks/rust/learning
85+
[github]: https://github.com/exercism/rust
86+
[contribution guide]: https://exercism.org/docs/community/contributors

rust/pascals-triangle/README.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Pascal's Triangle
2+
3+
Welcome to Pascal's Triangle on Exercism's Rust Track.
4+
If you need help running the tests or submitting your code, check out `HELP.md`.
5+
6+
## Introduction
7+
8+
With the weather being great, you're not looking forward to spending an hour in a classroom.
9+
Annoyed, you enter the class room, where you notice a strangely satisfying triangle shape on the blackboard.
10+
Whilst waiting for your math teacher to arrive, you can't help but notice some patterns in the triangle: the outer values are all ones, each subsequent row has one more value than its previous row and the triangle is symmetrical.
11+
Weird!
12+
13+
Not long after you sit down, your teacher enters the room and explains that this triangle is the famous [Pascal's triangle][wikipedia].
14+
15+
Over the next hour, your teacher reveals some amazing things hidden in this triangle:
16+
17+
- It can be used to compute how many ways you can pick K elements from N values.
18+
- It contains the Fibonacci sequence.
19+
- If you color odd and even numbers differently, you get a beautiful pattern called the [Sierpiński triangle][wikipedia-sierpinski-triangle].
20+
21+
The teacher implores you and your classmates to lookup other uses, and assures you that there are lots more!
22+
At that moment, the school bell rings.
23+
You realize that for the past hour, you were completely absorbed in learning about Pascal's triangle.
24+
You quickly grab your laptop from your bag and go outside, ready to enjoy both the sunshine _and_ the wonders of Pascal's triangle.
25+
26+
[wikipedia]: https://en.wikipedia.org/wiki/Pascal%27s_triangle
27+
[wikipedia-sierpinski-triangle]: https://en.wikipedia.org/wiki/Sierpi%C5%84ski_triangle
28+
29+
## Instructions
30+
31+
Your task is to output the first N rows of Pascal's triangle.
32+
33+
[Pascal's triangle][wikipedia] is a triangular array of positive integers.
34+
35+
In Pascal's triangle, the number of values in a row is equal to its row number (which starts at one).
36+
Therefore, the first row has one value, the second row has two values, and so on.
37+
38+
The first (topmost) row has a single value: `1`.
39+
Subsequent rows' values are computed by adding the numbers directly to the right and left of the current position in the previous row.
40+
41+
If the previous row does _not_ have a value to the left or right of the current position (which only happens for the leftmost and rightmost positions), treat that position's value as zero (effectively "ignoring" it in the summation).
42+
43+
## Example
44+
45+
Let's look at the first 5 rows of Pascal's Triangle:
46+
47+
```text
48+
1
49+
1 1
50+
1 2 1
51+
1 3 3 1
52+
1 4 6 4 1
53+
```
54+
55+
The topmost row has one value, which is `1`.
56+
57+
The leftmost and rightmost values have only one preceding position to consider, which is the position to its right respectively to its left.
58+
With the topmost value being `1`, it follows from this that all the leftmost and rightmost values are also `1`.
59+
60+
The other values all have two positions to consider.
61+
For example, the fifth row's (`1 4 6 4 1`) middle value is `6`, as the values to its left and right in the preceding row are `3` and `3`:
62+
63+
[wikipedia]: https://en.wikipedia.org/wiki/Pascal%27s_triangle
64+
65+
## Source
66+
67+
### Created by
68+
69+
- @IanWhitney
70+
71+
### Contributed to by
72+
73+
- @coriolinus
74+
- @cwhakes
75+
- @efx
76+
- @ErikSchierboom
77+
- @hekrause
78+
- @lutostag
79+
- @navossoc
80+
- @nfiles
81+
- @petertseng
82+
- @rofrol
83+
- @stringparser
84+
- @xakon
85+
- @ZapAnton
86+
87+
### Based on
88+
89+
Pascal's Triangle at Wolfram Math World - https://www.wolframalpha.com/input/?i=Pascal%27s+triangle

rust/pascals-triangle/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
pub struct PascalsTriangle;
2+
3+
impl PascalsTriangle {
4+
pub fn new(row_count: u32) -> Self {
5+
todo!("create Pascal's triangle with {row_count} rows");
6+
}
7+
8+
pub fn rows(&self) -> Vec<Vec<u32>> {
9+
todo!();
10+
}
11+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
use pascals_triangle::PascalsTriangle;
2+
3+
#[test]
4+
fn zero_rows() {
5+
let pt = PascalsTriangle::new(0);
6+
let expected: Vec<Vec<u32>> = vec![];
7+
assert_eq!(pt.rows(), expected);
8+
}
9+
10+
#[test]
11+
#[ignore]
12+
fn single_row() {
13+
let pt = PascalsTriangle::new(1);
14+
let expected: Vec<Vec<u32>> = vec![vec![1]];
15+
assert_eq!(pt.rows(), expected);
16+
}
17+
18+
#[test]
19+
#[ignore]
20+
fn two_rows() {
21+
let pt = PascalsTriangle::new(2);
22+
let expected: Vec<Vec<u32>> = vec![vec![1], vec![1, 1]];
23+
assert_eq!(pt.rows(), expected);
24+
}
25+
26+
#[test]
27+
#[ignore]
28+
fn three_rows() {
29+
let pt = PascalsTriangle::new(3);
30+
let expected: Vec<Vec<u32>> = vec![vec![1], vec![1, 1], vec![1, 2, 1]];
31+
assert_eq!(pt.rows(), expected);
32+
}
33+
34+
#[test]
35+
#[ignore]
36+
fn four_rows() {
37+
let pt = PascalsTriangle::new(4);
38+
let expected: Vec<Vec<u32>> = vec![vec![1], vec![1, 1], vec![1, 2, 1], vec![1, 3, 3, 1]];
39+
assert_eq!(pt.rows(), expected);
40+
}
41+
42+
#[test]
43+
#[ignore]
44+
fn five_rows() {
45+
let pt = PascalsTriangle::new(5);
46+
let expected: Vec<Vec<u32>> = vec![
47+
vec![1],
48+
vec![1, 1],
49+
vec![1, 2, 1],
50+
vec![1, 3, 3, 1],
51+
vec![1, 4, 6, 4, 1],
52+
];
53+
assert_eq!(pt.rows(), expected);
54+
}
55+
56+
#[test]
57+
#[ignore]
58+
fn six_rows() {
59+
let pt = PascalsTriangle::new(6);
60+
let expected: Vec<Vec<u32>> = vec![
61+
vec![1],
62+
vec![1, 1],
63+
vec![1, 2, 1],
64+
vec![1, 3, 3, 1],
65+
vec![1, 4, 6, 4, 1],
66+
vec![1, 5, 10, 10, 5, 1],
67+
];
68+
assert_eq!(pt.rows(), expected);
69+
}
70+
71+
#[test]
72+
#[ignore]
73+
fn ten_rows() {
74+
let pt = PascalsTriangle::new(10);
75+
let expected: Vec<Vec<u32>> = vec![
76+
vec![1],
77+
vec![1, 1],
78+
vec![1, 2, 1],
79+
vec![1, 3, 3, 1],
80+
vec![1, 4, 6, 4, 1],
81+
vec![1, 5, 10, 10, 5, 1],
82+
vec![1, 6, 15, 20, 15, 6, 1],
83+
vec![1, 7, 21, 35, 35, 21, 7, 1],
84+
vec![1, 8, 28, 56, 70, 56, 28, 8, 1],
85+
vec![1, 9, 36, 84, 126, 126, 84, 36, 9, 1],
86+
];
87+
assert_eq!(pt.rows(), expected);
88+
}

0 commit comments

Comments
 (0)