From 08c85f8d72da2aed505996614ea893bfe7cbda54 Mon Sep 17 00:00:00 2001 From: Mariano Riefolo Date: Sat, 31 Aug 2024 10:34:25 +0200 Subject: [PATCH] Wrote program for Day 37 --- README.md | 2 +- .../day37/Cargo.toml | 6 ++++ .../day37/src/lib.rs | 15 +++++++++ .../day37/src/main.rs | 33 +++++++++++++++++++ .../day37/tests/example.rs | 9 +++++ 5 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 Week-06/Day-37_Breaking-The-Records/day37/Cargo.toml create mode 100644 Week-06/Day-37_Breaking-The-Records/day37/src/lib.rs create mode 100644 Week-06/Day-37_Breaking-The-Records/day37/src/main.rs create mode 100644 Week-06/Day-37_Breaking-The-Records/day37/tests/example.rs diff --git a/README.md b/README.md index 7c5a406..3143d1c 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,7 @@ We encourage you to share your progress and ask questions in the Discussions sec | Day #34 | [Primary Arithmetic](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-34_Primary-Arithmetic) | :white_check_mark: | | Day #35 | [Dog And Gopher](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-35_Dog-And-Gopher) | :white_check_mark: | | Day #36 | [LCD Display](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-36_LCD-Display) | :white_check_mark: | -| Day #37 | [Breaking The Records](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-37_Breaking-The-Records) | :white_large_square: | +| Day #37 | [Breaking The Records](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-37_Breaking-The-Records) | :white_check_mark: | | Day #38 | [Electronics Shop](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-38_Electronics-Shop) | :white_large_square: | | Day #39 | [Halloween Sale](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-39_Halloween-Sale) | :white_large_square: | | Day #40 | [Larrys Array](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-40_Larrys-Array) | :white_large_square: | diff --git a/Week-06/Day-37_Breaking-The-Records/day37/Cargo.toml b/Week-06/Day-37_Breaking-The-Records/day37/Cargo.toml new file mode 100644 index 0000000..0fdf155 --- /dev/null +++ b/Week-06/Day-37_Breaking-The-Records/day37/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day37" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/Week-06/Day-37_Breaking-The-Records/day37/src/lib.rs b/Week-06/Day-37_Breaking-The-Records/day37/src/lib.rs new file mode 100644 index 0000000..e999ec7 --- /dev/null +++ b/Week-06/Day-37_Breaking-The-Records/day37/src/lib.rs @@ -0,0 +1,15 @@ +pub fn breaking_records(scores: &[usize]) -> (usize, usize) { + let mut result = (0, 0); + let mut min = &scores[0]; + let mut max = &scores[0]; + for score in scores[1..].iter() { + if score < min { + min = score; + result.1 += 1; + } else if score > max { + max = score; + result.0 += 1; + } + } + result +} diff --git a/Week-06/Day-37_Breaking-The-Records/day37/src/main.rs b/Week-06/Day-37_Breaking-The-Records/day37/src/main.rs new file mode 100644 index 0000000..d88d8b3 --- /dev/null +++ b/Week-06/Day-37_Breaking-The-Records/day37/src/main.rs @@ -0,0 +1,33 @@ +use std::{ + io::{self, Write}, + process::exit, +}; + +use day37::breaking_records; + +fn main() { + print!("Insert the scores separated by spaces: "); + io::stdout().flush().expect("Failed to flush stdout"); + + let mut buffer = String::new(); + io::stdin() + .read_line(&mut buffer) + .expect("Failed to read line"); + + let scores: Vec = buffer + .split_whitespace() + .map(|x| match x.parse() { + Ok(x) => x, + Err(e) => { + eprintln!("{}", e); + exit(1); + } + }) + .collect(); + + let result = breaking_records(&scores); + println!( + "Maria broke her records {} times for the most and {} for the least points.", + result.0, result.1 + ); +} diff --git a/Week-06/Day-37_Breaking-The-Records/day37/tests/example.rs b/Week-06/Day-37_Breaking-The-Records/day37/tests/example.rs new file mode 100644 index 0000000..47fa9e4 --- /dev/null +++ b/Week-06/Day-37_Breaking-The-Records/day37/tests/example.rs @@ -0,0 +1,9 @@ +#[cfg(test)] +mod example { + use day37::breaking_records; + + #[test] + fn example() { + assert_eq!(breaking_records(&[10, 5, 20, 20, 4, 5, 2, 25, 1]), (2, 4)); + } +}