Skip to content

Commit

Permalink
add resistor-color-duo exercise (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
BNAndras authored Nov 14, 2023
1 parent 3775632 commit d1315f4
Showing 11 changed files with 324 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -294,6 +294,14 @@
"difficulty": 1,
"topics": null
},
{
"slug": "resistor-color-duo",
"name": "Resistor Color Duo",
"uuid": "4a1bd9da-e0d4-445f-8b66-cde7ec4888c8",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "markdown",
"name": "Markdown",
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
brown-green-violet should return 15 too, ignoring the third color.
21 changes: 21 additions & 0 deletions exercises/practice/resistor-color-duo/.meta/Example.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Here is an example solution for the ResistorColorDuo exercise
*/
component {

COLORS = ["black","brown","red","orange","yellow","green","blue","violet","grey","white"];

/**
* @returns
*/
function value( colors ) {
tens = colorCode(colors[1])
ones = colorCode(colors[2])
return tens * 10 + ones
}

function colorCode( color ) {
return ArrayFind(COLORS, color) - 1;
}

}
7 changes: 7 additions & 0 deletions exercises/practice/resistor-color-duo/.meta/ExampleTest.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
component extends="ResistorColorDuoTest" {

function beforeAll(){
SUT = createObject( 'Solution' );
}

}
20 changes: 20 additions & 0 deletions exercises/practice/resistor-color-duo/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"authors": [
"BNAndras"
],
"files": {
"solution": [
"ResistorColorDuo.cfc"
],
"test": [
"ResistorColorDuoTest.cfc"
],
"example": [
".meta/Example.cfc",
".meta/ExampleTest.cfc"
]
},
"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"
13 changes: 13 additions & 0 deletions exercises/practice/resistor-color-duo/ResistorColorDuo.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Your implementation of the ResistorColorDuo exercise
*/
component {

/**
* @returns
*/
function value( colors ) {
// Implement me here
}

}
43 changes: 43 additions & 0 deletions exercises/practice/resistor-color-duo/ResistorColorDuoTest.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
component extends="testbox.system.BaseSpec" {

function beforeAll(){
SUT = createObject( 'ResistorColorDuo' );
}

function run(){

describe( "My ResistorColorDuo class", function(){

it( 'Brown and black', function(){
expect( SUT.value( colors=["brown", "black"] ) ).toBe( '10' );
});

it( 'Blue and grey', function(){
expect( SUT.value( colors=["blue", "grey"] ) ).toBe( '68' );
});

it( 'Yellow and violet', function(){
expect( SUT.value( colors=["yellow", "violet"] ) ).toBe( '47' );
});

it( 'White and red', function(){
expect( SUT.value( colors=["white", "red"] ) ).toBe( '92' );
});

it( 'Orange and orange', function(){
expect( SUT.value( colors=["orange", "orange"] ) ).toBe( '33' );
});

it( 'Ignore additional colors', function(){
expect( SUT.value( colors=["green", "brown", "orange"] ) ).toBe( '51' );
});

it( 'Black and brown, one-digit', function(){
expect( SUT.value( colors=["black", "brown"] ) ).toBe( '1' );
});

});

}

}
103 changes: 103 additions & 0 deletions exercises/practice/resistor-color-duo/TestRunner.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**
* I am a CommandBox task runner which you can use to test your implementation of this exercise against the
* provided test suite. To use me, open the CommandBox CLI and run this:
*
* CommandBox> task run TestRunner
*
* To start up a test watcher that will automatically rerun the test suite every time you save a file change, run this:
*
* CommandBox> task run TestRunner --watcher
*
*/
component {

/**
* @solution Runs the tests against the solution
* @watcher Start up a file watch that re-runs the tests on file changes. Use Ctrl-C to stop
*/
function run( boolean solution=false, boolean watcher=false ) {

ensureTestBox();

if( watcher ) {

// Tabula rasa
command( 'cls' ).run();

// Start watcher
watch()
.paths( '*.cfc' )
.inDirectory( getCWD() )
.withDelay( 500 )
.onChange( function() {

// Clear the screen
command( 'cls' )
.run();

// This is neccessary so changes to tests get picked up right away.
pagePoolClear();

runTests( solution );

} )
.start();

} else {
runTests( solution );
}

}

/**
* Make sure the TestBox framework is installed
*/
private function ensureTestBox() {
var excerciseRoot = getCWD();
var testBoxRoot = excerciseRoot & '/testbox';

if( !directoryExists( testBoxRoot ) ) {

print.boldYellowLine( 'Installing some missing dependencies for you!' ).toConsole();
command( 'install' )
.inWorkingDirectory( excerciseRoot )
.run();
}

// Bootstrap TestBox framework
filesystemUtil.createMapping( '/testbox', testBoxRoot );
}

/**
* Invoke TestBox to run the test suite
*/
private function runTests( boolean solution=false ) {

// Create TestBox and run the tests
testData = new testbox.system.TestBox()
.runRaw( directory = {
// Find all CFCs...
mapping = filesystemUtil.makePathRelative( getCWD() ),
// ... in this directory ...
recurse = false,
// ... whose name ends in "test"
filter = function( path ) {
return path.reFind( ( solution ? 'Solution' : '' ) & 'Test.cfc$' );
}
} )
.getMemento();

// Print out the results with ANSI formatting for the CLI
getInstance( 'CLIRenderer@testbox-commands' )
.render( print, testData, true );

print.toConsole();

// Set proper exit code
if( testData.totalFail || testData.totalError ) {
setExitCode( 1 );
}
}

}

8 changes: 8 additions & 0 deletions exercises/practice/resistor-color-duo/box.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"dependencies":{
"testbox":"^2.5.0+107"
},
"installPaths":{
"testbox":"testbox/"
}
}
37 changes: 37 additions & 0 deletions exercises/practice/resistor-color-duo/index.cfm
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!---
This file will only be used if you want to start up a web server in this directory. You can do so by running:
$> box
CommandBox> install
CommandBox> server start
However, this is not necessary unless you really just want to use the HTML reporters on TestBox.
Ideally, you'll skip the need for this file entirely and just run the tests directly frm the CLI like this:
CommandBox> task run TestRunner
--->
<cfsetting showDebugOutput="false">
<cfparam name="url.reporter" default="simple">
<cfscript>
// get a list of all CFCs in this folder whose name looks like "XXXTest.cfc"
// And turn it into compnent path relative to the web root
url.bundles = directoryList(
path=expandPath( '/' ),
filter='*Test.cfc' )
.map( function( path ) {
return path
.replaceNoCase( expandPath( '/' ), '' )
.left( -4 )
} )
.toList();
</cfscript>
<!--- Ensure TestBox --->
<cfif fileExists( "/testbox/system/runners/HTMLRunner.cfm" )>
<!--- Include the TestBox HTML Runner --->
<cfinclude template="/testbox/system/runners/HTMLRunner.cfm">
<cfelse>
Oops, you don't have TestBox installed yet! Please run <b>box install</b> from the root of your excercise folder and refresh this page.
</cfif>

0 comments on commit d1315f4

Please sign in to comment.