diff --git a/config.json b/config.json index 4c559d575..3ccf2ad72 100644 --- a/config.json +++ b/config.json @@ -189,6 +189,18 @@ "loop" ] }, + { + "uuid": "c986c240-46de-419c-8ed6-700eb68f8db6", + "slug": "isbn-verifier", + "core": false, + "unlocked_by": null, + "difficulty": 4, + "topics": [ + "conversion between string and int", + "loop", + "mathematics" + ] + }, { "uuid": "20e7d347-b80a-4656-ac34-0825126939ff", "slug": "perfect-numbers", diff --git a/exercises/isbn-verifier/Cargo.lock b/exercises/isbn-verifier/Cargo.lock new file mode 100644 index 000000000..c1e9670b9 --- /dev/null +++ b/exercises/isbn-verifier/Cargo.lock @@ -0,0 +1,4 @@ +[root] +name = "isbn-verifier" +version = "1.1.0" + diff --git a/exercises/isbn-verifier/Cargo.toml b/exercises/isbn-verifier/Cargo.toml new file mode 100644 index 000000000..a8e9eb08e --- /dev/null +++ b/exercises/isbn-verifier/Cargo.toml @@ -0,0 +1,3 @@ +[package] +name = "isbn-verifier" +version = "1.1.0" diff --git a/exercises/isbn-verifier/README.md b/exercises/isbn-verifier/README.md new file mode 100644 index 000000000..890fb9961 --- /dev/null +++ b/exercises/isbn-verifier/README.md @@ -0,0 +1,74 @@ +# Isbn Verifier + +Check if a given ISBN-10 is valid. + +## Functionality + +Given an unknown string the program should check if the provided string is a valid ISBN-10. +Putting this into place requires some thinking about preprocessing/parsing of the string prior to calculating the check digit for the ISBN. + +The program should allow for ISBN-10 without the separating dashes to be verified as well. + +## ISBN + +Let's take a random ISBN-10 number, say `3-598-21508-8` for this. +The first digit block indicates the group where the ISBN belongs. Groups can consist of shared languages, geographic regions or countries. The leading '3' signals this ISBN is from a german speaking country. +The following number block is to identify the publisher. Since this is a three digit publisher number there is a 5 digit title number for this book. +The last digit in the ISBN is the check digit which is used to detect read errors. + +The first 9 digits in the ISBN have to be between 0 and 9. +The check digit can additionally be an 'X' to allow 10 to be a valid check digit as well. + +A valid ISBN-10 is calculated with this formula `(x1 * 10 + x2 * 9 + x3 * 8 + x4 * 7 + x5 * 6 + x6 * 5 + x7 * 4 + x8 * 3 + x9 * 2 + x10 * 1) mod 11 == 0` +So for our example ISBN this means: +(3 * 10 + 5 * 9 + 9 * 8 + 8 * 7 + 2 * 6 + 1 * 5 + 5 * 4 + 0 * 3 + 8 * 2 + 8 * 1) mod 11 = 0 + +Which proves that the ISBN is valid. + +## Caveats + +Converting from string to number can be tricky in certain languages. +It's getting even trickier since the check-digit of an ISBN-10 can be 'X'. + +## Bonus tasks + +* Generate a valid ISBN-13 from the input ISBN-10 (and maybe verify it again with a derived verifier) + +* Generate valid ISBN, maybe even from a given starting ISBN +## Rust Installation + +Refer to the [exercism help page][help-page] for Rust installation and learning +resources. + +## Writing the Code + +Execute the tests with: + +```bash +$ cargo test +``` + +All but the first test have been ignored. After you get the first test to +pass, remove the ignore flag (`#[ignore]`) from the next test and get the tests +to pass again. The test file is located in the `tests` directory. You can +also remove the ignore flag from all the tests to get them to run all at once +if you wish. + +Make sure to read the [Crates and Modules](https://doc.rust-lang.org/stable/book/crates-and-modules.html) chapter if you +haven't already, it will help you with organizing your files. + +## Feedback, Issues, Pull Requests + +The [exercism/rust](https://github.com/exercism/rust) repository on GitHub is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the [rust track team](https://github.com/orgs/exercism/teams/rust) are happy to help! + +If you want to know more about Exercism, take a look at the [contribution guide](https://github.com/exercism/docs/blob/master/contributing-to-language-tracks/README.md). + +[help-page]: http://exercism.io/languages/rust +[crates-and-modules]: http://doc.rust-lang.org/stable/book/crates-and-modules.html + +## Source + +Converting a string into a number and some basic processing utilizing a relatable real world example. [https://en.wikipedia.org/wiki/International_Standard_Book_Number#ISBN-10_check_digit_calculation](https://en.wikipedia.org/wiki/International_Standard_Book_Number#ISBN-10_check_digit_calculation) + +## Submitting Incomplete Solutions +It's possible to submit an incomplete solution so you can see how others have completed the exercise. diff --git a/exercises/isbn-verifier/example.rs b/exercises/isbn-verifier/example.rs new file mode 100644 index 000000000..a060d3856 --- /dev/null +++ b/exercises/isbn-verifier/example.rs @@ -0,0 +1,44 @@ +/// An ISBN type +#[derive(PartialEq, Eq)] +enum IsbnType { + Isbn10, + Isbn13, +} + +/// Checks if an 'X' is valid at the given position for the given ISBN type +#[allow(non_snake_case)] +fn is_X_valid(position: &usize, isbn_type: &IsbnType) -> bool { + (isbn_type == &IsbnType::Isbn10 && position == &9 ) || + (isbn_type == &IsbnType::Isbn13 && position == &12) +} + +/// Checks if a '-' is valid at the given position for the given ISBN type +fn is_dash_valid(position: &usize, isbn_type: &IsbnType) -> bool { + isbn_type == &IsbnType::Isbn13 && + (position == &1 || position == &5 || position == &11) +} + +/// Determines whether the supplied string is a valid ISBN number +pub fn is_valid_isbn(isbn: &str) -> bool { + let isbn_type = match isbn.len() { + 10 => IsbnType::Isbn10, + 13 => IsbnType::Isbn13, + _ => return false, + }; + + let mut checksum = 0; + let mut coefficient = 10; + for (position, c) in isbn.char_indices() { + let digit_value = match c { + '0' ... '9' => c.to_digit(10).unwrap(), + 'X' if is_X_valid(&position, &isbn_type) => 10, + '-' if is_dash_valid(&position, &isbn_type) => continue, + _ => return false, + }; + + checksum += coefficient * digit_value; + coefficient -= 1; + } + + checksum % 11 == 0 +} diff --git a/exercises/isbn-verifier/src/lib.rs b/exercises/isbn-verifier/src/lib.rs new file mode 100644 index 000000000..b2b0ad73b --- /dev/null +++ b/exercises/isbn-verifier/src/lib.rs @@ -0,0 +1,4 @@ +/// Determines whether the supplied string is a valid ISBN number +pub fn is_valid_isbn(_isbn: &str) -> bool { + unimplemented!(); +} diff --git a/exercises/isbn-verifier/tests/isbn-verifier.rs b/exercises/isbn-verifier/tests/isbn-verifier.rs new file mode 100644 index 000000000..c6c97df48 --- /dev/null +++ b/exercises/isbn-verifier/tests/isbn-verifier.rs @@ -0,0 +1,83 @@ +extern crate isbn_verifier; + +use isbn_verifier::is_valid_isbn; + +#[test] +fn test_valid() { + assert!(is_valid_isbn("3-598-21508-8")); +} + +#[test] +#[ignore] +fn test_invalid_check_digit() { + assert!(!is_valid_isbn("3-598-21508-9")); +} + +#[test] +#[ignore] +fn test_valid_check_digit_of_10() { + assert!(is_valid_isbn("3-598-21507-X")); +} + +#[test] +#[ignore] +fn test_invalid_character_as_check_digit() { + assert!(!is_valid_isbn("3-598-21507-A")); +} + +#[test] +#[ignore] +fn test_invalid_character_in_isbn() { + assert!(!is_valid_isbn("3-598-2K507-0")); +} + +#[test] +#[ignore] +#[allow(non_snake_case)] +fn test_invalid_isbn_with_invalid_X() { + assert!(!is_valid_isbn("3-598-2X507-9")); +} + +#[test] +#[ignore] +fn test_valid_isbn_without_dashes() { + assert!(is_valid_isbn("3598215088")); +} + +#[test] +#[ignore] +#[allow(non_snake_case)] +fn test_valid_isbn_without_dashes_and_X_as_check() { + assert!(is_valid_isbn("359821507X")); +} + +#[test] +#[ignore] +fn test_invalid_isbn_without_dashes_and_no_check_digit() { + assert!(!is_valid_isbn("359821507")); +} + +#[test] +#[ignore] +fn test_invalid_isbn_without_dashes_and_too_long() { + assert!(!is_valid_isbn("3598215078X")); +} + +#[test] +#[ignore] +fn test_invalid_isbn_without_check_digit() { + assert!(!is_valid_isbn("3-598-21507")); +} + +#[test] +#[ignore] +fn test_invalid_isbn_too_long() { + assert!(!is_valid_isbn("3-598-21507-XX")); +} + +#[test] +#[ignore] +#[allow(non_snake_case)] +fn test_invalid_isbn_with_check_digit_X_instead_of_0() { + assert!(!is_valid_isbn("3-598-21515-X")); +}