From 0c44dc91f8f034d733838af1d7e91f8e567edcbc Mon Sep 17 00:00:00 2001 From: Mariano Riefolo Date: Fri, 16 Aug 2024 12:00:25 +0200 Subject: [PATCH] Wrote program for Day 22 https://github.com/LiveGray/100-Days-Of-Rust/issues/6 --- README.md | 2 +- .../day22/Cargo.toml | 6 +++++ .../day22/src/lib.rs | 7 ++++++ .../day22/src/main.rs | 22 +++++++++++++++++++ .../day22/tests/example.rs | 9 ++++++++ 5 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 Week-04/Day-22_Marcio-Mellos-Challenge/day22/Cargo.toml create mode 100644 Week-04/Day-22_Marcio-Mellos-Challenge/day22/src/lib.rs create mode 100644 Week-04/Day-22_Marcio-Mellos-Challenge/day22/src/main.rs create mode 100644 Week-04/Day-22_Marcio-Mellos-Challenge/day22/tests/example.rs diff --git a/README.md b/README.md index 7440fd2..8464510 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ We encourage you to share your progress and ask questions in the Discussions sec | Day #19 | [URL Shortener](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-19_URL-Shortener) | :white_check_mark: | | Day #20 | [API Challenge](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-20_API-Challenge) | :white_check_mark: | | Day #21 | [Random Maze Generator](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-21_Random-Maze-Generator) | :white_check_mark: | -| Day #22 | [Marcio Mellos Challenge](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-22_Marcio-Mellos-Challenge) | :white_large_square: | +| Day #22 | [Marcio Mellos Challenge](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-22_Marcio-Mellos-Challenge) | :white_check_mark: | | Day #23 | [The Dining Philosophers](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-23_The-Dining_Philosophers) | :white_large_square: | | Day #24 | [The Josephus Problem](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-24_The-Josephus-Problem) | :white_large_square: | | Day #25 | [Coin Trouble](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-25_Coin-Trouble) | :white_large_square: | diff --git a/Week-04/Day-22_Marcio-Mellos-Challenge/day22/Cargo.toml b/Week-04/Day-22_Marcio-Mellos-Challenge/day22/Cargo.toml new file mode 100644 index 0000000..07fb531 --- /dev/null +++ b/Week-04/Day-22_Marcio-Mellos-Challenge/day22/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day22" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/Week-04/Day-22_Marcio-Mellos-Challenge/day22/src/lib.rs b/Week-04/Day-22_Marcio-Mellos-Challenge/day22/src/lib.rs new file mode 100644 index 0000000..1654589 --- /dev/null +++ b/Week-04/Day-22_Marcio-Mellos-Challenge/day22/src/lib.rs @@ -0,0 +1,7 @@ +pub fn area_to_fields(area: f64) -> f64 { + let field_area = 105f64 * 68f64; + let area_m2 = area * 1000f64 * 1000f64; + let result = area_m2 / field_area; + let integer_result: usize = (result * 1000f64) as usize; + integer_result as f64 / 1000f64 +} diff --git a/Week-04/Day-22_Marcio-Mellos-Challenge/day22/src/main.rs b/Week-04/Day-22_Marcio-Mellos-Challenge/day22/src/main.rs new file mode 100644 index 0000000..ca44eda --- /dev/null +++ b/Week-04/Day-22_Marcio-Mellos-Challenge/day22/src/main.rs @@ -0,0 +1,22 @@ +use std::io::{self, Write}; + +use day22::area_to_fields; + +fn main() { + let mut buffer = String::new(); + + print!("Insert the deforested area in km2: "); + io::stdout().flush().expect("Failed to flush stdout"); + + io::stdin() + .read_line(&mut buffer) + .expect("Failed to read line"); + + let area = buffer.trim().parse().expect("The area must be an integer"); + + let fields = area_to_fields(area); + println!( + "{} km2 is equivalent to {:.3} football fields", + area, fields + ); +} diff --git a/Week-04/Day-22_Marcio-Mellos-Challenge/day22/tests/example.rs b/Week-04/Day-22_Marcio-Mellos-Challenge/day22/tests/example.rs new file mode 100644 index 0000000..bc14be8 --- /dev/null +++ b/Week-04/Day-22_Marcio-Mellos-Challenge/day22/tests/example.rs @@ -0,0 +1,9 @@ +#[cfg(test)] +mod example { + use day22::area_to_fields; + + #[test] + fn example() { + assert_eq!(area_to_fields(1.034), 144.817); + } +}