Wrote program for Day 65

This commit is contained in:
Mariano Riefolo 2024-09-28 09:38:00 +02:00
parent 3fc852ef4d
commit c43287d0eb
5 changed files with 81 additions and 1 deletions

View File

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

View File

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

View File

@ -0,0 +1,19 @@
pub fn isbn_validator(digits: &str) -> Result<bool, String> {
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)
}

View File

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

View File

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