|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
|
|
1
|
2
|
#[cfg(test)]
|
|
2
|
3
|
mod tests {
|
|
3
|
4
|
#[test]
|
|
|
@@ -5,14 +6,40 @@ mod tests {
|
|
5
|
6
|
assert_eq!(2 + 2, 4);
|
|
6
|
7
|
}
|
|
7
|
8
|
}
|
|
|
9
|
+
|
|
8
|
10
|
pub struct PascalsTriangle {
|
|
9
|
11
|
height: u32,
|
|
10
|
12
|
}
|
|
11
|
13
|
|
|
12
|
14
|
impl PascalsTriangle {
|
|
13
|
|
- pub fn new(i:u32) {
|
|
14
|
|
- height = i;
|
|
|
15
|
+ pub fn new(i: u32) -> Self {
|
|
|
16
|
+ PascalsTriangle { height: i }
|
|
15
|
17
|
}
|
|
16
|
|
- pub fn rows() -> Vec<Vec<u32>> {
|
|
|
18
|
+ pub fn rows(&self) -> Vec<Vec<u32>> {
|
|
|
19
|
+ let rows = self.height as usize;
|
|
|
20
|
+ let mut matrix = Vec::with_capacity(rows);
|
|
|
21
|
+
|
|
|
22
|
+ for line in 0..rows {
|
|
|
23
|
+ let current = line as usize;
|
|
|
24
|
+ matrix.push(Vec::with_capacity(current + 1));
|
|
|
25
|
+ matrix[current].push(1);
|
|
|
26
|
+
|
|
|
27
|
+ if current > 1 {
|
|
|
28
|
+ let previous = current - 1;
|
|
|
29
|
+ for index in 1..current {
|
|
|
30
|
+ let add = matrix[previous][index - 1] + matrix[previous][index];
|
|
|
31
|
+ matrix[current].push(add);
|
|
|
32
|
+ }
|
|
|
33
|
+ }
|
|
|
34
|
+
|
|
|
35
|
+ if current > 0 {
|
|
|
36
|
+ matrix[current].push(1);
|
|
|
37
|
+ }
|
|
|
38
|
+
|
|
|
39
|
+ }
|
|
|
40
|
+
|
|
|
41
|
+ println!("{:?}", matrix);
|
|
|
42
|
+
|
|
|
43
|
+ matrix
|
|
17
|
44
|
}
|
|
18
|
45
|
}
|