From 09dccbbc8a00daa3e4a0a3749101ef665f2b3f30 Mon Sep 17 00:00:00 2001 From: Mariano Riefolo Date: Wed, 25 Sep 2024 10:13:56 +0200 Subject: [PATCH] Wrote program for Day 62 --- README.md | 2 +- Week-09/Day-62_Funny-Plant/day62/Cargo.toml | 6 ++++ Week-09/Day-62_Funny-Plant/day62/src/lib.rs | 11 ++++++ Week-09/Day-62_Funny-Plant/day62/src/main.rs | 35 +++++++++++++++++++ .../day62/tests/examples.rs | 19 ++++++++++ 5 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 Week-09/Day-62_Funny-Plant/day62/Cargo.toml create mode 100644 Week-09/Day-62_Funny-Plant/day62/src/lib.rs create mode 100644 Week-09/Day-62_Funny-Plant/day62/src/main.rs create mode 100644 Week-09/Day-62_Funny-Plant/day62/tests/examples.rs diff --git a/README.md b/README.md index 857c5a3..5e68afb 100644 --- a/README.md +++ b/README.md @@ -108,7 +108,7 @@ We encourage you to share your progress and ask questions in the Discussions sec | Day #59 | [Perfectly Balanced](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-59_Perfectly-Balanced) | :white_check_mark: | | Day #60 | [A Game Of Threes](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-60_A-Game-Of-Thrones) | :white_check_mark: | | Day #61 | [Write A Web Crawler](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-61_Write-A-Web-Crawler) | :white_check_mark: | -| Day #62 | [Funny Plant](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-62_Funny-Plant) | :white_large_square: | +| Day #62 | [Funny Plant](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-62_Funny-Plant) | :white_check_mark: | | Day #63 | [The Rabbit Problem](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-63_The-Rabbit-Problem) | :white_large_square: | | Day #64 | [First Recurring Character](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-10/Day-64_First-Recurring-Character) | :white_large_square: | | Day #65 | [ISBN Validator](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-10/Day-65_ISBN-Validator) | :white_large_square: | diff --git a/Week-09/Day-62_Funny-Plant/day62/Cargo.toml b/Week-09/Day-62_Funny-Plant/day62/Cargo.toml new file mode 100644 index 0000000..01a9b1e --- /dev/null +++ b/Week-09/Day-62_Funny-Plant/day62/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day62" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/Week-09/Day-62_Funny-Plant/day62/src/lib.rs b/Week-09/Day-62_Funny-Plant/day62/src/lib.rs new file mode 100644 index 0000000..93a8cd2 --- /dev/null +++ b/Week-09/Day-62_Funny-Plant/day62/src/lib.rs @@ -0,0 +1,11 @@ +pub fn funny_plant(people: usize, fruits: usize) -> usize { + let mut weeks = 1; + let mut increment = 0; + let mut plants = fruits; + while increment < people { + increment += plants; + plants += increment; + weeks += 1; + } + weeks +} diff --git a/Week-09/Day-62_Funny-Plant/day62/src/main.rs b/Week-09/Day-62_Funny-Plant/day62/src/main.rs new file mode 100644 index 0000000..a822940 --- /dev/null +++ b/Week-09/Day-62_Funny-Plant/day62/src/main.rs @@ -0,0 +1,35 @@ +use std::{ + io::{self, Write}, + process::exit, +}; + +use day62::funny_plant; + +fn main() { + let mut buffer = String::new(); + print!("Enter the number of people to fed: "); + io::stdout().flush().unwrap(); + io::stdin().read_line(&mut buffer).unwrap(); + let people = match buffer.trim().parse() { + Ok(x) => x, + Err(_) => { + eprintln!("Not a valid number"); + exit(1); + } + }; + + buffer.clear(); + print!("Enter the number of fruits: "); + io::stdout().flush().unwrap(); + io::stdin().read_line(&mut buffer).unwrap(); + let fruits = match buffer.trim().parse() { + Ok(x) => x, + Err(_) => { + eprintln!("Not a valid number"); + exit(1); + } + }; + + let weeks = funny_plant(people, fruits); + println!("The minimum number of weeks is {weeks}"); +} diff --git a/Week-09/Day-62_Funny-Plant/day62/tests/examples.rs b/Week-09/Day-62_Funny-Plant/day62/tests/examples.rs new file mode 100644 index 0000000..181b5b1 --- /dev/null +++ b/Week-09/Day-62_Funny-Plant/day62/tests/examples.rs @@ -0,0 +1,19 @@ +#[cfg(test)] +mod examples { + use day62::funny_plant; + + #[test] + fn example1() { + assert_eq!(funny_plant(200, 15), 5); + } + + #[test] + fn example2() { + assert_eq!(funny_plant(50000, 1), 14); + } + + #[test] + fn example3() { + assert_eq!(funny_plant(150000, 250), 9); + } +}