From aa89f9b36b5d5710da285b2eeb71197ecf612070 Mon Sep 17 00:00:00 2001 From: Lars Hvam Date: Sun, 28 Nov 2021 07:40:00 +0100 Subject: [PATCH] add resistor-color exercise (#21) * add resistor-color exercise * use NEW --- config.json | 8 ++++ .../resistor-color/.docs/instructions.md | 36 +++++++++++++++++ .../practice/resistor-color/.meta/config.json | 12 ++++++ .../.meta/zcl_resistor_color.clas.abap | 37 ++++++++++++++++++ .../zcl_resistor_color.clas.abap | 16 ++++++++ .../zcl_resistor_color.clas.testclasses.abap | 39 +++++++++++++++++++ 6 files changed, 148 insertions(+) create mode 100644 exercises/practice/resistor-color/.docs/instructions.md create mode 100644 exercises/practice/resistor-color/.meta/config.json create mode 100644 exercises/practice/resistor-color/.meta/zcl_resistor_color.clas.abap create mode 100644 exercises/practice/resistor-color/zcl_resistor_color.clas.abap create mode 100644 exercises/practice/resistor-color/zcl_resistor_color.clas.testclasses.abap diff --git a/config.json b/config.json index c58afa0..348b444 100644 --- a/config.json +++ b/config.json @@ -45,6 +45,14 @@ "prerequisites": [], "difficulty": 1 }, + { + "slug": "resistor-color", + "name": "Resistor Color", + "uuid": "978db3d3-0a13-4ca1-b75b-051e389bf171", + "practices": [], + "prerequisites": [], + "difficulty": 1 + }, { "slug": "leap", "name": "Leap", diff --git a/exercises/practice/resistor-color/.docs/instructions.md b/exercises/practice/resistor-color/.docs/instructions.md new file mode 100644 index 0000000..e4ec429 --- /dev/null +++ b/exercises/practice/resistor-color/.docs/instructions.md @@ -0,0 +1,36 @@ +# Description + +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](https://en.wikipedia.org/wiki/Electronic_color_code) \ 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..93963dc --- /dev/null +++ b/exercises/practice/resistor-color/.meta/config.json @@ -0,0 +1,12 @@ +{ + "blurb": "Resistor Color", + "authors": ["larshp"], + "contributors": [], + "files": { + "solution": ["zcl_resistor_color.clas.abap"], + "test": ["zcl_resistor_color.clas.testclasses.abap"], + "example": [".meta/zcl_resistor_color.clas.abap"] + }, + "source": "Maud de Vries, Erik Schierboom", + "source_url": "https://github.com/exercism/problem-specifications/issues/1458" +} diff --git a/exercises/practice/resistor-color/.meta/zcl_resistor_color.clas.abap b/exercises/practice/resistor-color/.meta/zcl_resistor_color.clas.abap new file mode 100644 index 0000000..70623c0 --- /dev/null +++ b/exercises/practice/resistor-color/.meta/zcl_resistor_color.clas.abap @@ -0,0 +1,37 @@ +CLASS zcl_resistor_color DEFINITION PUBLIC CREATE PUBLIC. + PUBLIC SECTION. + METHODS resistor_color + IMPORTING + color_code TYPE string + RETURNING + VALUE(value) TYPE i. +ENDCLASS. + +CLASS zcl_resistor_color IMPLEMENTATION. + + METHOD resistor_color. + + CASE color_code. + WHEN 'brown'. + value = 1. + WHEN 'red'. + value = 2. + WHEN 'orange'. + value = 3. + WHEN 'yellow'. + value = 4. + WHEN 'green'. + value = 5. + WHEN 'blue'. + value = 6. + WHEN 'violet'. + value = 7. + WHEN 'grey'. + value = 8. + WHEN 'white'. + value = 9. + ENDCASE. + + ENDMETHOD. + +ENDCLASS. \ No newline at end of file diff --git a/exercises/practice/resistor-color/zcl_resistor_color.clas.abap b/exercises/practice/resistor-color/zcl_resistor_color.clas.abap new file mode 100644 index 0000000..7cedbed --- /dev/null +++ b/exercises/practice/resistor-color/zcl_resistor_color.clas.abap @@ -0,0 +1,16 @@ +CLASS zcl_resistor_color DEFINITION PUBLIC CREATE PUBLIC. + PUBLIC SECTION. + METHODS resistor_color + IMPORTING + color_code TYPE string + RETURNING + VALUE(value) TYPE i. +ENDCLASS. + +CLASS zcl_resistor_color IMPLEMENTATION. + + METHOD resistor_color. +* add solution here + ENDMETHOD. + +ENDCLASS. \ No newline at end of file diff --git a/exercises/practice/resistor-color/zcl_resistor_color.clas.testclasses.abap b/exercises/practice/resistor-color/zcl_resistor_color.clas.testclasses.abap new file mode 100644 index 0000000..0ebb342 --- /dev/null +++ b/exercises/practice/resistor-color/zcl_resistor_color.clas.testclasses.abap @@ -0,0 +1,39 @@ +CLASS ltcl_test DEFINITION FOR TESTING + DURATION SHORT + RISK LEVEL HARMLESS FINAL. + + PRIVATE SECTION. + DATA cut TYPE REF TO zcl_resistor_color. + METHODS setup. + METHODS test_black FOR TESTING. + METHODS test_white FOR TESTING. + METHODS test_orange FOR TESTING. + +ENDCLASS. + + +CLASS ltcl_test IMPLEMENTATION. + + METHOD setup. + cut = NEW #( ). + ENDMETHOD. + + METHOD test_black. + cl_abap_unit_assert=>assert_equals( + act = cut->resistor_color( 'black' ) + exp = 0 ). + ENDMETHOD. + + METHOD test_white. + cl_abap_unit_assert=>assert_equals( + act = cut->resistor_color( 'white' ) + exp = 9 ). + ENDMETHOD. + + METHOD test_orange. + cl_abap_unit_assert=>assert_equals( + act = cut->resistor_color( 'orange' ) + exp = 3 ). + ENDMETHOD. + +ENDCLASS. \ No newline at end of file