Compare commits

...

2 commits

Author SHA1 Message Date
37cb1870c3
Zig: completed ISBN Verifier 2024-08-06 10:28:18 -04:00
d6118ccead
Zig: completed Grains 2024-08-06 10:27:54 -04:00
12 changed files with 438 additions and 0 deletions

View file

@ -0,0 +1,22 @@
{
"authors": [
"massivelivefun"
],
"contributors": [
"ee7"
],
"files": {
"solution": [
"grains.zig"
],
"test": [
"test_grains.zig"
],
"example": [
".meta/example.zig"
]
},
"blurb": "Calculate the number of grains of wheat on a chessboard given that the number on each square doubles.",
"source": "The CodeRanch Cattle Drive, Assignment 6",
"source_url": "https://coderanch.com/wiki/718824/Grains"
}

View file

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

53
zig/grains/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 grains.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

34
zig/grains/README.md Normal file
View file

@ -0,0 +1,34 @@
# Grains
Welcome to Grains on Exercism's Zig Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
Calculate the number of grains of wheat on a chessboard given that the number on each square doubles.
There once was a wise servant who saved the life of a prince.
The king promised to pay whatever the servant could dream up.
Knowing that the king loved chess, the servant told the king he would like to have grains of wheat.
One grain on the first square of a chess board, with the number of grains doubling on each successive square.
There are 64 squares on a chessboard (where square 1 has one grain, square 2 has two grains, and so on).
Write code that shows:
- how many grains were on a given square, and
- the total number of grains on the chessboard
## Source
### Created by
- @massivelivefun
### Contributed to by
- @ee7
### Based on
The CodeRanch Cattle Drive, Assignment 6 - https://coderanch.com/wiki/718824/Grains

13
zig/grains/grains.zig Normal file
View file

@ -0,0 +1,13 @@
const std = @import("std");
pub const ChessboardError = error{IndexOutOfBounds};
pub fn square(index: usize) ChessboardError!u64 {
if (index > 64 or index < 1) return error.IndexOutOfBounds;
return std.math.pow(u64, 2, index - 1);
}
pub fn total() u64 {
return std.math.maxInt(u64);
}

View file

@ -0,0 +1,65 @@
const std = @import("std");
const testing = std.testing;
const grains = @import("grains.zig");
const ChessboardError = grains.ChessboardError;
test "grains on square 1" {
const expected: u64 = 1;
const actual = try grains.square(1);
try testing.expectEqual(expected, actual);
}
test "grains on square 2" {
const expected: u64 = 2;
const actual = try grains.square(2);
try testing.expectEqual(expected, actual);
}
test "grains on square 3" {
const expected: u64 = 4;
const actual = try grains.square(3);
try testing.expectEqual(expected, actual);
}
test "grains on square 4" {
const expected: u64 = 8;
const actual = try grains.square(4);
try testing.expectEqual(expected, actual);
}
test "grains on square 16" {
const expected: u64 = 32_768;
const actual = try grains.square(16);
try testing.expectEqual(expected, actual);
}
test "grains on square 32" {
const expected: u64 = 2_147_483_648;
const actual = try grains.square(32);
try testing.expectEqual(expected, actual);
}
test "grains on square 64" {
const expected: u64 = 9_223_372_036_854_775_808;
const actual = try grains.square(64);
try testing.expectEqual(expected, actual);
}
test "square 0 produces an error" {
const expected = ChessboardError.IndexOutOfBounds;
const actual = grains.square(0);
try testing.expectError(expected, actual);
}
test "square greater than 64 produces an error" {
const expected = ChessboardError.IndexOutOfBounds;
const actual = grains.square(65);
try testing.expectError(expected, actual);
}
test "returns the total number of grains on the board" {
const expected: u64 = 18_446_744_073_709_551_615;
const actual = grains.total();
try testing.expectEqual(expected, actual);
}

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"));
}