mirror of
https://codeberg.org/andyscott/exercism.git
synced 2024-12-21 22:33:11 -05:00
Compare commits
2 commits
bba6ebb9b8
...
40ffc9faa3
Author | SHA1 | Date | |
---|---|---|---|
40ffc9faa3 | |||
a9995892d0 |
12 changed files with 391 additions and 0 deletions
19
zig/eliuds-eggs/.exercism/config.json
Normal file
19
zig/eliuds-eggs/.exercism/config.json
Normal file
|
@ -0,0 +1,19 @@
|
|||
{
|
||||
"authors": [
|
||||
"keiravillekode"
|
||||
],
|
||||
"files": {
|
||||
"solution": [
|
||||
"eliuds_eggs.zig"
|
||||
],
|
||||
"test": [
|
||||
"test_eliuds_eggs.zig"
|
||||
],
|
||||
"example": [
|
||||
".meta/example.zig"
|
||||
]
|
||||
},
|
||||
"blurb": "Help Eliud count the number of eggs in her chicken coop by counting the number of 1 bits in a binary representation.",
|
||||
"source": "Christian Willner, Eric Willigers",
|
||||
"source_url": "https://forum.exercism.org/t/new-exercise-suggestion-pop-count/7632/5"
|
||||
}
|
1
zig/eliuds-eggs/.exercism/metadata.json
Normal file
1
zig/eliuds-eggs/.exercism/metadata.json
Normal file
|
@ -0,0 +1 @@
|
|||
{"track":"zig","exercise":"eliuds-eggs","id":"d4996cf553124d6a820a484392d74691","url":"https://exercism.org/tracks/zig/exercises/eliuds-eggs","handle":"Chomp1295","is_requester":true,"auto_approve":false}
|
53
zig/eliuds-eggs/HELP.md
Normal file
53
zig/eliuds-eggs/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 eliuds_eggs.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
|
71
zig/eliuds-eggs/README.md
Normal file
71
zig/eliuds-eggs/README.md
Normal file
|
@ -0,0 +1,71 @@
|
|||
# Eliuds Eggs
|
||||
|
||||
Welcome to Eliuds Eggs on Exercism's Zig Track.
|
||||
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||
|
||||
## Introduction
|
||||
|
||||
Your friend Eliud inherited a farm from her grandma Tigist.
|
||||
Her granny was an inventor and had a tendency to build things in an overly complicated manner.
|
||||
The chicken coop has a digital display showing an encoded number representing the positions of all eggs that could be picked up.
|
||||
|
||||
Eliud is asking you to write a program that shows the actual number of eggs in the coop.
|
||||
|
||||
The position information encoding is calculated as follows:
|
||||
|
||||
1. Scan the potential egg-laying spots and mark down a `1` for an existing egg or a `0` for an empty spot.
|
||||
2. Convert the number from binary to decimal.
|
||||
3. Show the result on the display.
|
||||
|
||||
Example 1:
|
||||
|
||||
```text
|
||||
Chicken Coop:
|
||||
_ _ _ _ _ _ _
|
||||
|E| |E|E| | |E|
|
||||
|
||||
Resulting Binary:
|
||||
1 0 1 1 0 0 1
|
||||
|
||||
Decimal number on the display:
|
||||
89
|
||||
|
||||
Actual eggs in the coop:
|
||||
4
|
||||
```
|
||||
|
||||
Example 2:
|
||||
|
||||
```text
|
||||
Chicken Coop:
|
||||
_ _ _ _ _ _ _ _
|
||||
| | | |E| | | | |
|
||||
|
||||
Resulting Binary:
|
||||
0 0 0 1 0 0 0 0
|
||||
|
||||
Decimal number on the display:
|
||||
16
|
||||
|
||||
Actual eggs in the coop:
|
||||
1
|
||||
```
|
||||
|
||||
## Instructions
|
||||
|
||||
Your task is to count the number of 1 bits in the binary representation of a number.
|
||||
|
||||
## Restrictions
|
||||
|
||||
Keep your hands off that bit-count functionality provided by your standard library!
|
||||
Solve this one yourself using other basic tools instead.
|
||||
|
||||
## Source
|
||||
|
||||
### Created by
|
||||
|
||||
- @keiravillekode
|
||||
|
||||
### Based on
|
||||
|
||||
Christian Willner, Eric Willigers - https://forum.exercism.org/t/new-exercise-suggestion-pop-count/7632/5
|
12
zig/eliuds-eggs/eliuds_eggs.zig
Normal file
12
zig/eliuds-eggs/eliuds_eggs.zig
Normal file
|
@ -0,0 +1,12 @@
|
|||
const std = @import("std");
|
||||
|
||||
pub fn eggCount(number: usize) usize {
|
||||
var count: usize = 0;
|
||||
var temp: usize = number;
|
||||
|
||||
while (temp > 0) : (temp /= 2) {
|
||||
if (temp % 2 != 0) count += 1;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
28
zig/eliuds-eggs/test_eliuds_eggs.zig
Normal file
28
zig/eliuds-eggs/test_eliuds_eggs.zig
Normal file
|
@ -0,0 +1,28 @@
|
|||
const std = @import("std");
|
||||
const testing = std.testing;
|
||||
|
||||
const eliuds_eggs = @import("eliuds_eggs.zig");
|
||||
|
||||
test "0 eggs" {
|
||||
const expected: usize = 0;
|
||||
const actual = eliuds_eggs.eggCount(0);
|
||||
try testing.expectEqual(expected, actual);
|
||||
}
|
||||
|
||||
test "1 egg" {
|
||||
const expected: usize = 1;
|
||||
const actual = eliuds_eggs.eggCount(16);
|
||||
try testing.expectEqual(expected, actual);
|
||||
}
|
||||
|
||||
test "4 eggs" {
|
||||
const expected: usize = 4;
|
||||
const actual = eliuds_eggs.eggCount(89);
|
||||
try testing.expectEqual(expected, actual);
|
||||
}
|
||||
|
||||
test "13 eggs" {
|
||||
const expected: usize = 13;
|
||||
const actual = eliuds_eggs.eggCount(2000000000);
|
||||
try testing.expectEqual(expected, actual);
|
||||
}
|
19
zig/rotational-cipher/.exercism/config.json
Normal file
19
zig/rotational-cipher/.exercism/config.json
Normal file
|
@ -0,0 +1,19 @@
|
|||
{
|
||||
"authors": [
|
||||
"keiravillekode"
|
||||
],
|
||||
"files": {
|
||||
"solution": [
|
||||
"rotational_cipher.zig"
|
||||
],
|
||||
"test": [
|
||||
"test_rotational_cipher.zig"
|
||||
],
|
||||
"example": [
|
||||
".meta/example.zig"
|
||||
]
|
||||
},
|
||||
"blurb": "Create an implementation of the rotational cipher, also sometimes called the Caesar cipher.",
|
||||
"source": "Wikipedia",
|
||||
"source_url": "https://en.wikipedia.org/wiki/Caesar_cipher"
|
||||
}
|
1
zig/rotational-cipher/.exercism/metadata.json
Normal file
1
zig/rotational-cipher/.exercism/metadata.json
Normal file
|
@ -0,0 +1 @@
|
|||
{"track":"zig","exercise":"rotational-cipher","id":"febb4070e9eb44dabea37232f64c12c6","url":"https://exercism.org/tracks/zig/exercises/rotational-cipher","handle":"Chomp1295","is_requester":true,"auto_approve":false}
|
53
zig/rotational-cipher/HELP.md
Normal file
53
zig/rotational-cipher/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 rotational_cipher.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
|
44
zig/rotational-cipher/README.md
Normal file
44
zig/rotational-cipher/README.md
Normal file
|
@ -0,0 +1,44 @@
|
|||
# Rotational Cipher
|
||||
|
||||
Welcome to Rotational Cipher on Exercism's Zig Track.
|
||||
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||
|
||||
## Instructions
|
||||
|
||||
Create an implementation of the rotational cipher, also sometimes called the Caesar cipher.
|
||||
|
||||
The Caesar cipher is a simple shift cipher that relies on transposing all the letters in the alphabet using an integer key between `0` and `26`.
|
||||
Using a key of `0` or `26` will always yield the same output due to modular arithmetic.
|
||||
The letter is shifted for as many values as the value of the key.
|
||||
|
||||
The general notation for rotational ciphers is `ROT + <key>`.
|
||||
The most commonly used rotational cipher is `ROT13`.
|
||||
|
||||
A `ROT13` on the Latin alphabet would be as follows:
|
||||
|
||||
```text
|
||||
Plain: abcdefghijklmnopqrstuvwxyz
|
||||
Cipher: nopqrstuvwxyzabcdefghijklm
|
||||
```
|
||||
|
||||
It is stronger than the Atbash cipher because it has 27 possible keys, and 25 usable keys.
|
||||
|
||||
Ciphertext is written out in the same formatting as the input including spaces and punctuation.
|
||||
|
||||
## Examples
|
||||
|
||||
- ROT5 `omg` gives `trl`
|
||||
- ROT0 `c` gives `c`
|
||||
- ROT26 `Cool` gives `Cool`
|
||||
- ROT13 `The quick brown fox jumps over the lazy dog.` gives `Gur dhvpx oebja sbk whzcf bire gur ynml qbt.`
|
||||
- ROT13 `Gur dhvpx oebja sbk whzcf bire gur ynml qbt.` gives `The quick brown fox jumps over the lazy dog.`
|
||||
|
||||
## Source
|
||||
|
||||
### Created by
|
||||
|
||||
- @keiravillekode
|
||||
|
||||
### Based on
|
||||
|
||||
Wikipedia - https://en.wikipedia.org/wiki/Caesar_cipher
|
16
zig/rotational-cipher/rotational_cipher.zig
Normal file
16
zig/rotational-cipher/rotational_cipher.zig
Normal file
|
@ -0,0 +1,16 @@
|
|||
const std = @import("std");
|
||||
const mem = std.mem;
|
||||
|
||||
pub fn rotate(allocator: mem.Allocator, text: []const u8, shiftKey: u5) mem.Allocator.Error![]u8 {
|
||||
const result = try allocator.alloc(u8, text.len);
|
||||
|
||||
for (text, 0..) |c, i| {
|
||||
result[i] = switch(c) {
|
||||
'a'...'z' => (c - 'a' + shiftKey) % 26 + 'a',
|
||||
'A'...'Z' => (c - 'A' + shiftKey) % 26 + 'A',
|
||||
else => c,
|
||||
};
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
74
zig/rotational-cipher/test_rotational_cipher.zig
Normal file
74
zig/rotational-cipher/test_rotational_cipher.zig
Normal file
|
@ -0,0 +1,74 @@
|
|||
const std = @import("std");
|
||||
const testing = std.testing;
|
||||
|
||||
const rotational_cipher = @import("rotational_cipher.zig");
|
||||
|
||||
test "rotate a by 0, same output as input" {
|
||||
const expected: []const u8 = "a";
|
||||
const actual = try rotational_cipher.rotate(testing.allocator, "a", 0);
|
||||
defer testing.allocator.free(actual);
|
||||
try testing.expectEqualStrings(expected, actual);
|
||||
}
|
||||
|
||||
test "rotate a by 1" {
|
||||
const expected: []const u8 = "b";
|
||||
const actual = try rotational_cipher.rotate(testing.allocator, "a", 1);
|
||||
defer testing.allocator.free(actual);
|
||||
try testing.expectEqualStrings(expected, actual);
|
||||
}
|
||||
|
||||
test "rotate a by 26, same output as input" {
|
||||
const expected: []const u8 = "a";
|
||||
const actual = try rotational_cipher.rotate(testing.allocator, "a", 26);
|
||||
defer testing.allocator.free(actual);
|
||||
try testing.expectEqualStrings(expected, actual);
|
||||
}
|
||||
|
||||
test "rotate m by 13" {
|
||||
const expected: []const u8 = "z";
|
||||
const actual = try rotational_cipher.rotate(testing.allocator, "m", 13);
|
||||
defer testing.allocator.free(actual);
|
||||
try testing.expectEqualStrings(expected, actual);
|
||||
}
|
||||
|
||||
test "rotate n by 13 with wrap around alphabet" {
|
||||
const expected: []const u8 = "a";
|
||||
const actual = try rotational_cipher.rotate(testing.allocator, "n", 13);
|
||||
defer testing.allocator.free(actual);
|
||||
try testing.expectEqualStrings(expected, actual);
|
||||
}
|
||||
|
||||
test "rotate capital letters" {
|
||||
const expected: []const u8 = "TRL";
|
||||
const actual = try rotational_cipher.rotate(testing.allocator, "OMG", 5);
|
||||
defer testing.allocator.free(actual);
|
||||
try testing.expectEqualStrings(expected, actual);
|
||||
}
|
||||
|
||||
test "rotate spaces" {
|
||||
const expected: []const u8 = "T R L";
|
||||
const actual = try rotational_cipher.rotate(testing.allocator, "O M G", 5);
|
||||
defer testing.allocator.free(actual);
|
||||
try testing.expectEqualStrings(expected, actual);
|
||||
}
|
||||
|
||||
test "rotate numbers" {
|
||||
const expected: []const u8 = "Xiwxmrk 1 2 3 xiwxmrk";
|
||||
const actual = try rotational_cipher.rotate(testing.allocator, "Testing 1 2 3 testing", 4);
|
||||
defer testing.allocator.free(actual);
|
||||
try testing.expectEqualStrings(expected, actual);
|
||||
}
|
||||
|
||||
test "rotate punctuation" {
|
||||
const expected: []const u8 = "Gzo'n zvo, Bmviyhv!";
|
||||
const actual = try rotational_cipher.rotate(testing.allocator, "Let's eat, Grandma!", 21);
|
||||
defer testing.allocator.free(actual);
|
||||
try testing.expectEqualStrings(expected, actual);
|
||||
}
|
||||
|
||||
test "rotate all letters" {
|
||||
const expected: []const u8 = "Gur dhvpx oebja sbk whzcf bire gur ynml qbt.";
|
||||
const actual = try rotational_cipher.rotate(testing.allocator, "The quick brown fox jumps over the lazy dog.", 13);
|
||||
defer testing.allocator.free(actual);
|
||||
try testing.expectEqualStrings(expected, actual);
|
||||
}
|
Loading…
Reference in a new issue