From 24249a9ddbcfe20d6761d7e22a0ccb1774812abe Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Tue, 13 Aug 2024 16:10:04 -0400 Subject: [PATCH] Zig: completed Resistor Color --- zig/resistor-color/.exercism/config.json | 19 ++++++++ zig/resistor-color/.exercism/metadata.json | 1 + zig/resistor-color/HELP.md | 53 +++++++++++++++++++++ zig/resistor-color/README.md | 54 ++++++++++++++++++++++ zig/resistor-color/resistor_color.zig | 22 +++++++++ zig/resistor-color/test_resistor_color.zig | 32 +++++++++++++ 6 files changed, 181 insertions(+) create mode 100644 zig/resistor-color/.exercism/config.json create mode 100644 zig/resistor-color/.exercism/metadata.json create mode 100644 zig/resistor-color/HELP.md create mode 100644 zig/resistor-color/README.md create mode 100644 zig/resistor-color/resistor_color.zig create mode 100644 zig/resistor-color/test_resistor_color.zig diff --git a/zig/resistor-color/.exercism/config.json b/zig/resistor-color/.exercism/config.json new file mode 100644 index 0000000..532ba52 --- /dev/null +++ b/zig/resistor-color/.exercism/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "massivelivefun" + ], + "files": { + "solution": [ + "resistor_color.zig" + ], + "test": [ + "test_resistor_color.zig" + ], + "example": [ + ".meta/example.zig" + ] + }, + "blurb": "Convert a resistor band's color to its numeric representation.", + "source": "Maud de Vries, Erik Schierboom", + "source_url": "https://github.com/exercism/problem-specifications/issues/1458" +} diff --git a/zig/resistor-color/.exercism/metadata.json b/zig/resistor-color/.exercism/metadata.json new file mode 100644 index 0000000..c6871e1 --- /dev/null +++ b/zig/resistor-color/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"zig","exercise":"resistor-color","id":"a44178efdd0949b4936b7309fe486b87","url":"https://exercism.org/tracks/zig/exercises/resistor-color","handle":"Chomp1295","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/zig/resistor-color/HELP.md b/zig/resistor-color/HELP.md new file mode 100644 index 0000000..352a586 --- /dev/null +++ b/zig/resistor-color/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 resistor_color.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/resistor-color/README.md b/zig/resistor-color/README.md new file mode 100644 index 0000000..594ea0f --- /dev/null +++ b/zig/resistor-color/README.md @@ -0,0 +1,54 @@ +# Resistor Color + +Welcome to Resistor Color on Exercism's Zig Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Instructions + +If you want to build something using a Raspberry Pi, you'll probably use _resistors_. +For this exercise, you need to know two things about them: + +- Each resistor has a resistance value. +- Resistors are small - so small in fact that if you printed the resistance value on them, it would be hard to read. + +To get around this problem, manufacturers print color-coded bands onto the resistors to denote their resistance values. +Each band has a position and a numeric value. + +The first 2 bands of a resistor have a simple encoding scheme: each color maps to a single number. + +In this exercise you are going to create a helpful program so that you don't have to remember the values of the bands. + +These colors are encoded as follows: + +- black: 0 +- brown: 1 +- red: 2 +- orange: 3 +- yellow: 4 +- green: 5 +- blue: 6 +- violet: 7 +- grey: 8 +- white: 9 + +The goal of this exercise is to create a way: + +- to look up the numerical value associated with a particular color band +- to list the different band colors + +Mnemonics map the colors to the numbers, that, when stored as an array, happen to map to their index in the array: +Better Be Right Or Your Great Big Values Go Wrong. + +More information on the color encoding of resistors can be found in the [Electronic color code Wikipedia article][e-color-code]. + +[e-color-code]: https://en.wikipedia.org/wiki/Electronic_color_code + +## Source + +### Created by + +- @massivelivefun + +### Based on + +Maud de Vries, Erik Schierboom - https://github.com/exercism/problem-specifications/issues/1458 \ No newline at end of file diff --git a/zig/resistor-color/resistor_color.zig b/zig/resistor-color/resistor_color.zig new file mode 100644 index 0000000..593f033 --- /dev/null +++ b/zig/resistor-color/resistor_color.zig @@ -0,0 +1,22 @@ +const std = @import("std"); + +pub const ColorBand = enum(usize) { + black, + brown, + red, + orange, + yellow, + green, + blue, + violet, + grey, + white +}; + +pub fn colorCode(color: ColorBand) usize { + return @intFromEnum(color); +} + +pub fn colors() []const ColorBand { + return std.enums.values(ColorBand); +} diff --git a/zig/resistor-color/test_resistor_color.zig b/zig/resistor-color/test_resistor_color.zig new file mode 100644 index 0000000..eb0e083 --- /dev/null +++ b/zig/resistor-color/test_resistor_color.zig @@ -0,0 +1,32 @@ +const std = @import("std"); +const testing = std.testing; + +const resistor_color = @import("resistor_color.zig"); +const ColorBand = resistor_color.ColorBand; + +test "black" { + const expected: usize = 0; + const actual = resistor_color.colorCode(.black); + try testing.expectEqual(expected, actual); +} + +test "white" { + const expected: usize = 9; + const actual = resistor_color.colorCode(.white); + try testing.expectEqual(expected, actual); +} + +test "orange" { + const expected: usize = 3; + const actual = resistor_color.colorCode(.orange); + try testing.expectEqual(expected, actual); +} + +test "colors" { + const expected = &[_]ColorBand{ + .black, .brown, .red, .orange, .yellow, + .green, .blue, .violet, .grey, .white, + }; + const actual = resistor_color.colors(); + try testing.expectEqualSlices(ColorBand, expected, actual); +}