From 4f97eb4947ee7bc1d748fa7d29e19b97502bc0ce Mon Sep 17 00:00:00 2001 From: Erik Schierboom Date: Mon, 21 Oct 2024 14:01:19 +0200 Subject: [PATCH] Add `resistor-color-trio` exercise (#1293) --- config.json | 14 +++++ exercises/Exercises.sln | 7 +++ .../.config/dotnet-tools.json | 12 ++++ .../resistor-color-trio/.docs/instructions.md | 56 ++++++++++++++++++ .../resistor-color-trio/.meta/Example.fs | 28 +++++++++ .../resistor-color-trio/.meta/config.json | 19 ++++++ .../resistor-color-trio/.meta/tests.toml | 40 +++++++++++++ .../resistor-color-trio/ResistorColorTrio.fs | 4 ++ .../ResistorColorTrio.fsproj | 22 +++++++ .../ResistorColorTrioTests.fs | 59 +++++++++++++++++++ generators/Generators.fs | 3 + 11 files changed, 264 insertions(+) create mode 100644 exercises/practice/resistor-color-trio/.config/dotnet-tools.json create mode 100644 exercises/practice/resistor-color-trio/.docs/instructions.md create mode 100644 exercises/practice/resistor-color-trio/.meta/Example.fs create mode 100644 exercises/practice/resistor-color-trio/.meta/config.json create mode 100644 exercises/practice/resistor-color-trio/.meta/tests.toml create mode 100644 exercises/practice/resistor-color-trio/ResistorColorTrio.fs create mode 100644 exercises/practice/resistor-color-trio/ResistorColorTrio.fsproj create mode 100644 exercises/practice/resistor-color-trio/ResistorColorTrioTests.fs diff --git a/config.json b/config.json index 8eb35bf46..9455b508e 100644 --- a/config.json +++ b/config.json @@ -2191,6 +2191,20 @@ "strings" ], "difficulty": 3 + }, + { + "slug": "resistor-color-trio", + "name": "Resistor Color Trio", + "uuid": "6f58712b-8b74-4b4b-b6b7-1237dd717c04", + "practices": [ + "string-formatting" + ], + "prerequisites": [ + "numbers", + "lists", + "strings" + ], + "difficulty": 3 } ], "foregone": [ diff --git a/exercises/Exercises.sln b/exercises/Exercises.sln index bc05554ce..78358a3d0 100644 --- a/exercises/Exercises.sln +++ b/exercises/Exercises.sln @@ -303,6 +303,8 @@ Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "ZebraPuzzle", "practice\zeb EndProject Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Zipper", "practice\zipper\Zipper.fsproj", "{32F8738C-2782-4881-95C0-C621DC0D7ED9}" EndProject +Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "ResistorColorTrio", "practice\resistor-color-trio\ResistorColorTrio.fsproj", "{1850FAE9-5ACB-41D0-91BB-AD17A1021248}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -892,6 +894,10 @@ Global {32F8738C-2782-4881-95C0-C621DC0D7ED9}.Debug|Any CPU.Build.0 = Debug|Any CPU {32F8738C-2782-4881-95C0-C621DC0D7ED9}.Release|Any CPU.ActiveCfg = Release|Any CPU {32F8738C-2782-4881-95C0-C621DC0D7ED9}.Release|Any CPU.Build.0 = Release|Any CPU + {1850FAE9-5ACB-41D0-91BB-AD17A1021248}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1850FAE9-5ACB-41D0-91BB-AD17A1021248}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1850FAE9-5ACB-41D0-91BB-AD17A1021248}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1850FAE9-5ACB-41D0-91BB-AD17A1021248}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution {B404AA3C-A226-409A-A035-6C1DC66940DD} = {B7E719DB-FB8D-43B4-B529-55FCF6E3DC3F} @@ -1042,5 +1048,6 @@ Global {4AD3B365-83FE-4C43-BEB5-E7DC5CCED892} = {391BEEC4-91A8-43F3-AE94-D5CB9A8FA611} {A6E25412-34F6-49ED-834B-8A551CF3F2D3} = {391BEEC4-91A8-43F3-AE94-D5CB9A8FA611} {32F8738C-2782-4881-95C0-C621DC0D7ED9} = {391BEEC4-91A8-43F3-AE94-D5CB9A8FA611} + {1850FAE9-5ACB-41D0-91BB-AD17A1021248} = {391BEEC4-91A8-43F3-AE94-D5CB9A8FA611} EndGlobalSection EndGlobal diff --git a/exercises/practice/resistor-color-trio/.config/dotnet-tools.json b/exercises/practice/resistor-color-trio/.config/dotnet-tools.json new file mode 100644 index 000000000..0f7926bad --- /dev/null +++ b/exercises/practice/resistor-color-trio/.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-trio/.docs/instructions.md b/exercises/practice/resistor-color-trio/.docs/instructions.md new file mode 100644 index 000000000..1ac5cf5e9 --- /dev/null +++ b/exercises/practice/resistor-color-trio/.docs/instructions.md @@ -0,0 +1,56 @@ +# Instructions + +If you want to build something using a Raspberry Pi, you'll probably use _resistors_. +For this exercise, you need to know only three 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 acts as a digit of a 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 3 colors as input, and outputs the correct value, in ohms. + The color bands are encoded as follows: + +- black: 0 +- brown: 1 +- red: 2 +- orange: 3 +- yellow: 4 +- green: 5 +- blue: 6 +- violet: 7 +- grey: 8 +- white: 9 + +In Resistor Color Duo you decoded the first two colors. +For instance: orange-orange got the main value `33`. +The third color stands for how many zeros need to be added to the main value. +The main value plus the zeros gives us a value in ohms. +For the exercise it doesn't matter what ohms really are. +For example: + +- orange-orange-black would be 33 and no zeros, which becomes 33 ohms. +- orange-orange-red would be 33 and 2 zeros, which becomes 3300 ohms. +- orange-orange-orange would be 33 and 3 zeros, which becomes 33000 ohms. + +(If Math is your thing, you may want to think of the zeros as exponents of 10. +If Math is not your thing, go with the zeros. +It really is the same thing, just in plain English instead of Math lingo.) + +This exercise is about translating the colors into a label: + +> "... ohms" + +So an input of `"orange", "orange", "black"` should return: + +> "33 ohms" + +When we get to larger resistors, a [metric prefix][metric-prefix] is used to indicate a larger magnitude of ohms, such as "kiloohms". +That is similar to saying "2 kilometers" instead of "2000 meters", or "2 kilograms" for "2000 grams". + +For example, an input of `"orange", "orange", "orange"` should return: + +> "33 kiloohms" + +[metric-prefix]: https://en.wikipedia.org/wiki/Metric_prefix diff --git a/exercises/practice/resistor-color-trio/.meta/Example.fs b/exercises/practice/resistor-color-trio/.meta/Example.fs new file mode 100644 index 000000000..0066a8802 --- /dev/null +++ b/exercises/practice/resistor-color-trio/.meta/Example.fs @@ -0,0 +1,28 @@ +module ResistorColorTrio + +let private allColors = + [ "black" + "brown" + "red" + "orange" + "yellow" + "green" + "blue" + "violet" + "grey" + "white" ] + +let private colorCode color = List.findIndex ((=) color) allColors + +let private value colors = + let first = colorCode (List.item 0 colors) + let second = colorCode (List.item 1 colors) + let third = colorCode (List.item 2 colors) + uint64 (first * 10 + second) * uint64(pown 10 third) + +let label colors = + match value colors with + | ohms when ohms < 1_000UL -> $"{ohms} ohms" + | ohms when ohms < 1_000_000UL -> $"{ohms / 1000UL} kiloohms" + | ohms when ohms < 1_000_000_000UL -> $"{ohms / 1_000_000UL} megaohms" + | ohms -> $"{ohms / 1_000_000_000UL} gigaohms" diff --git a/exercises/practice/resistor-color-trio/.meta/config.json b/exercises/practice/resistor-color-trio/.meta/config.json new file mode 100644 index 000000000..49ff7dc38 --- /dev/null +++ b/exercises/practice/resistor-color-trio/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "erikschierboom" + ], + "files": { + "solution": [ + "ResistorColorTrio.fs" + ], + "test": [ + "ResistorColorTrioTests.fs" + ], + "example": [ + ".meta/Example.fs" + ] + }, + "blurb": "Convert color codes, as used on resistors, to a human-readable label.", + "source": "Maud de Vries, Erik Schierboom", + "source_url": "https://github.com/exercism/problem-specifications/issues/1549" +} diff --git a/exercises/practice/resistor-color-trio/.meta/tests.toml b/exercises/practice/resistor-color-trio/.meta/tests.toml new file mode 100644 index 000000000..b7d45fa5d --- /dev/null +++ b/exercises/practice/resistor-color-trio/.meta/tests.toml @@ -0,0 +1,40 @@ +# 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. + +[d6863355-15b7-40bb-abe0-bfb1a25512ed] +description = "Orange and orange and black" + +[1224a3a9-8c8e-4032-843a-5224e04647d6] +description = "Blue and grey and brown" + +[b8bda7dc-6b95-4539-abb2-2ad51d66a207] +description = "Red and black and red" + +[5b1e74bc-d838-4eda-bbb3-eaba988e733b] +description = "Green and brown and orange" + +[f5d37ef9-1919-4719-a90d-a33c5a6934c9] +description = "Yellow and violet and yellow" + +[5f6404a7-5bb3-4283-877d-3d39bcc33854] +description = "Blue and violet and blue" + +[7d3a6ab8-e40e-46c3-98b1-91639fff2344] +description = "Minimum possible value" + +[ca0aa0ac-3825-42de-9f07-dac68cc580fd] +description = "Maximum possible value" + +[0061a76c-903a-4714-8ce2-f26ce23b0e09] +description = "First two colors make an invalid octal number" + +[30872c92-f567-4b69-a105-8455611c10c4] +description = "Ignore extra colors" diff --git a/exercises/practice/resistor-color-trio/ResistorColorTrio.fs b/exercises/practice/resistor-color-trio/ResistorColorTrio.fs new file mode 100644 index 000000000..6dbd01447 --- /dev/null +++ b/exercises/practice/resistor-color-trio/ResistorColorTrio.fs @@ -0,0 +1,4 @@ +module ResistorColorTrio + +let label colors = + failwith "Please implement the 'label' function" diff --git a/exercises/practice/resistor-color-trio/ResistorColorTrio.fsproj b/exercises/practice/resistor-color-trio/ResistorColorTrio.fsproj new file mode 100644 index 000000000..58e5ed465 --- /dev/null +++ b/exercises/practice/resistor-color-trio/ResistorColorTrio.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-trio/ResistorColorTrioTests.fs b/exercises/practice/resistor-color-trio/ResistorColorTrioTests.fs new file mode 100644 index 000000000..7df6b35a4 --- /dev/null +++ b/exercises/practice/resistor-color-trio/ResistorColorTrioTests.fs @@ -0,0 +1,59 @@ +module ResistorColorTrioTests + +open FsUnit.Xunit +open Xunit + +open ResistorColorTrio + +[] +let ``Orange and orange and black`` () = + label [ "orange"; "orange"; "black" ] + |> should equal "33 ohms" + +[] +let ``Blue and grey and brown`` () = + label [ "blue"; "grey"; "brown" ] + |> should equal "680 ohms" + +[] +let ``Red and black and red`` () = + label [ "red"; "black"; "red" ] + |> should equal "2 kiloohms" + +[] +let ``Green and brown and orange`` () = + label [ "green"; "brown"; "orange" ] + |> should equal "51 kiloohms" + +[] +let ``Yellow and violet and yellow`` () = + label [ "yellow"; "violet"; "yellow" ] + |> should equal "470 kiloohms" + +[] +let ``Blue and violet and blue`` () = + label [ "blue"; "violet"; "blue" ] + |> should equal "67 megaohms" + +[] +let ``Minimum possible value`` () = + label [ "black"; "black"; "black" ] + |> should equal "0 ohms" + +[] +let ``Maximum possible value`` () = + label [ "white"; "white"; "white" ] + |> should equal "99 gigaohms" + +[] +let ``First two colors make an invalid octal number`` () = + label [ "black"; "grey"; "black" ] + |> should equal "8 ohms" + +[] +let ``Ignore extra colors`` () = + label [ "blue" + "green" + "yellow" + "orange" ] + |> should equal "650 kiloohms" diff --git a/generators/Generators.fs b/generators/Generators.fs index f72e2210f..c814e814e 100644 --- a/generators/Generators.fs +++ b/generators/Generators.fs @@ -2047,3 +2047,6 @@ type Knapsack() = type BottleSong() = inherit ExerciseGenerator() + +type ResistorColorTrio() = + inherit ExerciseGenerator()