forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Change problem definition As I wrote in exercism/julia#503 Thinking about it some more, I think that's because `isvalid` isn't really the right function for us. `isvalid` checks if a string or character contains only valid UTF-8 codepoints. I think it only makes sense for types where the representation of the type (the bits in memory) can encode something invalid in normal use. This is the case for strings because Julia does not validate that strings or characters are correctly encoded when they are created, it just treats them as opaque bytes. For most Julia types, however, you cannot create an invalid value of the type, except through silly use of pointers. I think in real code the advice would be to parse the string into an ISBN type, and if that is not possible (e.g. because the string does not encode a valid ISBN), the ISBN constructor (or `parse(ISBN, s)`, whichever makes more sense) should throw an error. If a non-throwing option is required, `tryparse(ISBN, s)` is the right function to define. Personally, I would define only a constructor and have it throw errors. * Improve docstring * Use the vararg form of print It's slightly faster and maybe better practice. * Remove unused isbn-verifier/.meta/description.md * isbn: update tests and instructions Simplifies the public API of `ISBN` to something that's arguably more realistic and includes enough detail in the description to solve the exercise without reading the tests. * Define an explicit `==` method in isbn-verifier example * write a design.md * better test_nothrows * Clarify where to put the bonus tests Co-authored-by: Sascha Mann <[email protected]> Co-authored-by: Sascha Mann <[email protected]>
- Loading branch information
1 parent
217c0f0
commit 60cade2
Showing
5 changed files
with
85 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Design | ||
|
||
This exercise differs from the upstream problem-specifications one because we're trying to teach "[parse don't validate](https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/)". | ||
|
||
Implementations of tests for the bonus tasks are deliberately excluded and instead we encourage the student to write their own. If you are thinking of changing this, maybe review the newer solutions and see if students did actually start defining their own tests (we started suggesting writing your own tests in May 2022). | ||
|
||
I (@cmcaine) wasn't sure if the better interface was `ISBN(s)` or `parse(ISBN, s)`. I went with the former to keep things simple, but there's a clear argument in the standard library for defining both (e.g. `Dates.Date` and `Base.UUID`). | ||
|
||
It might have been better to require `ISBN(s)` to throw `ArgumentError`, as e.g. `Dates(2022, 13, 1)` or `parse(Int, 'a')` do, but earlier versions of this exercise use `DomainError`, so whatever. Mostly this is Julia's fault for having two exception types with similar uses. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,44 @@ | ||
macro isbn_str(s) | ||
ISBN(s) | ||
end | ||
|
||
struct ISBN <: AbstractString | ||
s::AbstractString | ||
|
||
ISBN(s) = verify(s) ? new(replace(s, "-" => "")) : throw(DomainError(s, "invalid ISBN string")) | ||
import Base: ==, show | ||
|
||
struct ISBN{T<:AbstractString} | ||
s::T | ||
|
||
""" | ||
ISBN(s) | ||
An International Standard Book Number. Throws `DomainError` if `s` does not look like a valid 10-digit ISBN. | ||
""" | ||
function ISBN(s) | ||
acc = 0 | ||
coeff = 10 | ||
for c in s | ||
if c == '-' | ||
continue | ||
elseif isdigit(c) | ||
# c - '0' is a much faster version of parse(Int, c) when we know c is in '0':'9'. | ||
acc += (c - '0') * coeff | ||
coeff -= 1 | ||
# Only the check "digit" is allowed to be 'X' (10) | ||
elseif c == 'X' && coeff == 1 | ||
acc += 10 | ||
coeff -= 1 | ||
else | ||
throw(DomainError(s, "'$c' is not allowable at this location")) | ||
end | ||
end | ||
coeff == 0 || throw(DomainError(s, "ISBNs must be 10 digits long")) | ||
acc % 11 == 0 || throw(DomainError(s, "ISBN checksum failed")) | ||
# Canonicalise in the simplest way | ||
s′ = replace(s, '-' => "") | ||
new{typeof(s′)}(s′) | ||
end | ||
end | ||
|
||
Base.string(s::ISBN) = s.s | ||
|
||
function verify(s::AbstractString) | ||
s = replace(s, "-" => "") | ||
chars = split(s, "") | ||
value(x::ISBN) = x.s | ||
|
||
length(chars) == 10 || return false | ||
==(a::ISBN, b::ISBN) = value(a) == value(b) | ||
|
||
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 | ||
macro isbn_str(s) ISBN(s) end | ||
|
||
Base.isvalid(::Type{ISBN}, s::AbstractString) = verify(s) | ||
# Show ISBNs as they might appear in your source code if you used the macro. | ||
show(io::IO, isbn::ISBN) = print(io, "isbn\"", value(isbn), '"') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters