diff --git a/config.json b/config.json index bea37047..70b2bfa2 100644 --- a/config.json +++ b/config.json @@ -709,6 +709,14 @@ "prerequisites": [], "difficulty": 10 }, + { + "slug": "resistor-color", + "name": "Resistor Color", + "uuid": "cc590998-e7a1-4972-bd32-cf13bf64c909", + "practices": [], + "prerequisites": [], + "difficulty": 2 + }, { "slug": "resistor-color-duo", "name": "Resistor Color Duo", diff --git a/exercises/practice/resistor-color/.docs/instructions.md b/exercises/practice/resistor-color/.docs/instructions.md new file mode 100644 index 00000000..0125e718 --- /dev/null +++ b/exercises/practice/resistor-color/.docs/instructions.md @@ -0,0 +1,39 @@ +# 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 diff --git a/exercises/practice/resistor-color/.meta/config.json b/exercises/practice/resistor-color/.meta/config.json new file mode 100644 index 00000000..5d120e7c --- /dev/null +++ b/exercises/practice/resistor-color/.meta/config.json @@ -0,0 +1,22 @@ +{ + "authors": [ + "BNAndras" + ], + "files": { + "solution": [ + "src/lib.cairo" + ], + "test": [ + "tests/resistor_color.cairo" + ], + "example": [ + ".meta/example.cairo" + ], + "invalidator": [ + "Scarb.toml" + ] + }, + "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/exercises/practice/resistor-color/.meta/example.cairo b/exercises/practice/resistor-color/.meta/example.cairo new file mode 100644 index 00000000..8b9a0a41 --- /dev/null +++ b/exercises/practice/resistor-color/.meta/example.cairo @@ -0,0 +1,43 @@ +#[derive(Drop, Debug, PartialEq)] +pub enum Color { + Black, + Brown, + Red, + Orange, + Yellow, + Green, + Blue, + Violet, + Grey, + White, +} + +pub fn color_code(color: Color) -> u8 { + match color { + Color::Black => 0, + Color::Brown => 1, + Color::Red => 2, + Color::Orange => 3, + Color::Yellow => 4, + Color::Green => 5, + Color::Blue => 6, + Color::Violet => 7, + Color::Grey => 8, + Color::White => 9, + } +} + +pub fn colors() -> Array { + array![ + Color::Black, + Color::Brown, + Color::Red, + Color::Orange, + Color::Yellow, + Color::Green, + Color::Blue, + Color::Violet, + Color::Grey, + Color::White + ] +} diff --git a/exercises/practice/resistor-color/.meta/tests.toml b/exercises/practice/resistor-color/.meta/tests.toml new file mode 100644 index 00000000..9d4ee973 --- /dev/null +++ b/exercises/practice/resistor-color/.meta/tests.toml @@ -0,0 +1,22 @@ +# This is an auto-generated file. +# +# Regenerating this file via `configlet sync` will: +# - Recreate every `description` key/value pair +# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications +# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) +# - Preserve any other key/value pair +# +# As user-added comments (using the # character) will be removed when this file +# is regenerated, comments can be added via a `comment` key. + +[49eb31c5-10a8-4180-9f7f-fea632ab87ef] +description = "Color codes -> Black" + +[0a4df94b-92da-4579-a907-65040ce0b3fc] +description = "Color codes -> White" + +[5f81608d-f36f-4190-8084-f45116b6f380] +description = "Color codes -> Orange" + +[581d68fa-f968-4be2-9f9d-880f2fb73cf7] +description = "Colors" diff --git a/exercises/practice/resistor-color/Scarb.toml b/exercises/practice/resistor-color/Scarb.toml new file mode 100644 index 00000000..4cdd0d65 --- /dev/null +++ b/exercises/practice/resistor-color/Scarb.toml @@ -0,0 +1,7 @@ +[package] +name = "resistor_color" +version = "0.1.0" +edition = "2024_07" + +[dev-dependencies] +cairo_test = "2.8.2" diff --git a/exercises/practice/resistor-color/src/lib.cairo b/exercises/practice/resistor-color/src/lib.cairo new file mode 100644 index 00000000..d486d6e8 --- /dev/null +++ b/exercises/practice/resistor-color/src/lib.cairo @@ -0,0 +1,21 @@ +#[derive(Debug, Drop)] +pub enum Color { + Black, + Brown, + Red, + Orange, + Yellow, + Green, + Blue, + Violet, + Grey, + White, +} + +pub fn color_code(color: Color) -> u8 { + panic!("implement `colors`") +} + +pub fn colors() -> Array { + panic!("implement `colors`") +} diff --git a/exercises/practice/resistor-color/tests/resistor_color.cairo b/exercises/practice/resistor-color/tests/resistor_color.cairo new file mode 100644 index 00000000..49bde126 --- /dev/null +++ b/exercises/practice/resistor-color/tests/resistor_color.cairo @@ -0,0 +1,44 @@ +use resistor_color::{color_code, colors, Color}; + + +#[test] +fn black() { + let output = color_code(Color::Black); + let expected = 0; + assert_eq!(output, expected); +} + +#[test] +#[ignore] +fn white() { + let output = color_code(Color::White); + let expected = 9; + assert_eq!(output, expected); +} + +#[test] +#[ignore] +fn orange() { + let output = color_code(Color::Orange); + let expected = 3; + assert_eq!(output, expected); +} + +#[test] +#[ignore] +fn test_colors() { + let output = colors(); + let expected = array![ + Color::Black, + Color::Brown, + Color::Red, + Color::Orange, + Color::Yellow, + Color::Green, + Color::Blue, + Color::Violet, + Color::Grey, + Color::White + ]; + assert_eq!(output, expected); +}