Procházet zdrojové kódy

Solved hw2/task3. Everything fine.

Joshua Rutschmann před 8 roky
rodič
revize
6d8cc87a85
2 změnil soubory, kde provedl 37 přidání a 3 odebrání
  1. 30
    3
      hw2/task3/src/lib.rs
  2. 7
    0
      hw2/task3/src/main.rs

+ 30
- 3
hw2/task3/src/lib.rs Zobrazit soubor

@@ -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
 }

+ 7
- 0
hw2/task3/src/main.rs Zobrazit soubor

@@ -0,0 +1,7 @@
1
+extern crate task3;
2
+use task3::PascalsTriangle;
3
+
4
+fn main() {
5
+    let p = PascalsTriangle::new(10);
6
+    p.rows();
7
+}

Loading…
Zrušit
Uložit