From b1de4ca60807261ad6124d1608e844787fb0a5d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20B=20Nagy?= <20251272+BNAndras@users.noreply.github.com> Date: Mon, 1 Jul 2024 21:49:09 -0700 Subject: [PATCH 1/2] Add resistor-color --- config.json | 8 ++++ .../resistor-color/.docs/instructions.md | 39 +++++++++++++++++++ .../practice/resistor-color/.meta/config.json | 19 +++++++++ .../.meta/reference/resistor_color.bal | 26 +++++++++++++ .../practice/resistor-color/.meta/tests.toml | 22 +++++++++++ .../practice/resistor-color/Ballerina.toml | 5 +++ .../practice/resistor-color/Dependencies.toml | 38 ++++++++++++++++++ .../resistor-color/resistor_color.bal | 15 +++++++ .../tests/resistor_color_test.bal | 39 +++++++++++++++++++ 9 files changed, 211 insertions(+) create mode 100644 exercises/practice/resistor-color/.docs/instructions.md create mode 100644 exercises/practice/resistor-color/.meta/config.json create mode 100644 exercises/practice/resistor-color/.meta/reference/resistor_color.bal create mode 100644 exercises/practice/resistor-color/.meta/tests.toml create mode 100644 exercises/practice/resistor-color/Ballerina.toml create mode 100644 exercises/practice/resistor-color/Dependencies.toml create mode 100644 exercises/practice/resistor-color/resistor_color.bal create mode 100644 exercises/practice/resistor-color/tests/resistor_color_test.bal diff --git a/config.json b/config.json index d1bc056..78a610b 100644 --- a/config.json +++ b/config.json @@ -594,6 +594,14 @@ "prerequisites": [], "difficulty": 1 }, + { + "slug": "resistor-color", + "name": "Resistor Color", + "uuid": "b72f816f-8d3a-48c1-b3e7-ad313354a8ca", + "practices": [], + "prerequisites": [], + "difficulty": 2 + }, { "slug": "eliuds-eggs", "name": "Eliud's Eggs", diff --git a/exercises/practice/resistor-color/.docs/instructions.md b/exercises/practice/resistor-color/.docs/instructions.md new file mode 100644 index 0000000..646c143 --- /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 0000000..34be536 --- /dev/null +++ b/exercises/practice/resistor-color/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "BNAndras" + ], + "files": { + "solution": [ + "resistor_color.bal" + ], + "test": [ + "tests/resistor_color_test.bal" + ], + "example": [ + ".meta/reference/resistor_color.bal" + ] + }, + "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/reference/resistor_color.bal b/exercises/practice/resistor-color/.meta/reference/resistor_color.bal new file mode 100644 index 0000000..89a8afe --- /dev/null +++ b/exercises/practice/resistor-color/.meta/reference/resistor_color.bal @@ -0,0 +1,26 @@ +final string[] colorList = [ + "black", + "brown", + "red", + "orange", + "yellow", + "green", + "blue", + "violet", + "grey", + "white" +]; + +# Calculates the resistor value for the passed band color +# +# + color - The color of the resistor band +# + return - The value of the resistor band +function colorCode(string color) returns int { + return colorList.indexOf(color) ?: 0; +} + +# Returns the list of colors in the resistor color code +# + return - The list of colors +function colors() returns string[] { + return colorList; +} \ No newline at end of file diff --git a/exercises/practice/resistor-color/.meta/tests.toml b/exercises/practice/resistor-color/.meta/tests.toml new file mode 100644 index 0000000..9d4ee97 --- /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/Ballerina.toml b/exercises/practice/resistor-color/Ballerina.toml new file mode 100644 index 0000000..551bf1e --- /dev/null +++ b/exercises/practice/resistor-color/Ballerina.toml @@ -0,0 +1,5 @@ +[package] +org = "ballerina_exercism" +name = "resistor_color" +version = "0.1.0" +distribution = "2201.5.0" diff --git a/exercises/practice/resistor-color/Dependencies.toml b/exercises/practice/resistor-color/Dependencies.toml new file mode 100644 index 0000000..0299f22 --- /dev/null +++ b/exercises/practice/resistor-color/Dependencies.toml @@ -0,0 +1,38 @@ +# AUTO-GENERATED FILE. DO NOT MODIFY. + +# This file is auto-generated by Ballerina for managing dependency versions. +# It should not be modified by hand. + +[ballerina] +dependencies-toml-version = "2" +distribution-version = "2201.5.0" + +[[package]] +org = "ballerina" +name = "jballerina.java" +version = "0.0.0" +scope = "testOnly" + +[[package]] +org = "ballerina" +name = "test" +version = "0.0.0" +scope = "testOnly" +dependencies = [ + {org = "ballerina", name = "jballerina.java"} +] +modules = [ + {org = "ballerina", packageName = "test", moduleName = "test"} +] + +[[package]] +org = "ballerina_exercism" +name = "resistor_color" +version = "0.1.0" +dependencies = [ + {org = "ballerina", name = "test"} +] +modules = [ + {org = "ballerina_exercism", packageName = "resistor_color", moduleName = "resistor_color"} +] + diff --git a/exercises/practice/resistor-color/resistor_color.bal b/exercises/practice/resistor-color/resistor_color.bal new file mode 100644 index 0000000..c5e16a4 --- /dev/null +++ b/exercises/practice/resistor-color/resistor_color.bal @@ -0,0 +1,15 @@ +string[] colorList = ["black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white"]; + +# Calculates the resistor value for the passed band color +# +# + color - The color of the resistor band +# + return - The value of the resistor band +function colorCode(string color) returns int { + // TODO: implement this function +} + +# Returns the list of colors in the resistor color code +# + return - The list of colors +function colors() returns string[] { + // TODO: implement this function +} \ No newline at end of file diff --git a/exercises/practice/resistor-color/tests/resistor_color_test.bal b/exercises/practice/resistor-color/tests/resistor_color_test.bal new file mode 100644 index 0000000..8d1d33b --- /dev/null +++ b/exercises/practice/resistor-color/tests/resistor_color_test.bal @@ -0,0 +1,39 @@ +import ballerina/test; + +@test:Config {} +function testBlack() { + test:assertEquals(0, colorCode("black")); +} + +@test:Config { + enable: false +} +function testWhite() { + test:assertEquals(9, colorCode("white")); +} + +@test:Config { + enable: false +} +function testOrange() { + test:assertEquals(3, colorCode("orange")); +} + +@test:Config { + enable: false +} +function testColors() { + string[] expected = [ + "black", + "brown", + "red", + "orange", + "yellow", + "green", + "blue", + "violet", + "grey", + "white" + ]; + test:assertEquals(expected, colors()); +} From b16851c33f122413c797b6af80f90119612e116f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20B=20Nagy?= <20251272+BNAndras@users.noreply.github.com> Date: Mon, 1 Jul 2024 22:38:25 -0700 Subject: [PATCH 2/2] Remove colors from stub --- exercises/practice/resistor-color/resistor_color.bal | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/exercises/practice/resistor-color/resistor_color.bal b/exercises/practice/resistor-color/resistor_color.bal index c5e16a4..c6efa5b 100644 --- a/exercises/practice/resistor-color/resistor_color.bal +++ b/exercises/practice/resistor-color/resistor_color.bal @@ -1,5 +1,3 @@ -string[] colorList = ["black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white"]; - # Calculates the resistor value for the passed band color # # + color - The color of the resistor band @@ -12,4 +10,4 @@ function colorCode(string color) returns int { # + return - The list of colors function colors() returns string[] { // TODO: implement this function -} \ No newline at end of file +}