Skip to content

Instantly share code, notes, and snippets.

@NickyMeuleman
Created July 3, 2021 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NickyMeuleman/9bfcef3eb792cb28865fa602a0dbb92f to your computer and use it in GitHub Desktop.
Save NickyMeuleman/9bfcef3eb792cb28865fa602a0dbb92f to your computer and use it in GitHub Desktop.
exercism.io isbn-verifier
/// 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