Implement Exercise "Secret Handshake" (#213)

This commit is contained in:
bau-mann
2022-09-30 19:24:12 +02:00
committed by GitHub
parent d9c08f49ed
commit 7f974458e2
8 changed files with 257 additions and 0 deletions

View File

@@ -272,6 +272,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 1
},
{
"slug": "secret-handshake",
"name": "Secret Handshake",
"uuid": "bc263681-aabb-4171-8b0d-acfb7358e2ab",
"practices": [],
"prerequisites": [],
"difficulty": 4
}
]
},

View File

@@ -0,0 +1,37 @@
# Description
> There are 10 types of people in the world: Those who understand
> binary, and those who don't.
You and your fellow cohort of those in the "know" when it comes to
binary decide to come up with a secret "handshake".
```text
00001 = wink
00010 = double blink
00100 = close your eyes
01000 = jump
10000 = Reverse the order of the operations in the secret handshake.
```
Given a decimal number, convert it to the appropriate sequence of events for a secret handshake.
Here's a couple of examples:
Given the decimal input 3, the function would return the following string_table:
| Row | TABLE_LINE |
| --- | ------------ |
| 1 | wink |
| 2 | double blink |
This is because the decimal number 3 is 2+1 in powers of two and thus `11` in binary.
Let's now examine the input 19 which is 16+2+1 in powers of two and thus `10011` in binary.
Recalling that the addition of 16 (`10000` in binary) reverses the sequence and that we already know what result is returned given input 3, the string_table returned for input 19 is:
| Row | TABLE_LINE |
| --- | ------------ |
| 1 | double blink |
| 2 | wink |

View File

@@ -0,0 +1,20 @@
{
"blurb": "Given a decimal number, convert it to the appropriate sequence of events for a secret handshake.",
"authors": [
"bau-mann"
],
"contributors": [],
"files": {
"solution": [
"zcl_secret_handshake.clas.abap"
],
"test": [
"zcl_secret_handshake.clas.testclasses.abap"
],
"example": [
".meta/zcl_secret_handshake.clas.abap"
]
},
"source": "Bert, in Mary Poppins",
"source_url": "http://www.imdb.com/title/tt0058331/quotes/qt0437047"
}

View File

@@ -0,0 +1,40 @@
CLASS zcl_secret_handshake DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
METHODS get_commands
IMPORTING code TYPE i
RETURNING VALUE(commands) TYPE string_table.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS zcl_secret_handshake IMPLEMENTATION.
METHOD get_commands.
DATA binary_zero TYPE x VALUE 0.
DATA(binary_code) = CONV xstring( code ).
DATA(codes_transl) = VALUE string_table( ( `wink` ) ( `double blink` ) ( `close your eyes` ) ( `jump` ) ).
LOOP AT codes_transl REFERENCE INTO DATA(command).
IF CONV xstring( 2 ** ( sy-tabix - 1 ) ) BIT-AND binary_code > binary_zero.
APPEND command->* TO commands.
ENDIF.
ENDLOOP.
IF binary_code BIT-AND CONV xstring( 16 ) > binary_zero.
DATA(reversed_commands) = VALUE string_table( FOR line IN commands
INDEX INTO i
( commands[ lines( commands ) - i + 1 ] ) ).
commands = reversed_commands.
ENDIF.
ENDMETHOD.
ENDCLASS.

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<abapGit version="v1.0.0" serializer="LCL_OBJECT_DEVC" serializer_version="v1.0.0">
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
<asx:values>
<DEVC>
<CTEXT>Exercism: Secret Handshake</CTEXT>
</DEVC>
</asx:values>
</asx:abap>
</abapGit>

View File

@@ -0,0 +1,22 @@
CLASS zcl_secret_handshake DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
METHODS get_commands
IMPORTING code TYPE i
RETURNING VALUE(commands) TYPE string_table.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS zcl_secret_handshake IMPLEMENTATION.
METHOD get_commands.
" add solution here
ENDMETHOD.
ENDCLASS.

View File

@@ -0,0 +1,103 @@
CLASS ltcl_secret_handshake DEFINITION FINAL FOR TESTING
DURATION SHORT
RISK LEVEL HARMLESS.
PRIVATE SECTION.
DATA cut TYPE REF TO zcl_secret_handshake.
METHODS setup.
"! wink for 0b1
METHODS wink_for_1 FOR TESTING RAISING cx_static_check.
"! double blink for 0b10
METHODS double_blink_for_10 FOR TESTING RAISING cx_static_check.
"! close your eyes for 0b100
METHODS close_your_eyes_for_100 FOR TESTING RAISING cx_static_check.
"! jump for 0b1000
METHODS jump_for_1000 FOR TESTING RAISING cx_static_check.
METHODS combine_two_actions FOR TESTING RAISING cx_static_check.
METHODS reverse_two_actions FOR TESTING RAISING cx_static_check.
"! reversing one action gives the same action
METHODS reverse_one_actions FOR TESTING RAISING cx_static_check.
"! reversing no actions still gives no actions
METHODS reverse_no_action FOR TESTING RAISING cx_static_check.
METHODS all_possible_actions FOR TESTING RAISING cx_static_check.
METHODS reverse_all_possible_actions FOR TESTING RAISING cx_static_check.
METHODS do_nothing_for_zero FOR TESTING RAISING cx_static_check.
ENDCLASS.
CLASS ltcl_secret_handshake IMPLEMENTATION.
METHOD setup.
cut = NEW #( ).
ENDMETHOD.
METHOD wink_for_1.
cl_abap_unit_assert=>assert_equals(
exp = VALUE string_table( ( `wink` ) )
act = cut->get_commands( 1 ) ).
ENDMETHOD.
METHOD double_blink_for_10.
cl_abap_unit_assert=>assert_equals(
exp = VALUE string_table( ( `double blink` ) )
act = cut->get_commands( 2 ) ).
ENDMETHOD.
METHOD close_your_eyes_for_100.
cl_abap_unit_assert=>assert_equals(
exp = VALUE string_table( ( `close your eyes` ) )
act = cut->get_commands( 4 ) ).
ENDMETHOD.
METHOD jump_for_1000.
cl_abap_unit_assert=>assert_equals(
exp = VALUE string_table( ( `jump` ) )
act = cut->get_commands( 8 ) ).
ENDMETHOD.
METHOD combine_two_actions.
cl_abap_unit_assert=>assert_equals(
exp = VALUE string_table( ( `wink` ) ( `double blink` ) )
act = cut->get_commands( 3 ) ).
ENDMETHOD.
METHOD reverse_two_actions.
cl_abap_unit_assert=>assert_equals(
exp = VALUE string_table( ( `double blink` ) ( `wink` ) )
act = cut->get_commands( 19 ) ).
ENDMETHOD.
METHOD reverse_one_actions.
cl_abap_unit_assert=>assert_equals(
exp = VALUE string_table( ( `jump` ) )
act = cut->get_commands( 24 ) ).
ENDMETHOD.
METHOD reverse_no_action.
cl_abap_unit_assert=>assert_equals(
exp = VALUE string_table( )
act = cut->get_commands( 16 ) ).
ENDMETHOD.
METHOD all_possible_actions.
cl_abap_unit_assert=>assert_equals(
exp = VALUE string_table( ( `wink` ) ( `double blink` ) ( `close your eyes` ) ( `jump` ) )
act = cut->get_commands( 15 ) ).
ENDMETHOD.
METHOD reverse_all_possible_actions.
cl_abap_unit_assert=>assert_equals(
exp = VALUE string_table( ( `jump` ) ( `close your eyes` ) ( `double blink` ) ( `wink` ) )
act = cut->get_commands( 31 ) ).
ENDMETHOD.
METHOD do_nothing_for_zero.
cl_abap_unit_assert=>assert_equals(
exp = VALUE string_table( )
act = cut->get_commands( 0 ) ).
ENDMETHOD.
ENDCLASS.

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<abapGit version="v1.0.0" serializer="LCL_OBJECT_CLAS" serializer_version="v1.0.0">
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
<asx:values>
<VSEOCLASS>
<CLSNAME>ZCL_SECRET_HANDSHAKE</CLSNAME>
<LANGU>E</LANGU>
<DESCRIPT>Exercism: Secret Handshake</DESCRIPT>
<STATE>1</STATE>
<CLSCCINCL>X</CLSCCINCL>
<FIXPT>X</FIXPT>
<UNICODE>X</UNICODE>
<WITH_UNIT_TESTS>X</WITH_UNIT_TESTS>
</VSEOCLASS>
</asx:values>
</asx:abap>
</abapGit>