Zig: completed Eliud's Eggs

This commit is contained in:
Andrew Scott 2024-09-14 11:24:19 -04:00
parent bba6ebb9b8
commit a9995892d0
Signed by: a
GPG key ID: 7CD5A5977E4931C1
6 changed files with 184 additions and 0 deletions

View 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"
}

View 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
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 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
View 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

View 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;
}

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