mirror of
https://codeberg.org/andyscott/exercism.git
synced 2024-12-22 06:43:09 -05:00
Zig: completed Resistor Color Duo
This commit is contained in:
parent
9cfecd833c
commit
65cbcd83d8
6 changed files with 173 additions and 0 deletions
19
zig/resistor-color-duo/.exercism/config.json
Normal file
19
zig/resistor-color-duo/.exercism/config.json
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
{
|
||||||
|
"authors": [
|
||||||
|
"massivelivefun"
|
||||||
|
],
|
||||||
|
"files": {
|
||||||
|
"solution": [
|
||||||
|
"resistor_color_duo.zig"
|
||||||
|
],
|
||||||
|
"test": [
|
||||||
|
"test_resistor_color_duo.zig"
|
||||||
|
],
|
||||||
|
"example": [
|
||||||
|
".meta/example.zig"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"blurb": "Convert color codes, as used on resistors, to a numeric value.",
|
||||||
|
"source": "Maud de Vries, Erik Schierboom",
|
||||||
|
"source_url": "https://github.com/exercism/problem-specifications/issues/1464"
|
||||||
|
}
|
1
zig/resistor-color-duo/.exercism/metadata.json
Normal file
1
zig/resistor-color-duo/.exercism/metadata.json
Normal file
|
@ -0,0 +1 @@
|
||||||
|
{"track":"zig","exercise":"resistor-color-duo","id":"4904409bf2c744acb829962709a345ef","url":"https://exercism.org/tracks/zig/exercises/resistor-color-duo","handle":"Chomp1295","is_requester":true,"auto_approve":false}
|
53
zig/resistor-color-duo/HELP.md
Normal file
53
zig/resistor-color-duo/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_duo.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
|
48
zig/resistor-color-duo/README.md
Normal file
48
zig/resistor-color-duo/README.md
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
# Resistor Color Duo
|
||||||
|
|
||||||
|
Welcome to Resistor Color Duo 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.
|
||||||
|
For example, if they printed a brown band (value 1) followed by a green band (value 5), it would translate to the number 15.
|
||||||
|
|
||||||
|
In this exercise you are going to create a helpful program so that you don't have to remember the values of the bands.
|
||||||
|
The program will take color names as input and output a two digit number, even if the input is more than two colors!
|
||||||
|
|
||||||
|
The band 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
|
||||||
|
|
||||||
|
From the example above:
|
||||||
|
brown-green should return 15, and
|
||||||
|
brown-green-violet should return 15 too, ignoring the third color.
|
||||||
|
|
||||||
|
## Source
|
||||||
|
|
||||||
|
### Created by
|
||||||
|
|
||||||
|
- @massivelivefun
|
||||||
|
|
||||||
|
### Based on
|
||||||
|
|
||||||
|
Maud de Vries, Erik Schierboom - https://github.com/exercism/problem-specifications/issues/1464
|
5
zig/resistor-color-duo/resistor_color_duo.zig
Normal file
5
zig/resistor-color-duo/resistor_color_duo.zig
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
pub const ColorBand = enum(usize) { black = 0, brown = 1, red = 2, orange = 3, yellow = 4, green = 5, blue = 6, violet = 7, grey = 8, white = 9 };
|
||||||
|
|
||||||
|
pub fn colorCode(comptime colors: [2]ColorBand) usize {
|
||||||
|
return 10 * @intFromEnum(colors[0]) + @intFromEnum(colors[1]);
|
||||||
|
}
|
47
zig/resistor-color-duo/test_resistor_color_duo.zig
Normal file
47
zig/resistor-color-duo/test_resistor_color_duo.zig
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
const std = @import("std");
|
||||||
|
const testing = std.testing;
|
||||||
|
|
||||||
|
const resistor_color_duo = @import("resistor_color_duo.zig");
|
||||||
|
const ColorBand = resistor_color_duo.ColorBand;
|
||||||
|
|
||||||
|
test "brown and black" {
|
||||||
|
const array = [_]ColorBand{ .brown, .black };
|
||||||
|
const expected: usize = 10;
|
||||||
|
const actual = resistor_color_duo.colorCode(array);
|
||||||
|
try testing.expectEqual(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "blue and grey" {
|
||||||
|
const array = [_]ColorBand{ .blue, .grey };
|
||||||
|
const expected: usize = 68;
|
||||||
|
const actual = resistor_color_duo.colorCode(array);
|
||||||
|
try testing.expectEqual(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "yellow and violet" {
|
||||||
|
const array = [_]ColorBand{ .yellow, .violet };
|
||||||
|
const expected: usize = 47;
|
||||||
|
const actual = resistor_color_duo.colorCode(array);
|
||||||
|
try testing.expectEqual(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "white and red" {
|
||||||
|
const array = [_]ColorBand{ .white, .red };
|
||||||
|
const expected: usize = 92;
|
||||||
|
const actual = resistor_color_duo.colorCode(array);
|
||||||
|
try testing.expectEqual(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "orange and orange" {
|
||||||
|
const array = [_]ColorBand{ .orange, .orange };
|
||||||
|
const expected: usize = 33;
|
||||||
|
const actual = resistor_color_duo.colorCode(array);
|
||||||
|
try testing.expectEqual(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "black and brown, one-digit" {
|
||||||
|
const array = [_]ColorBand{ .black, .brown };
|
||||||
|
const expected: usize = 1;
|
||||||
|
const actual = resistor_color_duo.colorCode(array);
|
||||||
|
try testing.expectEqual(expected, actual);
|
||||||
|
}
|
Loading…
Reference in a new issue