Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add resistor-color-duo exercise #1288

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
7 changes: 7 additions & 0 deletions exercises/Exercises.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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
12 changes: 12 additions & 0 deletions exercises/practice/resistor-color-duo/.config/dotnet-tools.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": 1,
"isRoot": true,
"tools": {
"fantomas-tool": {
"version": "4.7.9",
"commands": [
"fantomas"
]
}
}
}
33 changes: 33 additions & 0 deletions exercises/practice/resistor-color-duo/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -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.
19 changes: 19 additions & 0 deletions exercises/practice/resistor-color-duo/.meta/Example.fs
Original file line number Diff line number Diff line change
@@ -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)
19 changes: 19 additions & 0 deletions exercises/practice/resistor-color-duo/.meta/config.json
Original file line number Diff line number Diff line change
@@ -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"
}
31 changes: 31 additions & 0 deletions exercises/practice/resistor-color-duo/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -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"
4 changes: 4 additions & 0 deletions exercises/practice/resistor-color-duo/ResistorColorDuo.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module ResistorColorDuo

let value colors =
failwith "Please implement the 'value' function"
22 changes: 22 additions & 0 deletions exercises/practice/resistor-color-duo/ResistorColorDuo.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable>
<GenerateProgramFile>false</GenerateProgramFile>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<Compile Include="ResistorColorDuo.fs" />
<Compile Include="ResistorColorDuoTests.fs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Exercism.Tests" Version="0.1.0-beta1" />
<PackageReference Include="FsUnit.xUnit" Version="4.0.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
</Project>
35 changes: 35 additions & 0 deletions exercises/practice/resistor-color-duo/ResistorColorDuoTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module ResistorColorDuoTests

open FsUnit.Xunit
open Xunit

open ResistorColorDuo

[<Fact>]
let ``Brown and black`` () =
value [ "brown"; "black" ] |> should equal 10

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Blue and grey`` () =
value [ "blue"; "grey" ] |> should equal 68

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Yellow and violet`` () =
value [ "yellow"; "violet" ] |> should equal 47

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``White and red`` () =
value [ "white"; "red" ] |> should equal 92

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Orange and orange`` () =
value [ "orange"; "orange" ] |> should equal 33

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Ignore additional colors`` () =
value [ "green"; "brown"; "orange" ]
|> should equal 51

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Black and brown, one-digit`` () =
value [ "black"; "brown" ] |> should equal 1
3 changes: 3 additions & 0 deletions generators/Generators.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2032,3 +2032,6 @@ type GameOfLife() =

type ResistorColor() =
inherit ExerciseGenerator()

type ResistorColorDuo() =
inherit ExerciseGenerator()
Loading