From dfe385aad69ba9d226da905bc47390e93688e04d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20B=20Nagy?= <20251272+BNAndras@users.noreply.github.com> Date: Tue, 14 Nov 2023 23:24:26 -0800 Subject: [PATCH] Add resistor-color exercise (#190) * Add resistor-color exercise * Revert inadvertent config formatting * Fix CI issues --- config.json | 8 ++ .../resistor-color/.docs/instructions.md | 39 +++++++ .../practice/resistor-color/.meta/Example.cfc | 16 +++ .../resistor-color/.meta/ExampleTest.cfc | 7 ++ .../practice/resistor-color/.meta/config.json | 19 ++++ .../practice/resistor-color/.meta/tests.toml | 22 ++++ .../practice/resistor-color/ResistorColor.cfc | 19 ++++ .../resistor-color/ResistorColorTest.cfc | 35 ++++++ .../practice/resistor-color/TestRunner.cfc | 103 ++++++++++++++++++ exercises/practice/resistor-color/box.json | 8 ++ exercises/practice/resistor-color/index.cfm | 37 +++++++ 11 files changed, 313 insertions(+) create mode 100644 exercises/practice/resistor-color/.docs/instructions.md create mode 100644 exercises/practice/resistor-color/.meta/Example.cfc create mode 100644 exercises/practice/resistor-color/.meta/ExampleTest.cfc create mode 100644 exercises/practice/resistor-color/.meta/config.json create mode 100644 exercises/practice/resistor-color/.meta/tests.toml create mode 100644 exercises/practice/resistor-color/ResistorColor.cfc create mode 100644 exercises/practice/resistor-color/ResistorColorTest.cfc create mode 100644 exercises/practice/resistor-color/TestRunner.cfc create mode 100644 exercises/practice/resistor-color/box.json create mode 100644 exercises/practice/resistor-color/index.cfm diff --git a/config.json b/config.json index 6e060f4..a9b4137 100644 --- a/config.json +++ b/config.json @@ -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", diff --git a/exercises/practice/resistor-color/.docs/instructions.md b/exercises/practice/resistor-color/.docs/instructions.md new file mode 100644 index 0000000..646c143 --- /dev/null +++ b/exercises/practice/resistor-color/.docs/instructions.md @@ -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 diff --git a/exercises/practice/resistor-color/.meta/Example.cfc b/exercises/practice/resistor-color/.meta/Example.cfc new file mode 100644 index 0000000..5e46e5d --- /dev/null +++ b/exercises/practice/resistor-color/.meta/Example.cfc @@ -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; + } + +} \ No newline at end of file diff --git a/exercises/practice/resistor-color/.meta/ExampleTest.cfc b/exercises/practice/resistor-color/.meta/ExampleTest.cfc new file mode 100644 index 0000000..308ed1f --- /dev/null +++ b/exercises/practice/resistor-color/.meta/ExampleTest.cfc @@ -0,0 +1,7 @@ +component extends="ResistorColorTest" { + + function beforeAll(){ + SUT = createObject( 'Solution' ); + } + +} \ No newline at end of file diff --git a/exercises/practice/resistor-color/.meta/config.json b/exercises/practice/resistor-color/.meta/config.json new file mode 100644 index 0000000..6e04656 --- /dev/null +++ b/exercises/practice/resistor-color/.meta/config.json @@ -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" +} diff --git a/exercises/practice/resistor-color/.meta/tests.toml b/exercises/practice/resistor-color/.meta/tests.toml new file mode 100644 index 0000000..9d4ee97 --- /dev/null +++ b/exercises/practice/resistor-color/.meta/tests.toml @@ -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" diff --git a/exercises/practice/resistor-color/ResistorColor.cfc b/exercises/practice/resistor-color/ResistorColor.cfc new file mode 100644 index 0000000..aa0876f --- /dev/null +++ b/exercises/practice/resistor-color/ResistorColor.cfc @@ -0,0 +1,19 @@ +/** +* Your implementation of the ResistorColor exercise +*/ +component { + + /** + * @returns + */ + function colorCode( color ) { + // Implement me here + } + + /** + * @returns + */ + function colors() { + // Implement me here + } +} \ No newline at end of file diff --git a/exercises/practice/resistor-color/ResistorColorTest.cfc b/exercises/practice/resistor-color/ResistorColorTest.cfc new file mode 100644 index 0000000..8e0d061 --- /dev/null +++ b/exercises/practice/resistor-color/ResistorColorTest.cfc @@ -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"] ); + }); + + }); + + } + +} \ No newline at end of file diff --git a/exercises/practice/resistor-color/TestRunner.cfc b/exercises/practice/resistor-color/TestRunner.cfc new file mode 100644 index 0000000..762b153 --- /dev/null +++ b/exercises/practice/resistor-color/TestRunner.cfc @@ -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 ); + } + } + +} + diff --git a/exercises/practice/resistor-color/box.json b/exercises/practice/resistor-color/box.json new file mode 100644 index 0000000..3b95f7e --- /dev/null +++ b/exercises/practice/resistor-color/box.json @@ -0,0 +1,8 @@ +{ + "dependencies":{ + "testbox":"^2.5.0+107" + }, + "installPaths":{ + "testbox":"testbox/" + } +} \ No newline at end of file diff --git a/exercises/practice/resistor-color/index.cfm b/exercises/practice/resistor-color/index.cfm new file mode 100644 index 0000000..1e35079 --- /dev/null +++ b/exercises/practice/resistor-color/index.cfm @@ -0,0 +1,37 @@ + + + + + // 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(); + + + + + + + + Oops, you don't have TestBox installed yet! Please run box install from the root of your excercise folder and refresh this page. +