🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of wconrad

wconrad's solution

to ISBN Verifier in the Julia Track

Published at Oct 27 2020 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

The ISBN-10 verification process is used to validate book identification numbers. These normally contain dashes and look like: 3-598-21508-8

ISBN

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.

Example

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.

Task

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.

Caveats

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.

Bonus tasks

  • Implement show(io::IO, isbn::ISBN) to print the ISBN in the standard format with dashes.

  • 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.

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

Version compatibility

This exercise has been tested on Julia versions >=1.0.

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

runtests.jl

using Test

include("isbn-verifier.jl")

@test ISBN <: AbstractString

@testset "valid ISBN numbers" begin
    # ISBN number
    @test isvalid(ISBN, "3-598-21508-8")
    # ISBN number with a check digit of 10
    @test isvalid(ISBN, "3-598-21507-X")
    # ISBN without separating dashes
    @test isvalid(ISBN, "3598215088")
    # ISBN without separating dashes and X as check digit
    @test isvalid(ISBN, "359821507X")
end

@testset "invalid ISBN numbers" begin
    # invalid ISBN check digit
    @test !isvalid(ISBN, "3-598-21508-9")
    # check digit is a character other than X
    @test !isvalid(ISBN, "3-598-21507-A")
    # invalid character in ISBN
    @test !isvalid(ISBN, "3-598-2K507-0")
    # X is only valid as a check isdigit
    @test !isvalid(ISBN, "3-598-2X507-9")
    # ISBN without check digit and dashes
    @test !isvalid(ISBN, "359821507")
    # too long ISBN and no dashes
    @test !isvalid(ISBN, "3598215078X")
    # ISBN without check digit
    @test !isvalid(ISBN, "3-598-21507")
    # too long ISBN
    @test !isvalid(ISBN, "3-598-21507-XX")
    # check digit of X should not be used for 0
    @test !isvalid(ISBN, "3-598-21515-X")
    # empty ISBN
    @test !isvalid(ISBN, "")
end

@testset "constructing valid ISBN numbers" begin
    # ISBN number
    @test string(isbn"3-598-21508-8") == "3598215088"
    # ISBN number with a check digit of 10
    @test string(isbn"3-598-21507-X") == "359821507X"
    # ISBN without separating dashes
    @test string(isbn"3598215088") == "3598215088"
    # ISBN without separating dashes and X as check digit
    @test string(isbn"359821507X") == "359821507X"
end

@testset "constructing invalid ISBN numbers" begin
    # invalid ISBN check digit
    @test_throws ArgumentError ISBN("3-598-21508-9")
    # check digit is a character other than X
    @test_throws ArgumentError ISBN("3-598-21507-A")
    # invalid character in ISBN
    @test_throws ArgumentError ISBN("3-598-2K507-0")
    # X is only valid as a check isdigit
    @test_throws ArgumentError ISBN("3-598-2X507-9")
    # ISBN without check digit and dashes
    @test_throws ArgumentError ISBN("359821507")
    # too long ISBN and no dashes
    @test_throws ArgumentError ISBN("3598215078X")
    # ISBN without check digit
    @test_throws ArgumentError ISBN("3-598-21507")
    # too long ISBN
    @test_throws ArgumentError ISBN("3-598-21507-XX")
    # check digit of X should not be used for 0
    @test_throws ArgumentError ISBN("3-598-21515-X")
    # empty ISBN
    @test_throws ArgumentError ISBN("")
end
struct ISBN <: AbstractString
    string::AbstractString
    function ISBN(s::AbstractString)
        s = filter_isbn(s)
        check_filtered_isbn(s) || throw(ArgumentError("Invalid ISBN"))
        new(s)
    end
end

function isvalid(_::Type{ISBN}, s::AbstractString)
    filter_isbn(s) |> check_filtered_isbn
end

macro isbn_str(s::AbstractString)
    ISBN(filter_isbn(s))
end

Base.string(isbn::ISBN) = isbn.string

## Private ## 

filter_isbn(s::AbstractString) = filter(c -> isdigit(c) || c == 'X', s)

function check_filtered_isbn(s::AbstractString)
    occursin(r"\A\d{9}[\dX]\Z", s) || return false
    digits = [c == 'X' ? 10 : c - '0' for c in s]
    pairs = zip(digits, 10:-1:1)
    total = [n * coef for (n, coef) in pairs] |> sum
    total % 11 == 0
end

Community comments

Find this solution interesting? Ask the author a question to learn more.

wconrad's Reflection

This was a fun one that taught me a couple of things.

I found myself naming an argument `isbn_str`, then I realized that a type declaration is way better than a _str suffix. I hate strictly typed languages without type inference, because who wants to spend half their time declaring types? But when they help the compiler make good code, or especially help the human understand the code, then type declarations are great.

I suspect that the funky signature taking a parameter of `Type{ISBN}`, where ISBN is a custom type, is demonstrating a way to declare a function which name cannot conflict with any existing function.

At one point I thought the test was demanding that I implement the AbstractString interface for ISBN. It wasn't, but it was a valuable misunderstanding because it caused me to find where in the base library source that interface is defined.

The tests don't actually care whether ISBN's ctor returns an ISBN. I found this out by accidentally returning a String. As long as the ctor returns a string with all the dashes removed, the test is happy.