From f3ec023eba46e802dd9354c65d65aebecf7103d3 Mon Sep 17 00:00:00 2001 From: Erik Schierboom Date: Mon, 21 Oct 2024 08:40:05 +0200 Subject: [PATCH] Add `resistor-color-duo` exercise --- config.json | 8 +++++ exercises/Exercises.sln | 7 ++++ .../.config/dotnet-tools.json | 12 +++++++ .../resistor-color-duo/.docs/instructions.md | 33 +++++++++++++++++ .../resistor-color-duo/.meta/Example.fs | 19 ++++++++++ .../resistor-color-duo/.meta/config.json | 19 ++++++++++ .../resistor-color-duo/.meta/tests.toml | 31 ++++++++++++++++ .../resistor-color-duo/ResistorColorDuo.fs | 4 +++ .../ResistorColorDuo.fsproj | 22 ++++++++++++ .../ResistorColorDuoTests.fs | 35 +++++++++++++++++++ generators/Generators.fs | 3 ++ 11 files changed, 193 insertions(+) create mode 100644 exercises/practice/resistor-color-duo/.config/dotnet-tools.json create mode 100644 exercises/practice/resistor-color-duo/.docs/instructions.md create mode 100644 exercises/practice/resistor-color-duo/.meta/Example.fs create mode 100644 exercises/practice/resistor-color-duo/.meta/config.json create mode 100644 exercises/practice/resistor-color-duo/.meta/tests.toml create mode 100644 exercises/practice/resistor-color-duo/ResistorColorDuo.fs create mode 100644 exercises/practice/resistor-color-duo/ResistorColorDuo.fsproj create mode 100644 exercises/practice/resistor-color-duo/ResistorColorDuoTests.fs diff --git a/config.json b/config.json index 774192c39..6b30b0f86 100644 --- a/config.json +++ b/config.json @@ -2125,6 +2125,14 @@ "practices": [], "prerequisites": [], "difficulty": 1 + }, + { + "slug": "resistor-color-duo", + "name": "Resistor Color Duo", + "uuid": "4ecde4e3-308c-42e9-bb41-0e746d74b526", + "practices": [], + "prerequisites": [], + "difficulty": 1 } ], "foregone": [ diff --git a/exercises/Exercises.sln b/exercises/Exercises.sln index 4e6ebdb9a..f17d45ed8 100644 --- a/exercises/Exercises.sln +++ b/exercises/Exercises.sln @@ -281,6 +281,8 @@ Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Zipper", "practice\zipper\Z EndProject Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "ResistorColor", "practice\resistor-color\ResistorColor.fsproj", "{6D2CFE86-D6B2-4820-B3D8-62255BF94DCD}" EndProject +Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "ResistorColorDuo", "practice\resistor-color-duo\ResistorColorDuo.fsproj", "{D7E215C6-4EE6-41AB-BD80-F126C50A590E}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -838,6 +840,10 @@ Global {6D2CFE86-D6B2-4820-B3D8-62255BF94DCD}.Debug|Any CPU.Build.0 = Debug|Any CPU {6D2CFE86-D6B2-4820-B3D8-62255BF94DCD}.Release|Any CPU.ActiveCfg = Release|Any CPU {6D2CFE86-D6B2-4820-B3D8-62255BF94DCD}.Release|Any CPU.Build.0 = Release|Any CPU + {D7E215C6-4EE6-41AB-BD80-F126C50A590E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D7E215C6-4EE6-41AB-BD80-F126C50A590E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D7E215C6-4EE6-41AB-BD80-F126C50A590E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D7E215C6-4EE6-41AB-BD80-F126C50A590E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution {9815492D-D8F9-439C-B73C-711693755626} = {9D239135-8242-4AC0-94AE-7CCD8408B531} @@ -977,5 +983,6 @@ Global {F6217F6F-DD9C-4DBD-8A7A-894E05FCF78E} = {29984DF2-2734-483C-BC7D-F6D41599DACD} {73AB6DA8-AA91-44A9-B5E5-0670FFB6A4AC} = {29984DF2-2734-483C-BC7D-F6D41599DACD} {6D2CFE86-D6B2-4820-B3D8-62255BF94DCD} = {29984DF2-2734-483C-BC7D-F6D41599DACD} + {D7E215C6-4EE6-41AB-BD80-F126C50A590E} = {29984DF2-2734-483C-BC7D-F6D41599DACD} EndGlobalSection EndGlobal diff --git a/exercises/practice/resistor-color-duo/.config/dotnet-tools.json b/exercises/practice/resistor-color-duo/.config/dotnet-tools.json new file mode 100644 index 000000000..0f7926bad --- /dev/null +++ b/exercises/practice/resistor-color-duo/.config/dotnet-tools.json @@ -0,0 +1,12 @@ +{ + "version": 1, + "isRoot": true, + "tools": { + "fantomas-tool": { + "version": "4.7.9", + "commands": [ + "fantomas" + ] + } + } +} \ No newline at end of file diff --git a/exercises/practice/resistor-color-duo/.docs/instructions.md b/exercises/practice/resistor-color-duo/.docs/instructions.md new file mode 100644 index 000000000..4ae694da0 --- /dev/null +++ b/exercises/practice/resistor-color-duo/.docs/instructions.md @@ -0,0 +1,33 @@ +# 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. diff --git a/exercises/practice/resistor-color-duo/.meta/Example.fs b/exercises/practice/resistor-color-duo/.meta/Example.fs new file mode 100644 index 000000000..2b9717a16 --- /dev/null +++ b/exercises/practice/resistor-color-duo/.meta/Example.fs @@ -0,0 +1,19 @@ +module ResistorColorDuo + +let private colors = + [ "black" + "brown" + "red" + "orange" + "yellow" + "green" + "blue" + "violet" + "grey" + "white" ] + +let private colorCode color = List.findIndex ((=) color) colors + +let value colors = + colorCode (List.item 0 colors) * 10 + + colorCode (List.item 1 colors) diff --git a/exercises/practice/resistor-color-duo/.meta/config.json b/exercises/practice/resistor-color-duo/.meta/config.json new file mode 100644 index 000000000..2e9bfa52b --- /dev/null +++ b/exercises/practice/resistor-color-duo/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "erikschierboom" + ], + "files": { + "solution": [ + "ResistorColorDuo.fs" + ], + "test": [ + "ResistorColorDuoTests.fs" + ], + "example": [ + ".meta/Example.fs" + ] + }, + "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" +} diff --git a/exercises/practice/resistor-color-duo/.meta/tests.toml b/exercises/practice/resistor-color-duo/.meta/tests.toml new file mode 100644 index 000000000..9036fc787 --- /dev/null +++ b/exercises/practice/resistor-color-duo/.meta/tests.toml @@ -0,0 +1,31 @@ +# 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. + +[ce11995a-5b93-4950-a5e9-93423693b2fc] +description = "Brown and black" + +[7bf82f7a-af23-48ba-a97d-38d59406a920] +description = "Blue and grey" + +[f1886361-fdfd-4693-acf8-46726fe24e0c] +description = "Yellow and violet" + +[b7a6cbd2-ae3c-470a-93eb-56670b305640] +description = "White and red" + +[77a8293d-2a83-4016-b1af-991acc12b9fe] +description = "Orange and orange" + +[0c4fb44f-db7c-4d03-afa8-054350f156a8] +description = "Ignore additional colors" + +[4a8ceec5-0ab4-4904-88a4-daf953a5e818] +description = "Black and brown, one-digit" diff --git a/exercises/practice/resistor-color-duo/ResistorColorDuo.fs b/exercises/practice/resistor-color-duo/ResistorColorDuo.fs new file mode 100644 index 000000000..8111da860 --- /dev/null +++ b/exercises/practice/resistor-color-duo/ResistorColorDuo.fs @@ -0,0 +1,4 @@ +module ResistorColorDuo + +let value colors = + failwith "Please implement the 'value' function" diff --git a/exercises/practice/resistor-color-duo/ResistorColorDuo.fsproj b/exercises/practice/resistor-color-duo/ResistorColorDuo.fsproj new file mode 100644 index 000000000..6b37e4ed8 --- /dev/null +++ b/exercises/practice/resistor-color-duo/ResistorColorDuo.fsproj @@ -0,0 +1,22 @@ + + + net8.0 + false + false + true + + + + + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + \ No newline at end of file diff --git a/exercises/practice/resistor-color-duo/ResistorColorDuoTests.fs b/exercises/practice/resistor-color-duo/ResistorColorDuoTests.fs new file mode 100644 index 000000000..0b2f68dfd --- /dev/null +++ b/exercises/practice/resistor-color-duo/ResistorColorDuoTests.fs @@ -0,0 +1,35 @@ +module ResistorColorDuoTests + +open FsUnit.Xunit +open Xunit + +open ResistorColorDuo + +[] +let ``Brown and black`` () = + value [ "brown"; "black" ] |> should equal 10 + +[] +let ``Blue and grey`` () = + value [ "blue"; "grey" ] |> should equal 68 + +[] +let ``Yellow and violet`` () = + value [ "yellow"; "violet" ] |> should equal 47 + +[] +let ``White and red`` () = + value [ "white"; "red" ] |> should equal 92 + +[] +let ``Orange and orange`` () = + value [ "orange"; "orange" ] |> should equal 33 + +[] +let ``Ignore additional colors`` () = + value [ "green"; "brown"; "orange" ] + |> should equal 51 + +[] +let ``Black and brown, one-digit`` () = + value [ "black"; "brown" ] |> should equal 1 diff --git a/generators/Generators.fs b/generators/Generators.fs index b19b7b105..dbf84b510 100644 --- a/generators/Generators.fs +++ b/generators/Generators.fs @@ -2032,3 +2032,6 @@ type GameOfLife() = type ResistorColor() = inherit ExerciseGenerator() + +type ResistorColorDuo() = + inherit ExerciseGenerator()