From c43287d0eb168e40a855334d7e51ddedd9217aea Mon Sep 17 00:00:00 2001 From: Mariano Riefolo Date: Sat, 28 Sep 2024 09:38:00 +0200 Subject: [PATCH] Wrote program for Day 65 --- README.md | 2 +- .../Day-65_ISBN-Validator/day65/Cargo.toml | 6 ++++ .../Day-65_ISBN-Validator/day65/src/lib.rs | 19 ++++++++++ .../Day-65_ISBN-Validator/day65/src/main.rs | 20 +++++++++++ .../day65/tests/examples.rs | 35 +++++++++++++++++++ 5 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 Week-10/Day-65_ISBN-Validator/day65/Cargo.toml create mode 100644 Week-10/Day-65_ISBN-Validator/day65/src/lib.rs create mode 100644 Week-10/Day-65_ISBN-Validator/day65/src/main.rs create mode 100644 Week-10/Day-65_ISBN-Validator/day65/tests/examples.rs diff --git a/README.md b/README.md index b0c511b..6028cbe 100644 --- a/README.md +++ b/README.md @@ -111,7 +111,7 @@ We encourage you to share your progress and ask questions in the Discussions sec | Day #62 | [Funny Plant](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-62_Funny-Plant) | :white_check_mark: | | Day #63 | [The Rabbit Problem](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-63_The-Rabbit-Problem) | :white_check_mark: | | Day #64 | [First Recurring Character](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-10/Day-64_First-Recurring-Character) | :white_check_mark: | -| Day #65 | [ISBN Validator](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-10/Day-65_ISBN-Validator) | :white_large_square: | +| Day #65 | [ISBN Validator](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-10/Day-65_ISBN-Validator) | :white_check_mark: | | Day #66 | [ISBN Generator](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-10/Day-66_ISBN-Generator) | :white_large_square: | | Day #67 | [Color Maze](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-10/Day-67_Color-Maze) | :white_large_square: | | Day #68 | [Clarence The Slow Typist](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-10/Day-68_Clarence-The-Slow-Typist) | :white_large_square: | diff --git a/Week-10/Day-65_ISBN-Validator/day65/Cargo.toml b/Week-10/Day-65_ISBN-Validator/day65/Cargo.toml new file mode 100644 index 0000000..49a3940 --- /dev/null +++ b/Week-10/Day-65_ISBN-Validator/day65/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day65" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/Week-10/Day-65_ISBN-Validator/day65/src/lib.rs b/Week-10/Day-65_ISBN-Validator/day65/src/lib.rs new file mode 100644 index 0000000..4554e6d --- /dev/null +++ b/Week-10/Day-65_ISBN-Validator/day65/src/lib.rs @@ -0,0 +1,19 @@ +pub fn isbn_validator(digits: &str) -> Result { + if digits.len() != 10 { + return Err("Invalid length for the ISBN".to_owned()); + } + let mut val = 0; + for (i, digit) in digits[..digits.len() - 1].chars().enumerate() { + if !digit.is_ascii_digit() { + return Err("Only digits allowed".to_owned()); + } + val += (digit as u8 - b'0') * (10u8 - i as u8); + } + let digit = digits.chars().last().unwrap(); + if digit == 'X' { + val += 10; + } else { + val += digit as u8 - b'0'; + } + Ok(val % 11 == 0) +} diff --git a/Week-10/Day-65_ISBN-Validator/day65/src/main.rs b/Week-10/Day-65_ISBN-Validator/day65/src/main.rs new file mode 100644 index 0000000..3a1d517 --- /dev/null +++ b/Week-10/Day-65_ISBN-Validator/day65/src/main.rs @@ -0,0 +1,20 @@ +use std::{ + io::{self, Write}, + process::exit, +}; + +use day65::isbn_validator; + +fn main() { + let mut buffer = String::new(); + print!("Insert a ISBN to check if it's valid: "); + io::stdout().flush().unwrap(); + io::stdin().read_line(&mut buffer).unwrap(); + match isbn_validator(buffer.trim()) { + Ok(x) => println!("The ISBN is {}valid", if x { "" } else { "not " }), + Err(e) => { + eprintln!("{e}"); + exit(1); + } + } +} diff --git a/Week-10/Day-65_ISBN-Validator/day65/tests/examples.rs b/Week-10/Day-65_ISBN-Validator/day65/tests/examples.rs new file mode 100644 index 0000000..2e5b61c --- /dev/null +++ b/Week-10/Day-65_ISBN-Validator/day65/tests/examples.rs @@ -0,0 +1,35 @@ +#[cfg(test)] +mod examples { + use day65::isbn_validator; + + #[test] + fn example1() { + assert!(isbn_validator("0747532699").unwrap()); + } + + #[test] + fn example2() { + assert!(isbn_validator("123456789X").unwrap()); + } + + #[test] + fn example3() { + assert!(!isbn_validator("1234567890").unwrap()); + } + + #[test] + fn example4() { + assert_eq!( + isbn_validator("abcdefghil"), + Err("Only digits allowed".to_owned()) + ) + } + + #[test] + fn example5() { + assert_eq!( + isbn_validator("123"), + Err("Invalid length for the ISBN".to_owned()) + ) + } +}