Wrote program for Day 65
This commit is contained in:
parent
3fc852ef4d
commit
c43287d0eb
@ -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: |
|
||||
|
6
Week-10/Day-65_ISBN-Validator/day65/Cargo.toml
Normal file
6
Week-10/Day-65_ISBN-Validator/day65/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "day65"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
19
Week-10/Day-65_ISBN-Validator/day65/src/lib.rs
Normal file
19
Week-10/Day-65_ISBN-Validator/day65/src/lib.rs
Normal 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)
|
||||
}
|
20
Week-10/Day-65_ISBN-Validator/day65/src/main.rs
Normal file
20
Week-10/Day-65_ISBN-Validator/day65/src/main.rs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
35
Week-10/Day-65_ISBN-Validator/day65/tests/examples.rs
Normal file
35
Week-10/Day-65_ISBN-Validator/day65/tests/examples.rs
Normal 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())
|
||||
)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user