Wrote program for Day 48

This commit is contained in:
Mariano Riefolo 2024-09-11 12:24:58 +02:00
parent cd79043caa
commit a4927ec7a7
5 changed files with 68 additions and 1 deletions

View File

@ -94,7 +94,7 @@ We encourage you to share your progress and ask questions in the Discussions sec
| Day #45 | [Subtract The Swapped Bits...](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-07/Day-45_Subtract-The-Swapped-Bits-Without-Temp-Storage) | :white_check_mark: |
| Day #46 | [Hot Pics Of Danny Devito](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-07/Day-46_Hot-Pics-Of-Danny-Devito) | :white_check_mark: |
| Day #47 | [Zip It](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-07/Day-47_Zip-It) | :white_check_mark: |
| Day #48 | [Christmas Tree](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-07/Day-48_Christmas-Tree) | :white_large_square: |
| Day #48 | [Christmas Tree](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-07/Day-48_Christmas-Tree) | :white_check_mark: |
| Day #49 | [Swimming Pool](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-07/Day-49_Swimming-Pool) | :white_large_square: |
| Day #50 | [Tic Tac Toe](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-50_Tic-Tac-Toe) | :white_large_square: |
| Day #51 | [Asteroid Collision](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-51_Asteroid-Collision) | :white_large_square: |

View File

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

View File

@ -0,0 +1,12 @@
pub fn tree(height: usize) -> Vec<String> {
let mut tree = Vec::new();
for i in 0..height {
tree.push(format!(
"{}{}{}",
" ".repeat(height - i - 1),
"#".repeat(i * 2 + 1),
" ".repeat(height - i - 1)
));
}
tree
}

View File

@ -0,0 +1,16 @@
use std::io::{self, Write};
use day48::tree;
fn main() {
print!("Insert the height of the tree: ");
io::stdout().flush().unwrap();
let mut buffer = String::new();
io::stdin().read_line(&mut buffer).unwrap();
let height: usize = buffer.trim().parse().unwrap();
let tree = tree(height);
println!("{:#?}", tree);
}

View File

@ -0,0 +1,33 @@
#[cfg(test)]
mod examples {
use day48::tree;
#[test]
fn test1() {
assert_eq!(tree(1), vec!["#"]);
}
#[test]
fn test2() {
assert_eq!(tree(2), vec![" # ", "###"]);
}
#[test]
fn test3() {
assert_eq!(
tree(5),
vec![
" # ",
" ### ",
" ##### ",
" ####### ",
"#########"
]
);
}
#[test]
fn test4() {
assert_eq!(tree(0), Vec::<String>::new());
}
}