Skip to content

Commit

Permalink
WIP: Implement Pascals Triangle
Browse files Browse the repository at this point in the history
Official tests have not been merged yet, but I'm mostly following them
here.

exercism/problem-specifications#362

Example code is naive. Just enough to pass the tests. A real example
solution will come later.
  • Loading branch information
whit0694 committed Sep 17, 2016
1 parent d6e8c86 commit 7dbb4b5
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
7 changes: 7 additions & 0 deletions exercises/pascals-triangle/.gitignore
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
3 changes: 3 additions & 0 deletions exercises/pascals-triangle/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[package]
name = "pascals-triangle"
version = "0.0.0"
35 changes: 35 additions & 0 deletions exercises/pascals-triangle/example.rs
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
}
}
38 changes: 38 additions & 0 deletions exercises/pascals-triangle/tests/pascals-triangle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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]
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());
}

0 comments on commit 7dbb4b5

Please sign in to comment.