Wrote program for Day 32
This commit is contained in:
parent
32e0240d0a
commit
836fef728d
@ -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: |
|
||||
|
6
Week-05/Day-32_Climbing-The-Leaderboard/day32/Cargo.toml
Normal file
6
Week-05/Day-32_Climbing-The-Leaderboard/day32/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "day32"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
26
Week-05/Day-32_Climbing-The-Leaderboard/day32/src/lib.rs
Normal file
26
Week-05/Day-32_Climbing-The-Leaderboard/day32/src/lib.rs
Normal file
@ -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<usize> {
|
||||
let mut result = Vec::new();
|
||||
|
||||
for score in player_scores.iter() {
|
||||
let pos = get_player_pos(ranked_scores, score);
|
||||
result.push(pos);
|
||||
}
|
||||
|
||||
result
|
||||
}
|
54
Week-05/Day-32_Climbing-The-Leaderboard/day32/src/main.rs
Normal file
54
Week-05/Day-32_Climbing-The-Leaderboard/day32/src/main.rs
Normal file
@ -0,0 +1,54 @@
|
||||
use std::{
|
||||
io::{self, Write},
|
||||
process::exit,
|
||||
};
|
||||
|
||||
use day32::climb_leaderboard;
|
||||
|
||||
fn read_usize_vector(request: &str) -> Result<Vec<usize>, 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::<Result<Vec<_>, _>>()
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
@ -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]
|
||||
)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user