Skip to content

Commit b845274

Browse files
authored
Add resistor-color-trio (#348)
1 parent 5b86a83 commit b845274

9 files changed

Lines changed: 340 additions & 0 deletions

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@
134134
"prerequisites": [],
135135
"difficulty": 1
136136
},
137+
{
138+
"slug": "resistor-color-trio",
139+
"name": "Resistor Color Trio",
140+
"uuid": "5d31468c-8e83-4901-8e08-0397f09e0462",
141+
"practices": [],
142+
"prerequisites": [],
143+
"difficulty": 1
144+
},
137145
{
138146
"slug": "leap",
139147
"name": "Leap",
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 only three 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+
To get around this problem, manufacturers print color-coded bands onto the resistors to denote their resistance values.
9+
- Each band acts as a digit of a number.
10+
For example, if they printed a brown band (value 1) followed by a green band (value 5), it would translate to the number 15.
11+
In this exercise, you are going to create a helpful program so that you don't have to remember the values of the bands.
12+
The program will take 3 colors as input, and outputs the correct value, in ohms.
13+
The color bands are encoded as follows:
14+
15+
- black: 0
16+
- brown: 1
17+
- red: 2
18+
- orange: 3
19+
- yellow: 4
20+
- green: 5
21+
- blue: 6
22+
- violet: 7
23+
- grey: 8
24+
- white: 9
25+
26+
In Resistor Color Duo you decoded the first two colors.
27+
For instance: orange-orange got the main value `33`.
28+
The third color stands for how many zeros need to be added to the main value.
29+
The main value plus the zeros gives us a value in ohms.
30+
For the exercise it doesn't matter what ohms really are.
31+
For example:
32+
33+
- orange-orange-black would be 33 and no zeros, which becomes 33 ohms.
34+
- orange-orange-red would be 33 and 2 zeros, which becomes 3300 ohms.
35+
- orange-orange-orange would be 33 and 3 zeros, which becomes 33000 ohms.
36+
37+
(If Math is your thing, you may want to think of the zeros as exponents of 10.
38+
If Math is not your thing, go with the zeros.
39+
It really is the same thing, just in plain English instead of Math lingo.)
40+
41+
This exercise is about translating the colors into a label:
42+
43+
> "... ohms"
44+
45+
So an input of `"orange", "orange", "black"` should return:
46+
47+
> "33 ohms"
48+
49+
When we get to larger resistors, a [metric prefix][metric-prefix] is used to indicate a larger magnitude of ohms, such as "kiloohms".
50+
That is similar to saying "2 kilometers" instead of "2000 meters", or "2 kilograms" for "2000 grams".
51+
52+
For example, an input of `"orange", "orange", "orange"` should return:
53+
54+
> "33 kiloohms"
55+
56+
[metric-prefix]: https://en.wikipedia.org/wiki/Metric_prefix
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_trio.clas.abap"
8+
],
9+
"test": [
10+
"zcl_resistor_color_trio.clas.testclasses.abap"
11+
],
12+
"example": [
13+
".meta/zcl_resistor_color_trio.clas.abap"
14+
]
15+
},
16+
"blurb": "Convert color codes, as used on resistors, to a human-readable label.",
17+
"source": "Maud de Vries, Erik Schierboom",
18+
"source_url": "https://github.com/exercism/problem-specifications/issues/1549"
19+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
[d6863355-15b7-40bb-abe0-bfb1a25512ed]
13+
description = "Orange and orange and black"
14+
15+
[1224a3a9-8c8e-4032-843a-5224e04647d6]
16+
description = "Blue and grey and brown"
17+
18+
[b8bda7dc-6b95-4539-abb2-2ad51d66a207]
19+
description = "Red and black and red"
20+
21+
[5b1e74bc-d838-4eda-bbb3-eaba988e733b]
22+
description = "Green and brown and orange"
23+
24+
[f5d37ef9-1919-4719-a90d-a33c5a6934c9]
25+
description = "Yellow and violet and yellow"
26+
27+
[5f6404a7-5bb3-4283-877d-3d39bcc33854]
28+
description = "Blue and violet and blue"
29+
30+
[7d3a6ab8-e40e-46c3-98b1-91639fff2344]
31+
description = "Minimum possible value"
32+
33+
[ca0aa0ac-3825-42de-9f07-dac68cc580fd]
34+
description = "Maximum possible value"
35+
36+
[0061a76c-903a-4714-8ce2-f26ce23b0e09]
37+
description = "First two colors make an invalid octal number"
38+
39+
[30872c92-f567-4b69-a105-8455611c10c4]
40+
description = "Ignore extra colors"
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
CLASS zcl_resistor_color_trio DEFINITION
2+
PUBLIC
3+
FINAL
4+
CREATE PUBLIC.
5+
6+
PUBLIC SECTION.
7+
METHODS label
8+
IMPORTING
9+
colors TYPE string_table
10+
RETURNING
11+
VALUE(result) TYPE string.
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_trio IMPLEMENTATION.
22+
METHOD label.
23+
DATA(color1) = colors[ 1 ].
24+
DATA(color2) = colors[ 2 ].
25+
DATA(color3) = colors[ 3 ].
26+
27+
DATA(base_value) = color_code( color1 ) * 10 + color_code( color2 ).
28+
DATA(zeros) = color_code( color3 ).
29+
30+
DATA(formatted_value) = base_value * ( 10 ** zeros ).
31+
DATA(unit) = CONV string( 'ohms' ).
32+
33+
IF formatted_value >= 1000000000.
34+
formatted_value = formatted_value / 1000000000.
35+
unit = 'gigaohms'.
36+
ELSEIF formatted_value >= 1000000.
37+
formatted_value = formatted_value / 1000000.
38+
unit = 'megaohms'.
39+
ELSEIF formatted_value >= 1000.
40+
formatted_value = formatted_value / 1000.
41+
unit = 'kiloohms'.
42+
ENDIF.
43+
44+
result = |{ formatted_value } { unit }|.
45+
ENDMETHOD.
46+
47+
METHOD color_code.
48+
CASE color.
49+
WHEN 'black'.
50+
result = 0.
51+
WHEN 'brown'.
52+
result = 1.
53+
WHEN 'red'.
54+
result = 2.
55+
WHEN 'orange'.
56+
result = 3.
57+
WHEN 'yellow'.
58+
result = 4.
59+
WHEN 'green'.
60+
result = 5.
61+
WHEN 'blue'.
62+
result = 6.
63+
WHEN 'violet'.
64+
result = 7.
65+
WHEN 'grey'.
66+
result = 8.
67+
WHEN 'white'.
68+
result = 9.
69+
ENDCASE.
70+
ENDMETHOD.
71+
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 Trio</CTEXT>
7+
</DEVC>
8+
</asx:values>
9+
</asx:abap>
10+
</abapGit>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
CLASS zcl_resistor_color_trio DEFINITION
2+
PUBLIC
3+
FINAL
4+
CREATE PUBLIC.
5+
6+
PUBLIC SECTION.
7+
METHODS label
8+
IMPORTING
9+
colors TYPE string_table
10+
RETURNING
11+
VALUE(result) TYPE string.
12+
PROTECTED SECTION.
13+
PRIVATE SECTION.
14+
15+
ENDCLASS.
16+
17+
CLASS zcl_resistor_color_trio IMPLEMENTATION.
18+
METHOD label.
19+
"Implement solution
20+
ENDMETHOD.
21+
ENDCLASS.
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
*"* use this source file for your ABAP unit test classes
2+
CLASS ltcl_resistor_color_trio DEFINITION FINAL FOR TESTING
3+
DURATION SHORT
4+
RISK LEVEL HARMLESS.
5+
6+
PRIVATE SECTION.
7+
DATA cut TYPE REF TO zcl_resistor_color_trio.
8+
METHODS setup.
9+
METHODS:
10+
test_orange_and_orange_black FOR TESTING,
11+
test_blue_and_grey_brown FOR TESTING,
12+
test_red_and_black_red FOR TESTING,
13+
test_green_and_brown_orange FOR TESTING,
14+
test_yellow_and_violet_yellow FOR TESTING,
15+
test_blue_and_violet_blue FOR TESTING,
16+
test_minimum_possible_value FOR TESTING,
17+
test_maximum_possible_value FOR TESTING,
18+
test_invalid_octal FOR TESTING,
19+
test_ignore_extra_colors FOR TESTING.
20+
ENDCLASS.
21+
22+
CLASS ltcl_resistor_color_trio IMPLEMENTATION.
23+
24+
METHOD setup.
25+
cut = NEW zcl_resistor_color_trio( ).
26+
ENDMETHOD.
27+
28+
METHOD test_orange_and_orange_black.
29+
DATA(input_colors) = VALUE string_table( ( `orange` ) ( `orange` ) ( `black` ) ).
30+
cl_abap_unit_assert=>assert_equals(
31+
act = cut->label( input_colors )
32+
exp = '33 ohms' ).
33+
ENDMETHOD.
34+
35+
METHOD test_blue_and_grey_brown.
36+
DATA(input_colors) = VALUE string_table( ( `blue` ) ( `grey` ) ( `brown` ) ).
37+
cl_abap_unit_assert=>assert_equals(
38+
act = cut->label( input_colors )
39+
exp = '680 ohms' ).
40+
ENDMETHOD.
41+
42+
METHOD test_red_and_black_red.
43+
DATA(input_colors) = VALUE string_table( ( `red` ) ( `black` ) ( `red` ) ).
44+
cl_abap_unit_assert=>assert_equals(
45+
act = cut->label( input_colors )
46+
exp = '2 kiloohms' ).
47+
ENDMETHOD.
48+
49+
METHOD test_green_and_brown_orange.
50+
DATA(input_colors) = VALUE string_table( ( `green` ) ( `brown` ) ( `orange` ) ).
51+
cl_abap_unit_assert=>assert_equals(
52+
act = cut->label( input_colors )
53+
exp = '51 kiloohms' ).
54+
ENDMETHOD.
55+
56+
METHOD test_yellow_and_violet_yellow.
57+
DATA(input_colors) = VALUE string_table( ( `yellow` ) ( `violet` ) ( `yellow` ) ).
58+
cl_abap_unit_assert=>assert_equals(
59+
act = cut->label( input_colors )
60+
exp = '470 kiloohms' ).
61+
ENDMETHOD.
62+
63+
METHOD test_blue_and_violet_blue.
64+
DATA(input_colors) = VALUE string_table( ( `blue` ) ( `violet` ) ( `blue` ) ).
65+
cl_abap_unit_assert=>assert_equals(
66+
act = cut->label( input_colors )
67+
exp = '67 megaohms' ).
68+
ENDMETHOD.
69+
70+
METHOD test_minimum_possible_value.
71+
DATA(input_colors) = VALUE string_table( ( `black` ) ( `black` ) ( `black` ) ).
72+
cl_abap_unit_assert=>assert_equals(
73+
act = cut->label( input_colors )
74+
exp = '0 ohms' ).
75+
ENDMETHOD.
76+
77+
METHOD test_maximum_possible_value.
78+
DATA(input_colors) = VALUE string_table( ( `white` ) ( `white` ) ( `white` ) ).
79+
cl_abap_unit_assert=>assert_equals(
80+
act = cut->label( input_colors )
81+
exp = '99 gigaohms' ).
82+
ENDMETHOD.
83+
84+
METHOD test_invalid_octal.
85+
DATA(input_colors) = VALUE string_table( ( `black` ) ( `grey` ) ( `black` ) ).
86+
cl_abap_unit_assert=>assert_equals(
87+
act = cut->label( input_colors )
88+
exp = '8 ohms' ).
89+
ENDMETHOD.
90+
91+
METHOD test_ignore_extra_colors.
92+
DATA(input_colors) = VALUE string_table( ( `blue` ) ( `green` ) ( `yellow` ) ( `orange` ) ).
93+
cl_abap_unit_assert=>assert_equals(
94+
act = cut->label( input_colors )
95+
exp = '650 kiloohms' ).
96+
ENDMETHOD.
97+
98+
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_TRIO</CLSNAME>
7+
<LANGU>E</LANGU>
8+
<DESCRIPT>Exercism: Resistor Color Trio</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)