From a9995892d0fc8a606d7b310d1f490679729ce308 Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Sat, 14 Sep 2024 11:24:19 -0400 Subject: [PATCH] Zig: completed Eliud's Eggs --- zig/eliuds-eggs/.exercism/config.json | 19 +++++++ zig/eliuds-eggs/.exercism/metadata.json | 1 + zig/eliuds-eggs/HELP.md | 53 ++++++++++++++++++ zig/eliuds-eggs/README.md | 71 +++++++++++++++++++++++++ zig/eliuds-eggs/eliuds_eggs.zig | 12 +++++ zig/eliuds-eggs/test_eliuds_eggs.zig | 28 ++++++++++ 6 files changed, 184 insertions(+) create mode 100644 zig/eliuds-eggs/.exercism/config.json create mode 100644 zig/eliuds-eggs/.exercism/metadata.json create mode 100644 zig/eliuds-eggs/HELP.md create mode 100644 zig/eliuds-eggs/README.md create mode 100644 zig/eliuds-eggs/eliuds_eggs.zig create mode 100644 zig/eliuds-eggs/test_eliuds_eggs.zig diff --git a/zig/eliuds-eggs/.exercism/config.json b/zig/eliuds-eggs/.exercism/config.json new file mode 100644 index 0000000..ddefecc --- /dev/null +++ b/zig/eliuds-eggs/.exercism/config.json @@ -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" +} diff --git a/zig/eliuds-eggs/.exercism/metadata.json b/zig/eliuds-eggs/.exercism/metadata.json new file mode 100644 index 0000000..7e422b2 --- /dev/null +++ b/zig/eliuds-eggs/.exercism/metadata.json @@ -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} \ No newline at end of file diff --git a/zig/eliuds-eggs/HELP.md b/zig/eliuds-eggs/HELP.md new file mode 100644 index 0000000..8e0b523 --- /dev/null +++ b/zig/eliuds-eggs/HELP.md @@ -0,0 +1,53 @@ +# Help + +## Running the tests + +Write your code in `.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 \ No newline at end of file diff --git a/zig/eliuds-eggs/README.md b/zig/eliuds-eggs/README.md new file mode 100644 index 0000000..0149df3 --- /dev/null +++ b/zig/eliuds-eggs/README.md @@ -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 \ No newline at end of file diff --git a/zig/eliuds-eggs/eliuds_eggs.zig b/zig/eliuds-eggs/eliuds_eggs.zig new file mode 100644 index 0000000..ea9ffdd --- /dev/null +++ b/zig/eliuds-eggs/eliuds_eggs.zig @@ -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; +} diff --git a/zig/eliuds-eggs/test_eliuds_eggs.zig b/zig/eliuds-eggs/test_eliuds_eggs.zig new file mode 100644 index 0000000..ccb0986 --- /dev/null +++ b/zig/eliuds-eggs/test_eliuds_eggs.zig @@ -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); +}