add resistor-color exercise (#21)
* add resistor-color exercise * use NEW
This commit is contained in:
@@ -45,6 +45,14 @@
|
|||||||
"prerequisites": [],
|
"prerequisites": [],
|
||||||
"difficulty": 1
|
"difficulty": 1
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"slug": "resistor-color",
|
||||||
|
"name": "Resistor Color",
|
||||||
|
"uuid": "978db3d3-0a13-4ca1-b75b-051e389bf171",
|
||||||
|
"practices": [],
|
||||||
|
"prerequisites": [],
|
||||||
|
"difficulty": 1
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"slug": "leap",
|
"slug": "leap",
|
||||||
"name": "Leap",
|
"name": "Leap",
|
||||||
|
|||||||
36
exercises/practice/resistor-color/.docs/instructions.md
Normal file
36
exercises/practice/resistor-color/.docs/instructions.md
Normal file
@@ -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)
|
||||||
12
exercises/practice/resistor-color/.meta/config.json
Normal file
12
exercises/practice/resistor-color/.meta/config.json
Normal file
@@ -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"
|
||||||
|
}
|
||||||
@@ -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.
|
||||||
@@ -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.
|
||||||
@@ -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.
|
||||||
Reference in New Issue
Block a user