mirror of
https://codeberg.org/andyscott/exercism.git
synced 2024-12-21 14:23:10 -05:00
Zig: completed Grains
This commit is contained in:
parent
b5023e5a2a
commit
d6118ccead
6 changed files with 188 additions and 0 deletions
22
zig/grains/.exercism/config.json
Normal file
22
zig/grains/.exercism/config.json
Normal 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"
|
||||
}
|
1
zig/grains/.exercism/metadata.json
Normal file
1
zig/grains/.exercism/metadata.json
Normal 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
53
zig/grains/HELP.md
Normal 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
34
zig/grains/README.md
Normal 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
13
zig/grains/grains.zig
Normal 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);
|
||||
}
|
65
zig/grains/test_grains.zig
Normal file
65
zig/grains/test_grains.zig
Normal 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);
|
||||
}
|
Loading…
Reference in a new issue