Sha256: 7330f01532a886da7a3776ff7072227fe84f3eda475b1881bc038c5264f46fce

Contents?: true

Size: 596 Bytes

Versions: 72

Compression:

Stored size: 596 Bytes

Contents

macro isbn_str(s)
    ISBN(s)
end

struct ISBN <: AbstractString
    s::AbstractString

    ISBN(s) = verify(s) ? new(replace(s, "-", "")) : throw(ArgumentError("invalid ISBN string: $s"))
end

string(s::ISBN) = s.s

function verify(s::AbstractString)
    s = replace(s, "-", "")
    chars = split(s, "")

    length(chars) == 10 || return false

    if last(chars) == "X"
        chars[end] = "10"
    end

    all(c -> all(isdigit, c), chars) || return false

    sum(parse(Int, c) * i for (c, i) in zip(chars, 10:-1:1)) % 11 == 0
end

Base.isvalid(::Type{ISBN}, s::AbstractString) = verify(s)

Version data entries

72 entries across 72 versions & 1 rubygems

Version Path
trackler-2.2.1.139 tracks/julia/exercises/isbn-verifier/example.jl
trackler-2.2.1.138 tracks/julia/exercises/isbn-verifier/example.jl
trackler-2.2.1.137 tracks/julia/exercises/isbn-verifier/example.jl
trackler-2.2.1.136 tracks/julia/exercises/isbn-verifier/example.jl
trackler-2.2.1.135 tracks/julia/exercises/isbn-verifier/example.jl
trackler-2.2.1.134 tracks/julia/exercises/isbn-verifier/example.jl
trackler-2.2.1.133 tracks/julia/exercises/isbn-verifier/example.jl
trackler-2.2.1.132 tracks/julia/exercises/isbn-verifier/example.jl
trackler-2.2.1.131 tracks/julia/exercises/isbn-verifier/example.jl
trackler-2.2.1.130 tracks/julia/exercises/isbn-verifier/example.jl
trackler-2.2.1.129 tracks/julia/exercises/isbn-verifier/example.jl
trackler-2.2.1.128 tracks/julia/exercises/isbn-verifier/example.jl
trackler-2.2.1.127 tracks/julia/exercises/isbn-verifier/example.jl
trackler-2.2.1.126 tracks/julia/exercises/isbn-verifier/example.jl
trackler-2.2.1.125 tracks/julia/exercises/isbn-verifier/example.jl
trackler-2.2.1.124 tracks/julia/exercises/isbn-verifier/example.jl
trackler-2.2.1.123 tracks/julia/exercises/isbn-verifier/example.jl
trackler-2.2.1.122 tracks/julia/exercises/isbn-verifier/example.jl
trackler-2.2.1.121 tracks/julia/exercises/isbn-verifier/example.jl
trackler-2.2.1.120 tracks/julia/exercises/isbn-verifier/example.jl