Created
July 3, 2021 14:46
-
-
Save NickyMeuleman/9bfcef3eb792cb28865fa602a0dbb92f to your computer and use it in GitHub Desktop.
exercism.io isbn-verifier
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Determines whether the supplied string is a valid ISBN number | |
pub fn is_valid_isbn(isbn: &str) -> bool { | |
let numbers: Vec<u32> = isbn | |
.chars() | |
.filter_map(|c| match c { | |
'X' if Some('X') == isbn.chars().last() => Some(10), | |
_ => c.to_digit(10), | |
}) | |
.collect(); | |
if numbers.len() != 10 { | |
return false; | |
}; | |
let sum: u32 = (1..=10).rev().zip(numbers).map(|(n1, n2)| n1 * n2).sum(); | |
sum % 11 == 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment