Skip to content

Add isbn-verifier exercise #924

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions exercises/isbn-verifier/canonical-data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
{
"exercise": "isbn-verifier",
"version": "1.0.0",
"comments": [
"An expected value of true indicates a valid ISBN-10, ",
"whereas false means the ISBN-10 is invalid."
],
"cases": [
{
"description": "valid isbn number",
"property": "isbn",
"input": "3-598-21508-8",
"expected": true
},
{
"description": "invalid isbn check digit",
"property": "isbn",
"input": "3-598-21508-9",
"expected": false
},
{
"description": "valid isbn number with a check digit of 10",
"property": "isbn",
"input": "3-598-21507-X",
"expected": true
},
{
"description": "check digit is a character other than X",
"property": "isbn",
"input": "3-598-21507-A",
"expected": false
},
{
"description": "invalid character in isbn",
"property": "isbn",
"input": "3-598-2K507-0",
"expected": false
},
{
"description": "X is only valid as a check digit",
"property": "isbn",
"input": "3-598-2X507-0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An implementation that unconditionally treats X as 10 will still reject this ISBN:

3 * 10 + 5 * 9 + 9 * 8 + 8 * 7 + 2 * 6 + 10 * 5 + 5 * 4 + 0 * 3 + 7 * 2 + 0 * 1 = 299, and 299 is not divisible by 11.

If the intention of this test is to ensure that an implementation only treats X as 10 in the check digit position, then a case such as "3-598-2X507-9" is needed.

"expected": false
},
{
"description": "valid isbn without separating dashes",
"property": "isbn",
"input": "3598215088",
"expected": true
},
{
"description": "isbn without separating dashes and X as check digit",
"property": "isbn",
"input": "359821507X",
"expected": true
},
{
"description": "isbn without check digit and dashes",
"property": "isbn",
"input": "359821507",
"expected": false
},
{
"description": "too long isbn and no dashes",
"property": "isbn",
"input": "3598215078X",
"expected": false
},
{
"description": "isbn without check digit",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

duplicate description

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

"property": "isbn",
"input": "3-598-21507",
"expected": false
},
{
"description": "too long isbn",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

duplicate description

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

"property": "isbn",
"input": "3-598-21507-XA",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An implementation that would potentially attempt to checksum ISBNs that are too long would still reject this input because it would see that the "A" is invalid.

If the intention of this case is to ensure that an implementation does in fact reject ISBNs that are too long, at the very least a valid character should be used instead of A.

If one wanted to be thorough, one might have to include three different cases here, respectively checking for implementations that try to checksum:

  • all 11 resulting digits (with a coefficient of 11 on the leftmost one, naturally)
  • the 10 rightmost digits
  • the 10 leftmost digits

by providing a too-long ISBN that has a valid check digit under each respective scheme.

"expected": false
},
{
"description": "check digit of X should not be used for 0",
"property": "isbn",
"input": "3-598-21515-X",
"expected": false
}
]
}
35 changes: 35 additions & 0 deletions exercises/isbn-verifier/description.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Check if a given ISBN-10 is valid.

## Functionality

Given an unkown 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
4 changes: 4 additions & 0 deletions exercises/isbn-verifier/metadata.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
blurb: "Check if a given string is a valid ISBN-10 number."
source: "Converting a string into a number and some basic processing utilizing a relatable real world example."
source_url: "https://en.wikipedia.org/wiki/International_Standard_Book_Number#ISBN-10_check_digit_calculation"