This commit is contained in:
hendrik 2024-05-22 14:56:12 +02:00
commit d6e46b95fd
4 changed files with 53 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

7
Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "play"
version = "0.1.0"

6
Cargo.toml Normal file
View File

@ -0,0 +1,6 @@
[package]
name = "play"
version = "0.1.0"
edition = "2021"
[dependencies]

39
src/main.rs Normal file
View File

@ -0,0 +1,39 @@
pub struct PascalsTriangle {
row_count: u32,
}
impl PascalsTriangle {
pub fn new(row_count: u32) -> Self {
Self { row_count }
}
pub fn rows(&self) -> Vec<Vec<u32>> {
let mut all_rows = vec![vec![1]];
for _ in 0..self.row_count {
all_rows.push(
self.get_next(
all_rows
.last()
.unwrap()
.iter()
.map(|&x| x)
.collect::<Vec<u32>>(),
),
);
}
all_rows
}
pub fn get_next(&self, last_row: Vec<u32>) -> Vec<u32> {
let mut next_row: Vec<u32> = last_row.windows(2).map(|el| el[0] + el[1]).collect();
next_row.insert(0, last_row[0]);
next_row.push(*last_row.last().unwrap());
next_row
}
}
fn main() {
let abbb = PascalsTriangle::new(3);
let v = vec![1];
abbb.rows();
}