diff --git a/bin/create-exercise.sh b/bin/create-exercise.sh index 2e57da4..2be26bc 100755 --- a/bin/create-exercise.sh +++ b/bin/create-exercise.sh @@ -22,6 +22,8 @@ while [[ ${pascal} =~ (.*)-(.*) ]]; do pascal=${BASH_REMATCH[1]}${BASH_REMATCH[2]^} done +snake="${slug//-/_}" + if [[ -z $author ]]; then echo read -rp "What's your github username? " author @@ -42,3 +44,5 @@ sed -e "s#hello-world#${slug}#g" exercises/practice/hello-world/hello-world.ipkg echo "module ${pascal}" echo } | tee exercises/practice/${slug}/src/${pascal}.idr > exercises/practice/${slug}/example/${pascal}.idr + +touch generators/exercises/${snake}.py diff --git a/config.json b/config.json index 144d446..9709049 100644 --- a/config.json +++ b/config.json @@ -100,6 +100,14 @@ "prerequisites": [], "difficulty": 1 }, + { + "slug": "resistor-color", + "name": "Resistor Color", + "uuid": "78e88218-c23e-4210-8721-466768a819cd", + "practices": [], + "prerequisites": [], + "difficulty": 1 + }, { "slug": "bob", "name": "Bob", diff --git a/exercises/practice/resistor-color/.docs/instructions.append.md b/exercises/practice/resistor-color/.docs/instructions.append.md new file mode 100644 index 0000000..f1907d7 --- /dev/null +++ b/exercises/practice/resistor-color/.docs/instructions.append.md @@ -0,0 +1,3 @@ +# Instructions append + +You can assume the input is valid. diff --git a/exercises/practice/resistor-color/.docs/instructions.md b/exercises/practice/resistor-color/.docs/instructions.md new file mode 100644 index 0000000..0125e71 --- /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..29801d5 --- /dev/null +++ b/exercises/practice/resistor-color/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "keiravillekode" + ], + "files": { + "solution": [ + "src/ResistorColor.idr" + ], + "test": [ + "test/src/Main.idr" + ], + "example": [ + "example/ResistorColor.idr" + ] + }, + "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/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/example/ResistorColor.idr b/exercises/practice/resistor-color/example/ResistorColor.idr new file mode 100644 index 0000000..2c8043e --- /dev/null +++ b/exercises/practice/resistor-color/example/ResistorColor.idr @@ -0,0 +1,23 @@ +module ResistorColor + +import Data.Maybe +import Data.Vect + +export +colors : Vect 10 String +colors = + [ "black" + , "brown" + , "red" + , "orange" + , "yellow" + , "green" + , "blue" + , "violet" + , "grey" + , "white" + ] + +export +colorCode : String -> Fin 10 +colorCode color = fromMaybe 0 $ elemIndex color colors diff --git a/exercises/practice/resistor-color/pack.toml b/exercises/practice/resistor-color/pack.toml new file mode 100644 index 0000000..8a81c62 --- /dev/null +++ b/exercises/practice/resistor-color/pack.toml @@ -0,0 +1,10 @@ +[custom.all.resistor-color] +type = "local" +path = "." +ipkg = "resistor-color.ipkg" +test = "test/test.ipkg" + +[custom.all.resistor-color-test] +type = "local" +path = "test" +ipkg = "test.ipkg" \ No newline at end of file diff --git a/exercises/practice/resistor-color/resistor-color.ipkg b/exercises/practice/resistor-color/resistor-color.ipkg new file mode 100644 index 0000000..eefd410 --- /dev/null +++ b/exercises/practice/resistor-color/resistor-color.ipkg @@ -0,0 +1,3 @@ +package resistor-color +modules = ResistorColor +sourcedir = "src" diff --git a/exercises/practice/resistor-color/src/ResistorColor.idr b/exercises/practice/resistor-color/src/ResistorColor.idr new file mode 100644 index 0000000..8611edd --- /dev/null +++ b/exercises/practice/resistor-color/src/ResistorColor.idr @@ -0,0 +1,11 @@ +module ResistorColor + +import Data.Vect + +export +colorCode : String -> Fin 10 +colorCode color = ?colorCode_rhs + +export +colors : Vect 10 String +colors = ?colors_rhs diff --git a/exercises/practice/resistor-color/test/src/Main.idr b/exercises/practice/resistor-color/test/src/Main.idr new file mode 100644 index 0000000..74f98ee --- /dev/null +++ b/exercises/practice/resistor-color/test/src/Main.idr @@ -0,0 +1,24 @@ +module Main + +import System +import Tester +import Tester.Runner + +import ResistorColor +import Data.Vect + +tests : List Test +tests = + [ test "Black" (assertEq (colorCode "black") $ 0) + , test "White" (assertEq (colorCode "white") $ 9) + , test "Orange" (assertEq (colorCode "orange") $ 3) + , test "Colors" (assertEq colors ["black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white"]) + ] + +export +main : IO () +main = do + success <- runTests tests + if success + then putStrLn "All tests passed" + else exitFailure diff --git a/exercises/practice/resistor-color/test/test.ipkg b/exercises/practice/resistor-color/test/test.ipkg new file mode 100644 index 0000000..62da3fd --- /dev/null +++ b/exercises/practice/resistor-color/test/test.ipkg @@ -0,0 +1,6 @@ +package resistor-color-test +depends = resistor-color + , tester +main = Main +executable = "resistor-color-test" +sourcedir = "src" diff --git a/generators/exercises/resistor_color.py b/generators/exercises/resistor_color.py new file mode 100644 index 0000000..136fe0e --- /dev/null +++ b/generators/exercises/resistor_color.py @@ -0,0 +1,13 @@ + +def header(): + return "import Data.Vect\n" + +def generate_test(case): + property = case["property"] + expected = case["expected"] + if "color" in case["input"]: + color = case["input"]["color"] + return f'assertEq ({property} "{color}") $ {expected}' + + expected = str(expected).replace("'", '"') + return f'assertEq {property} {expected}'