Wrote program for Day 42

This commit is contained in:
Mariano Riefolo 2024-09-05 11:19:45 +02:00
parent dc46978ac4
commit 361017a754
5 changed files with 51 additions and 1 deletions

View File

@ -88,7 +88,7 @@ We encourage you to share your progress and ask questions in the Discussions sec
| Day #39 | [Halloween Sale](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-39_Halloween-Sale) | :white_check_mark: |
| Day #40 | [Larrys Array](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-40_Larrys-Array) | :white_check_mark: |
| Day #41 | [Sales By Match](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-41_Sales-By-Match) | :white_check_mark: |
| Day #42 | [Drawing Book](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-42_Drawing-Book) | :white_large_square: |
| Day #42 | [Drawing Book](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-42_Drawing-Book) | :white_check_mark: |
| Day #43 | [Area Of A Triangle](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-07/Day-43_Area-Of-A-Triangle) | :white_large_square: |
| Day #44 | [Maximum Edge Of A Triangle](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-07/Day-44_Maximum-Edge-Of-A-Triangle) | :white_large_square: |
| Day #45 | [Subtract The Swapped Bits...](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-07/Day-45_Subtract-The-Swapped-Bits-Without-Temp-Storage) | :white_large_square: |

View File

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

View File

@ -0,0 +1,3 @@
pub fn min_pages(n: usize, p: usize) -> usize {
std::cmp::min(p / 2, (n + (n + 1) % 2 - p) / 2)
}

View File

@ -0,0 +1,32 @@
use day42::min_pages;
fn read_usize(request: &str) -> Result<usize, std::num::ParseIntError> {
use std::io::{self, Write};
print!("{}", request);
io::stdout().flush().expect("Failed to flush stdout.");
let mut input = String::new();
io::stdin()
.read_line(&mut input)
.expect("Failed to read from stdin.");
input.trim().parse()
}
fn main() {
let n = match read_usize("Insert the number of pages: ") {
Ok(n) => n,
Err(e) => {
eprintln!("Error: {}", e);
return;
}
};
let p = match read_usize("Insert the page number: ") {
Ok(p) => p,
Err(e) => {
eprintln!("Error: {}", e);
return;
}
};
let result = min_pages(n, p);
println!("The minimum number of pages to turn is: {}", result);
}

View File

@ -0,0 +1,9 @@
#[cfg(test)]
mod example {
use day42::min_pages;
#[test]
fn test() {
assert_eq!(min_pages(5, 3), 1);
}
}