Skip to content

Commit 5b86a83

Browse files
authored
Add resistor-color-duo (#347)
1 parent 5abf7e5 commit 5b86a83

9 files changed

Lines changed: 265 additions & 0 deletions

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@
126126
"prerequisites": [],
127127
"difficulty": 1
128128
},
129+
{
130+
"slug": "resistor-color-duo",
131+
"name": "Resistor Color Duo",
132+
"uuid": "02daf508-b4d8-47c9-a9c7-f26e7032258c",
133+
"practices": [],
134+
"prerequisites": [],
135+
"difficulty": 1
136+
},
129137
{
130138
"slug": "leap",
131139
"name": "Leap",
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Instructions
2+
3+
If you want to build something using a Raspberry Pi, you'll probably use _resistors_.
4+
For this exercise, you need to know two things about them:
5+
6+
- Each resistor has a resistance value.
7+
- Resistors are small - so small in fact that if you printed the resistance value on them, it would be hard to read.
8+
9+
To get around this problem, manufacturers print color-coded bands onto the resistors to denote their resistance values.
10+
Each band has a position and a numeric value.
11+
12+
The first 2 bands of a resistor have a simple encoding scheme: each color maps to a single number.
13+
For example, if they printed a brown band (value 1) followed by a green band (value 5), it would translate to the number 15.
14+
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.
16+
The program will take color names as input and output a two digit number, even if the input is more than two colors!
17+
18+
The band colors are encoded as follows:
19+
20+
- black: 0
21+
- brown: 1
22+
- red: 2
23+
- orange: 3
24+
- yellow: 4
25+
- green: 5
26+
- blue: 6
27+
- violet: 7
28+
- grey: 8
29+
- white: 9
30+
31+
From the example above:
32+
brown-green should return 15, and
33+
brown-green-violet should return 15 too, ignoring the third color.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"BNAndras"
4+
],
5+
"files": {
6+
"solution": [
7+
"zcl_resistor_color_duo.clas.abap"
8+
],
9+
"test": [
10+
"zcl_resistor_color_duo.clas.testclasses.abap"
11+
],
12+
"example": [
13+
".meta/zcl_resistor_color_duo.clas.abap"
14+
]
15+
},
16+
"blurb": "Convert color codes, as used on resistors, to a numeric value.",
17+
"source": "Maud de Vries, Erik Schierboom",
18+
"source_url": "https://github.com/exercism/problem-specifications/issues/1464"
19+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[ce11995a-5b93-4950-a5e9-93423693b2fc]
13+
description = "Brown and black"
14+
15+
[7bf82f7a-af23-48ba-a97d-38d59406a920]
16+
description = "Blue and grey"
17+
18+
[f1886361-fdfd-4693-acf8-46726fe24e0c]
19+
description = "Yellow and violet"
20+
21+
[b7a6cbd2-ae3c-470a-93eb-56670b305640]
22+
description = "White and red"
23+
24+
[77a8293d-2a83-4016-b1af-991acc12b9fe]
25+
description = "Orange and orange"
26+
27+
[0c4fb44f-db7c-4d03-afa8-054350f156a8]
28+
description = "Ignore additional colors"
29+
30+
[4a8ceec5-0ab4-4904-88a4-daf953a5e818]
31+
description = "Black and brown, one-digit"
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
CLASS zcl_resistor_color_duo DEFINITION
2+
PUBLIC
3+
FINAL
4+
CREATE PUBLIC.
5+
6+
PUBLIC SECTION.
7+
METHODS value
8+
IMPORTING
9+
colors TYPE string_table
10+
RETURNING
11+
VALUE(result) TYPE i.
12+
PROTECTED SECTION.
13+
PRIVATE SECTION.
14+
METHODS color_code
15+
IMPORTING
16+
color TYPE string
17+
RETURNING
18+
VALUE(result) TYPE i.
19+
ENDCLASS.
20+
21+
CLASS zcl_resistor_color_duo IMPLEMENTATION.
22+
METHOD value.
23+
DATA(color1) = colors[ 1 ].
24+
DATA(color2) = colors[ 2 ].
25+
26+
result = color_code( color1 ) * 10 + color_code( color2 ).
27+
ENDMETHOD.
28+
29+
METHOD color_code.
30+
CASE color.
31+
WHEN 'black'.
32+
result = 0.
33+
WHEN 'brown'.
34+
result = 1.
35+
WHEN 'red'.
36+
result = 2.
37+
WHEN 'orange'.
38+
result = 3.
39+
WHEN 'yellow'.
40+
result = 4.
41+
WHEN 'green'.
42+
result = 5.
43+
WHEN 'blue'.
44+
result = 6.
45+
WHEN 'violet'.
46+
result = 7.
47+
WHEN 'grey'.
48+
result = 8.
49+
WHEN 'white'.
50+
result = 9.
51+
ENDCASE.
52+
ENDMETHOD.
53+
ENDCLASS.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<abapGit version="v1.0.0" serializer="LCL_OBJECT_DEVC" serializer_version="v1.0.0">
3+
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
4+
<asx:values>
5+
<DEVC>
6+
<CTEXT>Exercism: Resistor Color Duo</CTEXT>
7+
</DEVC>
8+
</asx:values>
9+
</asx:abap>
10+
</abapGit>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
CLASS zcl_resistor_color_duo DEFINITION
2+
PUBLIC
3+
FINAL
4+
CREATE PUBLIC.
5+
6+
PUBLIC SECTION.
7+
METHODS value
8+
IMPORTING
9+
colors TYPE string_table
10+
RETURNING
11+
VALUE(result) TYPE i.
12+
PROTECTED SECTION.
13+
PRIVATE SECTION.
14+
ENDCLASS.
15+
16+
CLASS zcl_resistor_color_duo IMPLEMENTATION.
17+
METHOD value.
18+
"Implement solution
19+
ENDMETHOD.
20+
ENDCLASS.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
*"* use this source file for your ABAP unit test classes
2+
CLASS ltcl_resistor_color_duo DEFINITION FINAL FOR TESTING
3+
DURATION SHORT
4+
RISK LEVEL HARMLESS.
5+
6+
PRIVATE SECTION.
7+
DATA cut TYPE REF TO zcl_resistor_color_duo.
8+
METHODS setup.
9+
METHODS:
10+
test_brown_and_black FOR TESTING,
11+
test_blue_and_grey FOR TESTING,
12+
test_yellow_and_violet FOR TESTING,
13+
test_white_and_red FOR TESTING,
14+
test_orange_and_orange FOR TESTING,
15+
test_ignore_additional_colors FOR TESTING,
16+
test_black_and_brown_one_digit FOR TESTING.
17+
ENDCLASS.
18+
19+
CLASS ltcl_resistor_color_duo IMPLEMENTATION.
20+
21+
METHOD setup.
22+
cut = NEW zcl_resistor_color_duo( ).
23+
ENDMETHOD.
24+
25+
METHOD test_brown_and_black.
26+
DATA(colors) = VALUE string_table( ( `brown` ) ( `black` ) ).
27+
cl_abap_unit_assert=>assert_equals(
28+
act = cut->value( colors )
29+
exp = 10 ).
30+
ENDMETHOD.
31+
32+
METHOD test_blue_and_grey.
33+
DATA(colors) = VALUE string_table( ( `blue` ) ( `grey` ) ).
34+
cl_abap_unit_assert=>assert_equals(
35+
act = cut->value( colors )
36+
exp = 68 ).
37+
ENDMETHOD.
38+
39+
METHOD test_yellow_and_violet.
40+
DATA(colors) = VALUE string_table( ( `yellow` ) ( `violet` ) ).
41+
cl_abap_unit_assert=>assert_equals(
42+
act = cut->value( colors )
43+
exp = 47 ).
44+
ENDMETHOD.
45+
46+
METHOD test_white_and_red.
47+
DATA(colors) = VALUE string_table( ( `white` ) ( `red` ) ).
48+
cl_abap_unit_assert=>assert_equals(
49+
act = cut->value( colors )
50+
exp = 92 ).
51+
ENDMETHOD.
52+
53+
METHOD test_orange_and_orange.
54+
DATA(colors) = VALUE string_table( ( `orange` ) ( `orange` ) ).
55+
cl_abap_unit_assert=>assert_equals(
56+
act = cut->value( colors )
57+
exp = 33 ).
58+
ENDMETHOD.
59+
60+
METHOD test_ignore_additional_colors.
61+
DATA(colors) = VALUE string_table( ( `green` ) ( `brown` ) ( `orange` ) ).
62+
cl_abap_unit_assert=>assert_equals(
63+
act = cut->value( colors )
64+
exp = 51 ).
65+
ENDMETHOD.
66+
67+
METHOD test_black_and_brown_one_digit.
68+
DATA(colors) = VALUE string_table( ( `black` ) ( `brown` ) ).
69+
cl_abap_unit_assert=>assert_equals(
70+
act = cut->value( colors )
71+
exp = 1 ).
72+
ENDMETHOD.
73+
74+
ENDCLASS.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<abapGit version="v1.0.0" serializer="LCL_OBJECT_CLAS" serializer_version="v1.0.0">
3+
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
4+
<asx:values>
5+
<VSEOCLASS>
6+
<CLSNAME>ZCL_RESISTOR_COLOR_DUO</CLSNAME>
7+
<LANGU>E</LANGU>
8+
<DESCRIPT>Exercism: Resistor Color Duo</DESCRIPT>
9+
<STATE>1</STATE>
10+
<CLSCCINCL>X</CLSCCINCL>
11+
<FIXPT>X</FIXPT>
12+
<UNICODE>X</UNICODE>
13+
<WITH_UNIT_TESTS>X</WITH_UNIT_TESTS>
14+
</VSEOCLASS>
15+
</asx:values>
16+
</asx:abap>
17+
</abapGit>

0 commit comments

Comments
 (0)