forked from exercism/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Official tests have not been merged yet, but I'm mostly following them here. exercism/problem-specifications#362 I thought I could use `should_panic` to test a negative row count, but no. It won't compile since I'm violating type safety. I've left that test in, but plan to remove it before merge. Example code is naive. Just enough to pass the tests. A real example solution will come later.
- Loading branch information
Showing
4 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Generated by Cargo | ||
# will have compiled files and executables | ||
/target/ | ||
|
||
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[package] | ||
name = "pascals-triangle" | ||
version = "0.0.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
struct PascalsTriangle { | ||
row_count: u32, | ||
} | ||
|
||
impl PascalsTriangle { | ||
fn with_rows(row_count: u32) -> Self { | ||
PascalsTriangle { row_count: row_count } | ||
} | ||
|
||
fn rows(&self) -> Vec<Vec<u32>> { | ||
let mut r: Vec<Vec<u32>> = Vec::new(); | ||
if self.row_count == 1 { | ||
r.push(vec![1]); | ||
} | ||
|
||
if self.row_count == 2 { | ||
r.push(vec![1]); | ||
r.push(vec![1, 1]); | ||
} | ||
|
||
if self.row_count == 3 { | ||
r.push(vec![1]); | ||
r.push(vec![1, 1]); | ||
r.push(vec![1, 2, 1]); | ||
} | ||
|
||
if self.row_count == 4 { | ||
r.push(vec![1]); | ||
r.push(vec![1, 1]); | ||
r.push(vec![1, 2, 1]); | ||
r.push(vec![1, 3, 3, 1]); | ||
} | ||
r | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
extern crate pascals_triangle; | ||
|
||
use pascals_triangle::*; | ||
|
||
#[test] | ||
fn no_rows() { | ||
let pt = PascalsTriangle::new(0); | ||
let expected: Vec<Vec<u32>> = Vec::new(); | ||
assert_eq!(expected, pt.rows()); | ||
} | ||
|
||
// #[test] | ||
// #[should_panic] | ||
// fn negative_rows_panic() { | ||
// PascalsTriangle::with_rows(0 - 1); | ||
// } | ||
|
||
#[test] | ||
fn one_row() { | ||
let pt = PascalsTriangle::new(1); | ||
let expected: Vec<Vec<u32>> = vec![vec![1]]; | ||
assert_eq!(expected, pt.rows()); | ||
} | ||
|
||
#[test] | ||
fn two_rows() { | ||
let pt = PascalsTriangle::new(2); | ||
let expected: Vec<Vec<u32>> = vec![vec![1], vec![1, 1]]; | ||
assert_eq!(expected, pt.rows()); | ||
} | ||
|
||
#[test] | ||
fn three_rows() { | ||
let pt = PascalsTriangle::new(3); | ||
let expected: Vec<Vec<u32>> = 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<u32> = vec![1, 3, 3, 1]; | ||
assert_eq!(expected, pt.rows().pop().unwrap()); | ||
} |