From 836fef728df1dd2d397f04ac6b14c2136371fec0 Mon Sep 17 00:00:00 2001 From: Mariano Riefolo Date: Mon, 26 Aug 2024 12:59:39 +0200 Subject: [PATCH] Wrote program for Day 32 --- README.md | 2 +- .../day32/Cargo.toml | 6 +++ .../day32/src/lib.rs | 26 +++++++++ .../day32/src/main.rs | 54 +++++++++++++++++++ .../day32/tests/examples.rs | 20 +++++++ 5 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 Week-05/Day-32_Climbing-The-Leaderboard/day32/Cargo.toml create mode 100644 Week-05/Day-32_Climbing-The-Leaderboard/day32/src/lib.rs create mode 100644 Week-05/Day-32_Climbing-The-Leaderboard/day32/src/main.rs create mode 100644 Week-05/Day-32_Climbing-The-Leaderboard/day32/tests/examples.rs diff --git a/README.md b/README.md index 0ad0e8b..beeb11b 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ We encourage you to share your progress and ask questions in the Discussions sec | Day #29 | [Traffic Light Checker](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-29_Traffic-Light-Checker) | :white_check_mark: | | Day #30 | [The Maximum Value](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-30_The-Maximum-Value) | :white_check_mark: | | Day #31 | [The Time In Words](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-31_The-Time-In-Words) | :white_check_mark: | -| Day #32 | [Climbing The Leaderboard](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-32_Climbing-The-Leaderboard) | :white_large_square: | +| Day #32 | [Climbing The Leaderboard](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-32_Climbing-The-Leaderboard) | :white_check_mark: | | Day #33 | [WERTYU](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-33_WERTYU) | :white_large_square: | | Day #34 | [Primary Arithmetic](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-34_Primary-Arithmetic) | :white_large_square: | | Day #35 | [Dog And Gopher](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-35_Dog-And-Gopher) | :white_large_square: | diff --git a/Week-05/Day-32_Climbing-The-Leaderboard/day32/Cargo.toml b/Week-05/Day-32_Climbing-The-Leaderboard/day32/Cargo.toml new file mode 100644 index 0000000..3017cf5 --- /dev/null +++ b/Week-05/Day-32_Climbing-The-Leaderboard/day32/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day32" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/Week-05/Day-32_Climbing-The-Leaderboard/day32/src/lib.rs b/Week-05/Day-32_Climbing-The-Leaderboard/day32/src/lib.rs new file mode 100644 index 0000000..3e374bd --- /dev/null +++ b/Week-05/Day-32_Climbing-The-Leaderboard/day32/src/lib.rs @@ -0,0 +1,26 @@ +fn get_player_pos(leaderboard: &[usize], score: &usize) -> usize { + let mut position = 1; + for (i, lb_score) in leaderboard[1..].iter().enumerate() { + if score > &leaderboard[i] { + return position; + } + if &leaderboard[i] > lb_score { + position += 1; + } + if score == lb_score { + return position; + } + } + position + 1 +} + +pub fn climb_leaderboard(ranked_scores: &[usize], player_scores: &[usize]) -> Vec { + let mut result = Vec::new(); + + for score in player_scores.iter() { + let pos = get_player_pos(ranked_scores, score); + result.push(pos); + } + + result +} diff --git a/Week-05/Day-32_Climbing-The-Leaderboard/day32/src/main.rs b/Week-05/Day-32_Climbing-The-Leaderboard/day32/src/main.rs new file mode 100644 index 0000000..b5a862b --- /dev/null +++ b/Week-05/Day-32_Climbing-The-Leaderboard/day32/src/main.rs @@ -0,0 +1,54 @@ +use std::{ + io::{self, Write}, + process::exit, +}; + +use day32::climb_leaderboard; + +fn read_usize_vector(request: &str) -> Result, String> { + let mut buffer = String::new(); + + print!("{}", request); + io::stdout().flush().expect("Failed to flush"); + + io::stdin() + .read_line(&mut buffer) + .expect("Failed to read line"); + + buffer + .split_whitespace() + .map(|x| { + x.parse() + .map_err(|_| "Text must be made of numbers separated by spaces".to_string()) + }) + .collect::, _>>() +} + +fn main() { + let ranked_scores = + match read_usize_vector("Insert the leaderboard (space separated positive integers): ") { + Ok(x) => x, + Err(e) => { + eprintln!("{}", e); + exit(1); + } + }; + + if ranked_scores.windows(2).any(|x| x[0] < x[1]) { + eprintln!("The leaderboard must be sorted in descending order"); + exit(1); + } + + let player_scores = + match read_usize_vector("Insert the player's scores (space separated positive integers): ") + { + Ok(x) => x, + Err(e) => { + eprintln!("{}", e); + exit(1); + } + }; + + let result = climb_leaderboard(&ranked_scores, &player_scores); + println!("Result: {:?}", result); +} diff --git a/Week-05/Day-32_Climbing-The-Leaderboard/day32/tests/examples.rs b/Week-05/Day-32_Climbing-The-Leaderboard/day32/tests/examples.rs new file mode 100644 index 0000000..3601d11 --- /dev/null +++ b/Week-05/Day-32_Climbing-The-Leaderboard/day32/tests/examples.rs @@ -0,0 +1,20 @@ +#[cfg(test)] +mod examples { + use day32::climb_leaderboard; + + #[test] + fn example1() { + assert_eq!( + climb_leaderboard(&[100, 90, 90, 80], &[70, 80, 105]), + [4, 3, 1] + ) + } + + #[test] + fn example2() { + assert_eq!( + climb_leaderboard(&[100, 90, 90, 80], &[106, 107, 105]), + [1, 1, 1] + ) + } +}