Zig: completed ISBN Verifier

This commit is contained in:
Andrew Scott 2024-08-06 10:28:18 -04:00
parent d6118ccead
commit 37cb1870c3
Signed by: a
GPG key ID: 7CD5A5977E4931C1
6 changed files with 250 additions and 0 deletions

View file

@ -0,0 +1,19 @@
{
"authors": [
"ee7"
],
"files": {
"solution": [
"isbn_verifier.zig"
],
"test": [
"test_isbn_verifier.zig"
],
"example": [
".meta/example.zig"
]
},
"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"
}

View file

@ -0,0 +1 @@
{"track":"zig","exercise":"isbn-verifier","id":"6fb4e96621bf4b30a2306e514899179c","url":"https://exercism.org/tracks/zig/exercises/isbn-verifier","handle":"Chomp1295","is_requester":true,"auto_approve":false}

53
zig/isbn-verifier/HELP.md Normal file
View file

@ -0,0 +1,53 @@
# Help
## Running the tests
Write your code in `<exercise_name>.zig`.
To run the tests for an exercise, run:
```bash
zig test test_exercise_name.zig
```
in the exercise's root directory (replacing `exercise_name` with the name of the exercise).
## Submitting your solution
You can submit your solution using the `exercism submit isbn_verifier.zig` command.
This command will upload your solution to the Exercism website and print the solution page's URL.
It's possible to submit an incomplete solution which allows you to:
- See how others have completed the exercise
- Request help from a mentor
## Need to get help?
If you'd like help solving the exercise, check the following pages:
- The [Zig track's documentation](https://exercism.org/docs/tracks/zig)
- The [Zig track's programming category on the forum](https://forum.exercism.org/c/programming/zig)
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
- [The Zig Programming Language Documentation][documentation] is a great overview of all of the language features that Zig provides to those who use it.
- [Zig Guide][zig-guide] is an excellent primer that explains the language features that Zig has to offer.
- [Ziglings][ziglings] is highly recommended.
Learn Zig by fixing tiny broken programs.
- [The Zig Programming Language Discord][discord-zig] is the main [Discord][discord].
It provides a great way to get in touch with the Zig community at large, and get some quick, direct help for any Zig related problem.
- [#zig][irc] on irc.freenode.net is the main Zig IRC channel.
- [/r/Zig][reddit] is the main Zig subreddit.
- [Stack Overflow][stack-overflow] can be used to discover code snippets and solutions to problems that may have already asked and maybe solved by others.
[discord]: https://discordapp.com
[discord-zig]: https://discord.com/invite/gxsFFjE
[documentation]: https://ziglang.org/documentation/master
[irc]: https://webchat.freenode.net/?channels=%23zig
[reddit]: https://www.reddit.com/r/Zig
[stack-overflow]: https://stackoverflow.com/questions/tagged/zig
[zig-guide]: https://zig.guide/
[ziglings]: https://codeberg.org/ziglings/exercises

View file

@ -0,0 +1,57 @@
# ISBN Verifier
Welcome to ISBN Verifier on Exercism's Zig Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
The [ISBN-10 verification process][isbn-verification] 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:
```text
(d₁ * 10 + d₂ * 9 + d₃ * 8 + d₄ * 7 + d₅ * 6 + d₆ * 5 + d₇ * 4 + d₈ * 3 + d₉ * 2 + d₁₀ * 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:
```text
(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.
[isbn-verification]: https://en.wikipedia.org/wiki/International_Standard_Book_Number
## Source
### Created by
- @ee7
### Based on
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

View file

@ -0,0 +1,40 @@
const std = @import("std");
pub fn isValidIsbn10(s: []const u8) bool {
if (s.len < 10) return false;
var isbn: [10]u8 = undefined;
var pos: usize = 0;
for (s) |c| {
if (pos == 10) return false;
isbn[pos] = switch (c) {
'-' => continue,
'0'...'9' => blk: {
pos += 1;
break :blk std.fmt.parseInt(u8, &[_]u8{c}, 10) catch unreachable;
},
else => blk: {
if (c == 'X' and pos == 9) {
pos += 1;
break :blk 10;
} else {
return false;
}
},
};
}
var sum: u32 = 0;
var factor: usize = 10;
var i: usize = 0;
while (factor > 0) : ({
factor -= 1;
i += 1;
}) {
sum += @intCast(isbn[i] * factor);
}
return sum % 11 == 0;
}

View file

@ -0,0 +1,80 @@
const std = @import("std");
const testing = std.testing;
const isValidIsbn10 = @import("isbn_verifier.zig").isValidIsbn10;
test "valid ISBN" {
try testing.expect(isValidIsbn10("3-598-21508-8"));
}
test "invalid ISBN check digit" {
try testing.expect(!isValidIsbn10("3-598-21508-9"));
}
test "valid ISBN with a check digit of 10" {
try testing.expect(isValidIsbn10("3-598-21507-X"));
}
test "check digit is a character other than x" {
try testing.expect(!isValidIsbn10("3-598-21507-A"));
}
test "invalid check digit in ISBN is not treated as zero" {
try testing.expect(!isValidIsbn10("4-598-21507-B"));
}
test "invalid character in ISBN is not treated as zero" {
try testing.expect(!isValidIsbn10("3-598-P1581-X"));
}
test "x is only valid as a check digit" {
try testing.expect(!isValidIsbn10("3-598-2X507-9"));
}
test "valid ISBN without separating dashes" {
try testing.expect(isValidIsbn10("3598215088"));
}
test "ISBN without separating dashes and x as check digit" {
try testing.expect(isValidIsbn10("359821507X"));
}
test "ISBN without check digit and dashes" {
try testing.expect(!isValidIsbn10("359821507"));
}
test "too long ISBN and no dashes" {
try testing.expect(!isValidIsbn10("3598215078X"));
}
test "too short ISBN" {
try testing.expect(!isValidIsbn10("00"));
}
test "ISBN without check digit" {
try testing.expect(!isValidIsbn10("3-598-21507"));
}
test "check digit of x should not be used for 0" {
try testing.expect(!isValidIsbn10("3-598-21515-X"));
}
test "empty ISBN" {
try testing.expect(!isValidIsbn10(""));
}
test "input is 9 characters" {
try testing.expect(!isValidIsbn10("134456729"));
}
test "invalid characters are not ignored after checking length" {
try testing.expect(!isValidIsbn10("3132P34035"));
}
test "invalid characters are not ignored before checking length" {
try testing.expect(!isValidIsbn10("3598P215088"));
}
test "input is too long but contains a valid ISBN" {
try testing.expect(!isValidIsbn10("98245726788"));
}