Skip to content

Commit

Permalink
Add resistor-color exercise (#190)
Browse files Browse the repository at this point in the history
* Add resistor-color exercise

* Revert inadvertent config formatting

* Fix CI issues
  • Loading branch information
BNAndras authored Nov 15, 2023
1 parent 9dbf74b commit dfe385a
Show file tree
Hide file tree
Showing 11 changed files with 313 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,14 @@
"prerequisites": [],
"difficulty": 1
},
{
"slug": "resistor-color",
"name": "Resistor Color",
"uuid": "fd376c87-e682-4afc-b12f-446443a9b57a",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "all-your-base",
"name": "All Your Base",
Expand Down
39 changes: 39 additions & 0 deletions exercises/practice/resistor-color/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions exercises/practice/resistor-color/.meta/Example.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Here is an example solution for the ResistorColor exercise
*/
component {

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

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

function colors() {
return COLORS;
}

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

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

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

/**
* @returns
*/
function colorCode( color ) {
// Implement me here
}

/**
* @returns
*/
function colors() {
// Implement me here
}
}
35 changes: 35 additions & 0 deletions exercises/practice/resistor-color/ResistorColorTest.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
component extends="testbox.system.BaseSpec" {

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

function run(){

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

describe( 'Color codes', function(){

it( 'Black', function(){
expect( SUT.colorCode( color='black' ) ).toBe( '0' );
});

it( 'White', function(){
expect( SUT.colorCode( color='white' ) ).toBe( '9' );
});

it( 'Orange', function(){
expect( SUT.colorCode( color='orange' ) ).toBe( '3' );
});

});

it( 'Colors', function(){
expect( SUT.colors() ).toBe( ["black","brown","red","orange","yellow","green","blue","violet","grey","white"] );
});

});

}

}
103 changes: 103 additions & 0 deletions exercises/practice/resistor-color/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/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/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 dfe385a

Please sign in to comment.