The exercise checks how well and optimally you can parse a string to check if an ISBN is correct.
Important things are:
The string can have multiple dashes or zero dashes. Your parser needs to omit all the dashes.
If the digits are separated by something other than dashes, the ISBN is invalid.
The code below shows an example of how you can omit all characters that are not digits and pass all the tests.
pub fn is_valid_isbn(isbn: &str) -> bool {
let mut acc = 0;
let mut factor = 10;
for character in isbn.chars() {
if factor == 0 { return false; }
if let Some(n) = character.to_digit(10) {
acc += n * factor;
factor -= 1;
} else if character == 'X' && factor == 1 {
acc += 10;
factor -= 1;
}
}
factor == 0 && acc % 11 == 0
}
If you add test i give at the beginning the code will not pass. And I believe the important thing in the exercise is how nicely you can parse the string. And this code in my opinion is wrong.
Don’t we already have tests for those? The canonical data shows tests with zero and multiple dashes for valid ISBNs. The canonical data shows tests with something other than dashes, where the ISBN is invalid. What does your test add that isn’t already in the tests?
The canonical data has tests for an invalid character, "P". Is "?" more “special” than "P" in any way? Is there any reason they should be treated differently in any way?
The test data includes inputs "3-598-P1581-X", "3132P34035", "3598P215088". I’m failing to understand what the proposed test adds which isn’t already covered.
I think you are suggesting that there should be a test which uses a valid 10-digit ISBN plus an 11th character added which is a non-digit. Is that correct?
The third test, 3598P215088, contains 10 digits with a non-digit separator, P. If you remove the P, the input is a valid ISBN value.
It’s entirely possible the Rust track isn’t in sync with the problem specs. You can post a thread and ask if the Rust maintainer would appreciate PRs to sync exercises!