Wrote program for Day 37

This commit is contained in:
Mariano Riefolo 2024-08-31 10:34:25 +02:00
parent 27b2603b3c
commit 08c85f8d72
5 changed files with 64 additions and 1 deletions

View File

@ -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: |

View File

@ -0,0 +1,6 @@
[package]
name = "day37"
version = "0.1.0"
edition = "2021"
[dependencies]

View File

@ -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
}

View File

@ -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<usize> = 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
);
}

View File

@ -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));
}
}