The ISBN-10 verification process is used to validate book identification
numbers. These normally contain dashes and look like: 3-598-21508-8
The ISBN-10 format is 9 digits (0 to 9) plus one check character (either a digit or an X only). In the case the check character is an X, this represents the value '10'. These may be communicated with or without hyphens, and can be checked for their validity by the following formula:
(x1 * 10 + x2 * 9 + x3 * 8 + x4 * 7 + x5 * 6 + x6 * 5 + x7 * 4 + x8 * 3 + x9 * 2 + x10 * 1) mod 11 == 0
If the result is 0, then it is a valid ISBN-10, otherwise it is invalid.
Let's take the ISBN-10 3-598-21508-8
. We plug it in to the formula, and get:
(3 * 10 + 5 * 9 + 9 * 8 + 8 * 7 + 2 * 6 + 1 * 5 + 5 * 4 + 0 * 3 + 8 * 2 + 8 * 1) mod 11 == 0
Since the result is 0, this proves that our ISBN is valid.
Given a 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 be able to verify ISBN-10 both with and without separating dashes.
Converting from strings to numbers can be tricky in certain languages.
Now, it's even trickier since the check digit of an ISBN-10 may be 'X' (representing '10'). For instance 3-598-21507-X
is a valid ISBN-10.
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.
Execute the tests with:
$ mix test
In the test suites, all but the first test have been skipped.
Once you get a test passing, you can unskip the next one by
commenting out the relevant @tag :pending
with a #
symbol.
For example:
# @tag :pending
test "shouting" do
assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
end
Or, you can enable all the tests by commenting out the
ExUnit.configure
line in the test suite.
# ExUnit.configure exclude: :pending, trace: true
If you're stuck on something, it may help to look at some of the available resources out there where answers might be found.
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
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
defmodule IsbnVerifierTest do
use ExUnit.Case
# @tag :pending
test "valid isbn number" do
assert IsbnVerifier.isbn?("3-598-21508-8")
end
@tag :pending
test "invalid isbn check digit" do
refute IsbnVerifier.isbn?("3-598-21508-9")
end
@tag :pending
test "valid isbn number with a check digit of 10" do
assert IsbnVerifier.isbn?("3-598-21507-X")
end
@tag :pending
test "check digit is a character other than X" do
refute IsbnVerifier.isbn?("3-598-21507-A")
end
@tag :pending
test "invalid character in isbn" do
refute IsbnVerifier.isbn?("3-598-2K507-0")
end
@tag :pending
test "X is only valid as a check digit" do
refute IsbnVerifier.isbn?("3-598-2X507-0")
end
@tag :pending
test "valid isbn without separating dashes" do
assert IsbnVerifier.isbn?("3598215088")
end
@tag :pending
test "isbn without separating dashes and X as check digit" do
assert IsbnVerifier.isbn?("359821507X")
end
@tag :pending
test "isbn without check digit and dashes" do
refute IsbnVerifier.isbn?("359821507")
end
@tag :pending
test "too long isbn and no dashes" do
refute IsbnVerifier.isbn?("3598215078X")
end
@tag :pending
test "isbn without check digit" do
refute IsbnVerifier.isbn?("3-598-21507")
end
@tag :pending
test "too long isbn" do
refute IsbnVerifier.isbn?("3-598-21507-XA")
end
@tag :pending
test "check digit of X should not be used for 0" do
refute IsbnVerifier.isbn?("3-598-21515-X")
end
# Test cases from international ISBN to test variable dash placement
# Adapted from https://en.wikipedia.org/wiki/International_Standard_Book_Number#Registrant_element
@tag :pending
test "Qatar NCCAH, Doha" do
assert IsbnVerifier.isbn?("99921-58-10-7")
end
@tag :pending
test "Singapore World Scientific" do
assert IsbnVerifier.isbn?("9971-5-0210-0")
end
@tag :pending
test "Greece Sigma Publications" do
assert IsbnVerifier.isbn?("960-425-059-0")
end
@tag :pending
test "Czech Republic; Slovakia Taita Publishers" do
assert IsbnVerifier.isbn?("80-902734-1-6")
end
@tag :pending
test "Brazil Companhia das Letras" do
assert IsbnVerifier.isbn?("85-359-0277-5")
end
@tag :pending
test "English-speaking area Simon Wallenberg Press" do
assert IsbnVerifier.isbn?("1-84356-028-3")
end
@tag :pending
test "English-speaking area Scribner" do
assert IsbnVerifier.isbn?("0-684-84328-5")
end
@tag :pending
test "English-speaking area Frederick Ungar" do
assert IsbnVerifier.isbn?("0-8044-2957-X")
end
@tag :pending
test "English-speaking area J. A. Allen & Co." do
assert IsbnVerifier.isbn?("0-85131-041-9")
end
@tag :pending
test "English-speaking area Edupedia Publications Pvt Ltd." do
assert IsbnVerifier.isbn?("93-86954-21-4")
end
@tag :pending
test "English-speaking area Willmann–Bell" do
assert IsbnVerifier.isbn?("0-943396-04-2")
end
@tag :pending
test "English-speaking area KT Publishing" do
assert IsbnVerifier.isbn?("0-9752298-0-X")
end
end
ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)
defmodule IsbnVerifier do
@spec isbn?(String.t()) :: boolean
def isbn?(isbn) do
digits = String.replace(isbn, "-", "")
if String.length(digits) === 10, do: check_digits(digits, 0), else: false
end
defp check_digits("", sum), do: rem(sum, 11) === 0
defp check_digits(<<"X", "">>, sum), do: check_digits("", 10 * 1 + sum)
defp check_digits(<<digit::binary-size(1), rest::binary()>> = digits, sum) do
case Integer.parse(digit) do
{digit, ""} -> check_digits(rest, digit * String.length(digits) + sum)
:error -> false
end
end
end
A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.
Level up your programming skills with 3,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments
Very nice solution. I learn a lot of your solution. Thx for sharing.
Thank you, Gerrit. It makes me happy to hear that :)