6 changed files with 259 additions and 0 deletions
@ -0,0 +1,8 @@
|
||||
[package] |
||||
edition = "2021" |
||||
name = "resistor-color" |
||||
version = "1.0.0" |
||||
|
||||
[dependencies] |
||||
int-enum = "0.4.0" |
||||
enum-iterator = "0.7.0" |
||||
@ -0,0 +1,85 @@
|
||||
# Help |
||||
|
||||
## Running the tests |
||||
|
||||
Execute the tests with: |
||||
|
||||
```bash |
||||
$ cargo test |
||||
``` |
||||
|
||||
All but the first test have been ignored. After you get the first test to |
||||
pass, open the tests source file which is located in the `tests` directory |
||||
and remove the `#[ignore]` flag from the next test and get the tests to pass |
||||
again. Each separate test is a function with `#[test]` flag above it. |
||||
Continue, until you pass every test. |
||||
|
||||
If you wish to run _only ignored_ tests without editing the tests source file, use: |
||||
|
||||
```bash |
||||
$ cargo test -- --ignored |
||||
``` |
||||
|
||||
If you are using Rust 1.51 or later, you can run _all_ tests with |
||||
|
||||
```bash |
||||
$ cargo test -- --include-ignored |
||||
``` |
||||
|
||||
To run a specific test, for example `some_test`, you can use: |
||||
|
||||
```bash |
||||
$ cargo test some_test |
||||
``` |
||||
|
||||
If the specific test is ignored, use: |
||||
|
||||
```bash |
||||
$ cargo test some_test -- --ignored |
||||
``` |
||||
|
||||
To learn more about Rust tests refer to the online [test documentation][rust-tests]. |
||||
|
||||
[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html |
||||
|
||||
## Submitting your solution |
||||
|
||||
You can submit your solution using the `exercism submit src/lib.rs Cargo.toml` 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 [Rust track's documentation](https://exercism.org/docs/tracks/rust) |
||||
- [Exercism's support channel on gitter](https://gitter.im/exercism/support) |
||||
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs) |
||||
|
||||
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring. |
||||
|
||||
## Rust Installation |
||||
|
||||
Refer to the [exercism help page][help-page] for Rust installation and learning |
||||
resources. |
||||
|
||||
## Submitting the solution |
||||
|
||||
Generally you should submit all files in which you implemented your solution (`src/lib.rs` in most cases). If you are using any external crates, please consider submitting the `Cargo.toml` file. This will make the review process faster and clearer. |
||||
|
||||
## Feedback, Issues, Pull Requests |
||||
|
||||
The GitHub [track repository][github] is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help! |
||||
|
||||
If you want to know more about Exercism, take a look at the [contribution guide]. |
||||
|
||||
## Submitting Incomplete Solutions |
||||
It's possible to submit an incomplete solution so you can see how others have completed the exercise. |
||||
|
||||
[help-page]: https://exercism.org/tracks/rust/learning |
||||
[github]: https://github.com/exercism/rust |
||||
[contribution guide]: https://exercism.org/docs/community/contributors |
||||
@ -0,0 +1,11 @@
|
||||
# Hints |
||||
|
||||
## General |
||||
|
||||
- Use the `enum-iterator` crate to be able to iterate over enums. Check out [docs](https://docs.rs/enum-iterator/0.7.0/enum_iterator/) to find out how. |
||||
|
||||
- Use the `int-enum` crate to be able to convert from int to enum. See the [docs](https://docs.rs/int-enum/0.4.0/int_enum/) for details. |
||||
|
||||
- The `Debug` trait is there because you'll likely need it. |
||||
|
||||
- You'll need to extend the list of `derive`d traits on your colors enum. |
||||
@ -0,0 +1,65 @@
|
||||
# Resistor Color |
||||
|
||||
Welcome to Resistor Color on Exercism's Rust Track. |
||||
If you need help running the tests or submitting your code, check out `HELP.md`. |
||||
If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :) |
||||
|
||||
## Introduction |
||||
|
||||
Most programs have dependencies on some libraries. Managing those by hand can be a pain. Luckily, the Rust ecosystem comes standard with `cargo`! `cargo` can manage dependencies for a project. |
||||
|
||||
You will often see external packages being referred to as "crates" in Rust. A crate is a compilation unit in Rust. A crate can be compiled into a binary or into a library. |
||||
|
||||
Most of the time, adding an external dependency is as simple as adding a line to your `Cargo.toml` file. |
||||
|
||||
In this exercise, [`enum-iterator`](https://docs.rs/enum-iterator/0.7.0/enum_iterator/) and [`int-enum`](https://docs.rs/int-enum/0.4.0/int_enum/) dependencies were added for you. |
||||
|
||||
## 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 convert the numerical value into a string representing color |
||||
- to list the different band colors |
||||
|
||||
For tasks number two and three, you will need external crates [`enum-iterator`](https://docs.rs/enum-iterator/0.7.0/enum_iterator/) and [`int-enum`](https://docs.rs/int-enum/0.4.0/int_enum/), which are included in this exercise's `Cargo.toml`. Be sure to check the crates' documentation to learn how to use them. |
||||
|
||||
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](https://en.wikipedia.org/wiki/Electronic_color_code) |
||||
|
||||
## Source |
||||
|
||||
### Created by |
||||
|
||||
- @still-flow |
||||
- @coriolinus |
||||
|
||||
### Based on |
||||
|
||||
Maud de Vries, Erik Schierboom - https://github.com/exercism/problem-specifications/issues/1458 |
||||
@ -0,0 +1,42 @@
|
||||
use std::fmt; |
||||
use int_enum::IntEnum; |
||||
use enum_iterator::IntoEnumIterator; |
||||
|
||||
|
||||
#[repr(u8)] |
||||
#[derive(Debug, PartialEq, Copy, Clone, IntEnum, IntoEnumIterator, Ord, PartialOrd, Eq)] |
||||
pub enum ResistorColor { |
||||
Black = 0, |
||||
Blue = 6, |
||||
Brown = 1, |
||||
Green = 5, |
||||
Grey = 8, |
||||
Orange = 3, |
||||
Red = 2, |
||||
Violet = 7, |
||||
White = 9, |
||||
Yellow = 4, |
||||
} |
||||
|
||||
impl fmt::Display for ResistorColor { |
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
||||
write!(f, "{}", format!("{:?}", self)) |
||||
} |
||||
} |
||||
|
||||
pub fn color_to_value(_color: ResistorColor) -> usize { |
||||
_color.int_value().into() |
||||
} |
||||
|
||||
pub fn value_to_color_string(value: usize) -> String { |
||||
match ResistorColor::from_int(value as u8) { |
||||
Ok(color) => format!("{}", color), |
||||
Err(_) => "value out of range".to_string(), |
||||
} |
||||
} |
||||
|
||||
pub fn colors() -> Vec<ResistorColor> { |
||||
let mut colors = ResistorColor::into_enum_iter().collect::<Vec<ResistorColor>>(); |
||||
colors.sort(); |
||||
colors |
||||
} |
||||
@ -0,0 +1,48 @@
|
||||
use resistor_color::{color_to_value, colors, value_to_color_string, ResistorColor}; |
||||
|
||||
#[test] |
||||
fn test_black() { |
||||
assert_eq!(color_to_value(ResistorColor::Black), 0); |
||||
} |
||||
|
||||
#[test] |
||||
fn test_orange() { |
||||
assert_eq!(color_to_value(ResistorColor::Orange), 3); |
||||
} |
||||
|
||||
#[test] |
||||
fn test_white() { |
||||
assert_eq!(color_to_value(ResistorColor::White), 9); |
||||
} |
||||
|
||||
#[test] |
||||
fn test_2() { |
||||
assert_eq!(value_to_color_string(2), String::from("Red")); |
||||
} |
||||
|
||||
#[test] |
||||
fn test_6() { |
||||
assert_eq!(value_to_color_string(6), String::from("Blue")); |
||||
} |
||||
|
||||
#[test] |
||||
fn test_8() { |
||||
assert_eq!(value_to_color_string(8), String::from("Grey")); |
||||
} |
||||
|
||||
#[test] |
||||
fn test_11_out_of_range() { |
||||
assert_eq!( |
||||
value_to_color_string(11), |
||||
String::from("value out of range") |
||||
); |
||||
} |
||||
|
||||
#[test] |
||||
fn test_all_colors() { |
||||
use ResistorColor::*; |
||||
assert_eq!( |
||||
colors(), |
||||
vec![Black, Brown, Red, Orange, Yellow, Green, Blue, Violet, Grey, White] |
||||
); |
||||
} |
||||
Loading…
Reference in new issue