mirror of
https://codeberg.org/andyscott/exercism.git
synced 2024-12-21 14:23:10 -05:00
Zig: completed Resistor Color
This commit is contained in:
parent
cb46967714
commit
24249a9ddb
6 changed files with 181 additions and 0 deletions
19
zig/resistor-color/.exercism/config.json
Normal file
19
zig/resistor-color/.exercism/config.json
Normal file
|
@ -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"
|
||||
}
|
1
zig/resistor-color/.exercism/metadata.json
Normal file
1
zig/resistor-color/.exercism/metadata.json
Normal file
|
@ -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}
|
53
zig/resistor-color/HELP.md
Normal file
53
zig/resistor-color/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 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
|
54
zig/resistor-color/README.md
Normal file
54
zig/resistor-color/README.md
Normal file
|
@ -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
|
22
zig/resistor-color/resistor_color.zig
Normal file
22
zig/resistor-color/resistor_color.zig
Normal file
|
@ -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);
|
||||
}
|
32
zig/resistor-color/test_resistor_color.zig
Normal file
32
zig/resistor-color/test_resistor_color.zig
Normal file
|
@ -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);
|
||||
}
|
Loading…
Reference in a new issue