Add hamming exercise (#23)
* add stubs * add exercise to config * rename method name * add exception * add solution and test cases * remove exception type, assert instead of throw * disable no_yoda_conditions
This commit is contained in:
@@ -365,11 +365,6 @@
|
||||
"severity": "Error",
|
||||
"allowReadOnly": false
|
||||
},
|
||||
"no_yoda_conditions": {
|
||||
"exclude": [],
|
||||
"severity": "Error",
|
||||
"onlyConstants": false
|
||||
},
|
||||
"object_naming": {
|
||||
"exclude": [],
|
||||
"severity": "Error",
|
||||
|
||||
@@ -365,11 +365,6 @@
|
||||
"severity": "Error",
|
||||
"allowReadOnly": false
|
||||
},
|
||||
"no_yoda_conditions": {
|
||||
"exclude": [],
|
||||
"severity": "Error",
|
||||
"onlyConstants": false
|
||||
},
|
||||
"object_naming": {
|
||||
"exclude": [],
|
||||
"severity": "Error",
|
||||
|
||||
@@ -68,6 +68,14 @@
|
||||
"practices": [],
|
||||
"prerequisites": [],
|
||||
"difficulty": 1
|
||||
},
|
||||
{
|
||||
"slug": "hamming",
|
||||
"name": "Hamming",
|
||||
"uuid": "4f44d8d4-5822-4cdd-8b5b-2a806a2b9ce0",
|
||||
"practices": [],
|
||||
"prerequisites": [],
|
||||
"difficulty": 1
|
||||
}
|
||||
]
|
||||
},
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
{
|
||||
"blurb": "Clock",
|
||||
"authors": ["mbtools"],
|
||||
"contributors": [],
|
||||
"files": {
|
||||
"solution": ["zcl_clock.clas.abap"],
|
||||
"test": ["zcl_clock.clas.testclasses.abap"],
|
||||
"example": [".meta/zcl_clock.clas.abap"]
|
||||
}
|
||||
{
|
||||
"blurb": "Clock",
|
||||
"authors": ["mbtools"],
|
||||
"contributors": [],
|
||||
"files": {
|
||||
"solution": ["zcl_clock.clas.abap"],
|
||||
"test": ["zcl_clock.clas.testclasses.abap"],
|
||||
"example": [".meta/zcl_clock.clas.abap"]
|
||||
}
|
||||
}
|
||||
@@ -1,54 +1,54 @@
|
||||
CLASS zcl_clock DEFINITION
|
||||
PUBLIC
|
||||
CREATE PUBLIC.
|
||||
|
||||
PUBLIC SECTION.
|
||||
|
||||
METHODS constructor
|
||||
IMPORTING
|
||||
!hours TYPE i
|
||||
!minutes TYPE i DEFAULT 0.
|
||||
METHODS get
|
||||
RETURNING
|
||||
VALUE(result) TYPE string.
|
||||
METHODS add
|
||||
IMPORTING
|
||||
!minutes TYPE i.
|
||||
METHODS sub
|
||||
IMPORTING
|
||||
!minutes TYPE i.
|
||||
|
||||
PRIVATE SECTION.
|
||||
|
||||
DATA clock TYPE t.
|
||||
|
||||
ENDCLASS.
|
||||
|
||||
|
||||
|
||||
CLASS zcl_clock IMPLEMENTATION.
|
||||
|
||||
|
||||
METHOD add.
|
||||
clock = clock + minutes * 60.
|
||||
ENDMETHOD.
|
||||
|
||||
|
||||
METHOD constructor.
|
||||
" Clock is number of seconds
|
||||
clock = hours * 60 * 60 + minutes * 60.
|
||||
ENDMETHOD.
|
||||
|
||||
|
||||
METHOD get.
|
||||
" hh:mm:ss
|
||||
result = |{ clock TIME = ISO }|.
|
||||
" return only hh:mm
|
||||
result = result(5).
|
||||
ENDMETHOD.
|
||||
|
||||
|
||||
METHOD sub.
|
||||
clock = clock - minutes * 60.
|
||||
ENDMETHOD.
|
||||
ENDCLASS.
|
||||
CLASS zcl_clock DEFINITION
|
||||
PUBLIC
|
||||
CREATE PUBLIC.
|
||||
|
||||
PUBLIC SECTION.
|
||||
|
||||
METHODS constructor
|
||||
IMPORTING
|
||||
!hours TYPE i
|
||||
!minutes TYPE i DEFAULT 0.
|
||||
METHODS get
|
||||
RETURNING
|
||||
VALUE(result) TYPE string.
|
||||
METHODS add
|
||||
IMPORTING
|
||||
!minutes TYPE i.
|
||||
METHODS sub
|
||||
IMPORTING
|
||||
!minutes TYPE i.
|
||||
|
||||
PRIVATE SECTION.
|
||||
|
||||
DATA clock TYPE t.
|
||||
|
||||
ENDCLASS.
|
||||
|
||||
|
||||
|
||||
CLASS zcl_clock IMPLEMENTATION.
|
||||
|
||||
|
||||
METHOD add.
|
||||
clock = clock + minutes * 60.
|
||||
ENDMETHOD.
|
||||
|
||||
|
||||
METHOD constructor.
|
||||
" Clock is number of seconds
|
||||
clock = hours * 60 * 60 + minutes * 60.
|
||||
ENDMETHOD.
|
||||
|
||||
|
||||
METHOD get.
|
||||
" hh:mm:ss
|
||||
result = |{ clock TIME = ISO }|.
|
||||
" return only hh:mm
|
||||
result = result(5).
|
||||
ENDMETHOD.
|
||||
|
||||
|
||||
METHOD sub.
|
||||
clock = clock - minutes * 60.
|
||||
ENDMETHOD.
|
||||
ENDCLASS.
|
||||
|
||||
@@ -1,49 +1,49 @@
|
||||
CLASS zcl_clock DEFINITION
|
||||
PUBLIC
|
||||
CREATE PUBLIC.
|
||||
|
||||
PUBLIC SECTION.
|
||||
|
||||
METHODS constructor
|
||||
IMPORTING
|
||||
!hours TYPE i
|
||||
!minutes TYPE i DEFAULT 0.
|
||||
METHODS get
|
||||
RETURNING
|
||||
VALUE(result) TYPE string.
|
||||
METHODS add
|
||||
IMPORTING
|
||||
!minutes TYPE i.
|
||||
METHODS sub
|
||||
IMPORTING
|
||||
!minutes TYPE i.
|
||||
|
||||
PRIVATE SECTION.
|
||||
|
||||
* add solution here
|
||||
|
||||
ENDCLASS.
|
||||
|
||||
|
||||
|
||||
CLASS zcl_clock IMPLEMENTATION.
|
||||
|
||||
METHOD add.
|
||||
* add solution here
|
||||
ENDMETHOD.
|
||||
|
||||
|
||||
METHOD constructor.
|
||||
* add solution here
|
||||
ENDMETHOD.
|
||||
|
||||
|
||||
METHOD get.
|
||||
* add solution here
|
||||
ENDMETHOD.
|
||||
|
||||
|
||||
METHOD sub.
|
||||
* add solution here
|
||||
ENDMETHOD.
|
||||
ENDCLASS.
|
||||
CLASS zcl_clock DEFINITION
|
||||
PUBLIC
|
||||
CREATE PUBLIC.
|
||||
|
||||
PUBLIC SECTION.
|
||||
|
||||
METHODS constructor
|
||||
IMPORTING
|
||||
!hours TYPE i
|
||||
!minutes TYPE i DEFAULT 0.
|
||||
METHODS get
|
||||
RETURNING
|
||||
VALUE(result) TYPE string.
|
||||
METHODS add
|
||||
IMPORTING
|
||||
!minutes TYPE i.
|
||||
METHODS sub
|
||||
IMPORTING
|
||||
!minutes TYPE i.
|
||||
|
||||
PRIVATE SECTION.
|
||||
|
||||
* add solution here
|
||||
|
||||
ENDCLASS.
|
||||
|
||||
|
||||
|
||||
CLASS zcl_clock IMPLEMENTATION.
|
||||
|
||||
METHOD add.
|
||||
* add solution here
|
||||
ENDMETHOD.
|
||||
|
||||
|
||||
METHOD constructor.
|
||||
* add solution here
|
||||
ENDMETHOD.
|
||||
|
||||
|
||||
METHOD get.
|
||||
* add solution here
|
||||
ENDMETHOD.
|
||||
|
||||
|
||||
METHOD sub.
|
||||
* add solution here
|
||||
ENDMETHOD.
|
||||
ENDCLASS.
|
||||
|
||||
@@ -1,303 +1,303 @@
|
||||
CLASS ltcl_clock DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATION SHORT FINAL.
|
||||
|
||||
PRIVATE SECTION.
|
||||
DATA cut TYPE REF TO zcl_clock.
|
||||
|
||||
METHODS:
|
||||
on_the_hour FOR TESTING,
|
||||
past_the_hour FOR TESTING,
|
||||
adding_a_few_minutes FOR TESTING,
|
||||
adding_zero_minutes FOR TESTING,
|
||||
adding_over_an_hour FOR TESTING,
|
||||
adding_more_than_two_hours_w_c FOR TESTING,
|
||||
adding_more_than_two_days FOR TESTING,
|
||||
wrap_around_at_midnight FOR TESTING,
|
||||
subtract_minutes FOR TESTING,
|
||||
subtract_more_than_two_hours FOR TESTING,
|
||||
subtract_more_than_two_hours_w FOR TESTING,
|
||||
subtract_more_than_two_days FOR TESTING,
|
||||
wrap_around_backwards FOR TESTING,
|
||||
wrap_around_day FOR TESTING,
|
||||
wrap_around_day_backwards FOR TESTING,
|
||||
equivalent_clocks FOR TESTING,
|
||||
inequivalent_clocks FOR TESTING,
|
||||
equivalent_clocks_1 FOR TESTING,
|
||||
equivalent_clocks_2 FOR TESTING,
|
||||
equivalent_clocks_3 FOR TESTING,
|
||||
equivalent_clocks_4 FOR TESTING,
|
||||
equivalent_clocks_5 FOR TESTING,
|
||||
equivalent_clocks_6 FOR TESTING,
|
||||
hours_rollover FOR TESTING,
|
||||
minutes_rollover FOR TESTING,
|
||||
hours_and_minutes_rollover FOR TESTING,
|
||||
negative_hours_rollover FOR TESTING,
|
||||
negative_minutes_rollover FOR TESTING,
|
||||
negative_hours_and_minutes FOR TESTING.
|
||||
|
||||
ENDCLASS.
|
||||
|
||||
CLASS ltcl_clock IMPLEMENTATION.
|
||||
|
||||
METHOD on_the_hour.
|
||||
cut = NEW zcl_clock( 8 ).
|
||||
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = '08:00'
|
||||
act = cut->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD past_the_hour.
|
||||
cut = NEW zcl_clock( hours = 11
|
||||
minutes = 9 ).
|
||||
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = '11:09'
|
||||
act = cut->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD adding_a_few_minutes.
|
||||
cut = NEW zcl_clock( 10 ).
|
||||
|
||||
cut->add( 3 ).
|
||||
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = '10:03'
|
||||
act = cut->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD adding_zero_minutes.
|
||||
cut = NEW zcl_clock( hours = 6
|
||||
minutes = 41 ).
|
||||
|
||||
cut->add( 0 ).
|
||||
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = '06:41'
|
||||
act = cut->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD adding_over_an_hour.
|
||||
cut = NEW zcl_clock( 10 ).
|
||||
|
||||
cut->add( 61 ).
|
||||
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = '11:01'
|
||||
act = cut->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD adding_more_than_two_hours_w_c.
|
||||
cut = NEW zcl_clock( hours = 0
|
||||
minutes = 45 ).
|
||||
|
||||
cut->add( 160 ).
|
||||
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = '03:25'
|
||||
act = cut->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD adding_more_than_two_days.
|
||||
cut = NEW zcl_clock( hours = 1
|
||||
minutes = 1 ).
|
||||
|
||||
cut->add( 3500 ).
|
||||
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = '11:21'
|
||||
act = cut->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD wrap_around_at_midnight.
|
||||
cut = NEW zcl_clock( hours = 23
|
||||
minutes = 30 ).
|
||||
|
||||
cut->add( 60 ).
|
||||
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = '00:30'
|
||||
act = cut->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD subtract_minutes.
|
||||
cut = NEW zcl_clock( 10 ).
|
||||
|
||||
cut->sub( 90 ).
|
||||
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = '08:30'
|
||||
act = cut->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD subtract_more_than_two_hours.
|
||||
cut = NEW zcl_clock( hours = 6
|
||||
minutes = 15 ).
|
||||
|
||||
cut->sub( 160 ).
|
||||
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = '03:35'
|
||||
act = cut->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD subtract_more_than_two_hours_w.
|
||||
cut = NEW zcl_clock( hours = 6
|
||||
minutes = 15 ).
|
||||
|
||||
cut->add( -160 ).
|
||||
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = '03:35'
|
||||
act = cut->get( ) ).
|
||||
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD subtract_more_than_two_days.
|
||||
cut = NEW zcl_clock( hours = 2
|
||||
minutes = 20 ).
|
||||
|
||||
cut->sub( 3000 ).
|
||||
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = '00:20'
|
||||
act = cut->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD wrap_around_backwards.
|
||||
cut = NEW zcl_clock( hours = 0
|
||||
minutes = 30 ).
|
||||
|
||||
cut->sub( 60 ).
|
||||
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = '23:30'
|
||||
act = cut->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD wrap_around_day.
|
||||
cut = NEW zcl_clock( hours = 5
|
||||
minutes = 32 ).
|
||||
|
||||
cut->add( 25 * 60 ).
|
||||
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = '06:32'
|
||||
act = cut->get( ) ).
|
||||
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD wrap_around_day_backwards.
|
||||
cut = NEW zcl_clock( hours = 5
|
||||
minutes = 32 ).
|
||||
|
||||
cut->sub( 25 * 60 ).
|
||||
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = '04:32'
|
||||
act = cut->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD equivalent_clocks.
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = NEW zcl_clock( hours = 15
|
||||
minutes = 37 )->get( )
|
||||
act = NEW zcl_clock( hours = 15
|
||||
minutes = 37 )->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD inequivalent_clocks.
|
||||
cl_abap_unit_assert=>assert_differs(
|
||||
exp = NEW zcl_clock( hours = 1
|
||||
minutes = 1 )->get( )
|
||||
act = NEW zcl_clock( hours = 18
|
||||
minutes = 32 )->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD equivalent_clocks_1.
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = NEW zcl_clock( hours = 3
|
||||
minutes = 11 )->get( )
|
||||
act = NEW zcl_clock( hours = 99
|
||||
minutes = 11 )->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD equivalent_clocks_2.
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = NEW zcl_clock( hours = 22
|
||||
minutes = 40 )->get( )
|
||||
act = NEW zcl_clock( hours = -2
|
||||
minutes = 40 )->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD equivalent_clocks_3.
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = NEW zcl_clock( hours = 17
|
||||
minutes = 3 )->get( )
|
||||
act = NEW zcl_clock( hours = -31
|
||||
minutes = 3 )->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD equivalent_clocks_4.
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = NEW zcl_clock( hours = 2
|
||||
minutes = 2 )->get( )
|
||||
act = NEW zcl_clock( hours = 2
|
||||
minutes = 4322 )->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD equivalent_clocks_5.
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = NEW zcl_clock( hours = 2
|
||||
minutes = 40 )->get( )
|
||||
act = NEW zcl_clock( hours = 3
|
||||
minutes = -20 )->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD equivalent_clocks_6.
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = NEW zcl_clock( hours = 7
|
||||
minutes = 32 )->get( )
|
||||
act = NEW zcl_clock( hours = -12
|
||||
minutes = -268 )->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD hours_rollover.
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = '04:00'
|
||||
act = NEW zcl_clock( 100 )->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD minutes_rollover.
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = '04:43'
|
||||
act = NEW zcl_clock( hours = 0
|
||||
minutes = 1723 )->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD hours_and_minutes_rollover.
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = '00:00'
|
||||
act = NEW zcl_clock( hours = 72
|
||||
minutes = 8640 )->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD negative_hours_rollover.
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = '05:00'
|
||||
act = NEW zcl_clock( -91 )->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD negative_minutes_rollover.
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = '16:40'
|
||||
act = NEW zcl_clock( hours = 1
|
||||
minutes = -4820 )->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD negative_hours_and_minutes.
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = '22:10'
|
||||
act = NEW zcl_clock( hours = -121
|
||||
minutes = -5810 )->get( ) ).
|
||||
ENDMETHOD.
|
||||
ENDCLASS.
|
||||
CLASS ltcl_clock DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATION SHORT FINAL.
|
||||
|
||||
PRIVATE SECTION.
|
||||
DATA cut TYPE REF TO zcl_clock.
|
||||
|
||||
METHODS:
|
||||
on_the_hour FOR TESTING,
|
||||
past_the_hour FOR TESTING,
|
||||
adding_a_few_minutes FOR TESTING,
|
||||
adding_zero_minutes FOR TESTING,
|
||||
adding_over_an_hour FOR TESTING,
|
||||
adding_more_than_two_hours_w_c FOR TESTING,
|
||||
adding_more_than_two_days FOR TESTING,
|
||||
wrap_around_at_midnight FOR TESTING,
|
||||
subtract_minutes FOR TESTING,
|
||||
subtract_more_than_two_hours FOR TESTING,
|
||||
subtract_more_than_two_hours_w FOR TESTING,
|
||||
subtract_more_than_two_days FOR TESTING,
|
||||
wrap_around_backwards FOR TESTING,
|
||||
wrap_around_day FOR TESTING,
|
||||
wrap_around_day_backwards FOR TESTING,
|
||||
equivalent_clocks FOR TESTING,
|
||||
inequivalent_clocks FOR TESTING,
|
||||
equivalent_clocks_1 FOR TESTING,
|
||||
equivalent_clocks_2 FOR TESTING,
|
||||
equivalent_clocks_3 FOR TESTING,
|
||||
equivalent_clocks_4 FOR TESTING,
|
||||
equivalent_clocks_5 FOR TESTING,
|
||||
equivalent_clocks_6 FOR TESTING,
|
||||
hours_rollover FOR TESTING,
|
||||
minutes_rollover FOR TESTING,
|
||||
hours_and_minutes_rollover FOR TESTING,
|
||||
negative_hours_rollover FOR TESTING,
|
||||
negative_minutes_rollover FOR TESTING,
|
||||
negative_hours_and_minutes FOR TESTING.
|
||||
|
||||
ENDCLASS.
|
||||
|
||||
CLASS ltcl_clock IMPLEMENTATION.
|
||||
|
||||
METHOD on_the_hour.
|
||||
cut = NEW zcl_clock( 8 ).
|
||||
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = '08:00'
|
||||
act = cut->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD past_the_hour.
|
||||
cut = NEW zcl_clock( hours = 11
|
||||
minutes = 9 ).
|
||||
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = '11:09'
|
||||
act = cut->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD adding_a_few_minutes.
|
||||
cut = NEW zcl_clock( 10 ).
|
||||
|
||||
cut->add( 3 ).
|
||||
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = '10:03'
|
||||
act = cut->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD adding_zero_minutes.
|
||||
cut = NEW zcl_clock( hours = 6
|
||||
minutes = 41 ).
|
||||
|
||||
cut->add( 0 ).
|
||||
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = '06:41'
|
||||
act = cut->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD adding_over_an_hour.
|
||||
cut = NEW zcl_clock( 10 ).
|
||||
|
||||
cut->add( 61 ).
|
||||
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = '11:01'
|
||||
act = cut->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD adding_more_than_two_hours_w_c.
|
||||
cut = NEW zcl_clock( hours = 0
|
||||
minutes = 45 ).
|
||||
|
||||
cut->add( 160 ).
|
||||
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = '03:25'
|
||||
act = cut->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD adding_more_than_two_days.
|
||||
cut = NEW zcl_clock( hours = 1
|
||||
minutes = 1 ).
|
||||
|
||||
cut->add( 3500 ).
|
||||
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = '11:21'
|
||||
act = cut->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD wrap_around_at_midnight.
|
||||
cut = NEW zcl_clock( hours = 23
|
||||
minutes = 30 ).
|
||||
|
||||
cut->add( 60 ).
|
||||
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = '00:30'
|
||||
act = cut->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD subtract_minutes.
|
||||
cut = NEW zcl_clock( 10 ).
|
||||
|
||||
cut->sub( 90 ).
|
||||
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = '08:30'
|
||||
act = cut->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD subtract_more_than_two_hours.
|
||||
cut = NEW zcl_clock( hours = 6
|
||||
minutes = 15 ).
|
||||
|
||||
cut->sub( 160 ).
|
||||
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = '03:35'
|
||||
act = cut->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD subtract_more_than_two_hours_w.
|
||||
cut = NEW zcl_clock( hours = 6
|
||||
minutes = 15 ).
|
||||
|
||||
cut->add( -160 ).
|
||||
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = '03:35'
|
||||
act = cut->get( ) ).
|
||||
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD subtract_more_than_two_days.
|
||||
cut = NEW zcl_clock( hours = 2
|
||||
minutes = 20 ).
|
||||
|
||||
cut->sub( 3000 ).
|
||||
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = '00:20'
|
||||
act = cut->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD wrap_around_backwards.
|
||||
cut = NEW zcl_clock( hours = 0
|
||||
minutes = 30 ).
|
||||
|
||||
cut->sub( 60 ).
|
||||
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = '23:30'
|
||||
act = cut->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD wrap_around_day.
|
||||
cut = NEW zcl_clock( hours = 5
|
||||
minutes = 32 ).
|
||||
|
||||
cut->add( 25 * 60 ).
|
||||
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = '06:32'
|
||||
act = cut->get( ) ).
|
||||
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD wrap_around_day_backwards.
|
||||
cut = NEW zcl_clock( hours = 5
|
||||
minutes = 32 ).
|
||||
|
||||
cut->sub( 25 * 60 ).
|
||||
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = '04:32'
|
||||
act = cut->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD equivalent_clocks.
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = NEW zcl_clock( hours = 15
|
||||
minutes = 37 )->get( )
|
||||
act = NEW zcl_clock( hours = 15
|
||||
minutes = 37 )->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD inequivalent_clocks.
|
||||
cl_abap_unit_assert=>assert_differs(
|
||||
exp = NEW zcl_clock( hours = 1
|
||||
minutes = 1 )->get( )
|
||||
act = NEW zcl_clock( hours = 18
|
||||
minutes = 32 )->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD equivalent_clocks_1.
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = NEW zcl_clock( hours = 3
|
||||
minutes = 11 )->get( )
|
||||
act = NEW zcl_clock( hours = 99
|
||||
minutes = 11 )->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD equivalent_clocks_2.
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = NEW zcl_clock( hours = 22
|
||||
minutes = 40 )->get( )
|
||||
act = NEW zcl_clock( hours = -2
|
||||
minutes = 40 )->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD equivalent_clocks_3.
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = NEW zcl_clock( hours = 17
|
||||
minutes = 3 )->get( )
|
||||
act = NEW zcl_clock( hours = -31
|
||||
minutes = 3 )->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD equivalent_clocks_4.
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = NEW zcl_clock( hours = 2
|
||||
minutes = 2 )->get( )
|
||||
act = NEW zcl_clock( hours = 2
|
||||
minutes = 4322 )->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD equivalent_clocks_5.
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = NEW zcl_clock( hours = 2
|
||||
minutes = 40 )->get( )
|
||||
act = NEW zcl_clock( hours = 3
|
||||
minutes = -20 )->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD equivalent_clocks_6.
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = NEW zcl_clock( hours = 7
|
||||
minutes = 32 )->get( )
|
||||
act = NEW zcl_clock( hours = -12
|
||||
minutes = -268 )->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD hours_rollover.
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = '04:00'
|
||||
act = NEW zcl_clock( 100 )->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD minutes_rollover.
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = '04:43'
|
||||
act = NEW zcl_clock( hours = 0
|
||||
minutes = 1723 )->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD hours_and_minutes_rollover.
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = '00:00'
|
||||
act = NEW zcl_clock( hours = 72
|
||||
minutes = 8640 )->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD negative_hours_rollover.
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = '05:00'
|
||||
act = NEW zcl_clock( -91 )->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD negative_minutes_rollover.
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = '16:40'
|
||||
act = NEW zcl_clock( hours = 1
|
||||
minutes = -4820 )->get( ) ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD negative_hours_and_minutes.
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
exp = '22:10'
|
||||
act = NEW zcl_clock( hours = -121
|
||||
minutes = -5810 )->get( ) ).
|
||||
ENDMETHOD.
|
||||
ENDCLASS.
|
||||
|
||||
23
exercises/practice/hamming/.docs/instructions.md
Normal file
23
exercises/practice/hamming/.docs/instructions.md
Normal file
@@ -0,0 +1,23 @@
|
||||
# Description
|
||||
|
||||
Calculate the Hamming Distance between two DNA strands.
|
||||
|
||||
Your body is made up of cells that contain DNA. Those cells regularly wear out and need replacing, which they achieve by dividing into daughter cells. In fact, the average human body experiences about 10 quadrillion cell divisions in a lifetime!
|
||||
|
||||
When cells divide, their DNA replicates too. Sometimes during this process mistakes happen and single pieces of DNA get encoded with the incorrect information. If we compare two strands of DNA and count the differences between them we can see how many mistakes occurred. This is known as the "Hamming Distance".
|
||||
|
||||
We read DNA using the letters C,A,G and T. Two strands might look like this:
|
||||
|
||||
GAGCCTACTAACGGGAT
|
||||
CATCGTAATGACGGCCT
|
||||
^ ^ ^ ^ ^ ^^
|
||||
|
||||
They have 7 differences, and therefore the Hamming Distance is 7.
|
||||
|
||||
The Hamming Distance is useful for lots of things in science, not just biology, so it's a nice phrase to be familiar with :)
|
||||
|
||||
## Implementation notes
|
||||
|
||||
The Hamming distance is only defined for sequences of equal length, so
|
||||
an attempt to calculate it between sequences of different lengths should
|
||||
not work.
|
||||
13
exercises/practice/hamming/.meta/config.json
Normal file
13
exercises/practice/hamming/.meta/config.json
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"blurb": "Calculate the Hamming difference between two DNA strands.",
|
||||
"authors": ["g-back"],
|
||||
"contributors": [
|
||||
],
|
||||
"files": {
|
||||
"solution": ["zcl_hamming.clas.abap"],
|
||||
"test": ["zcl_hamming.clas.testclasses.abap"],
|
||||
"example": [".meta/zcl_hamming.clas.abap"]
|
||||
},
|
||||
"source": "The Calculating Point Mutations problem at Rosalind",
|
||||
"source_url": "http://rosalind.info/problems/hamm/"
|
||||
}
|
||||
27
exercises/practice/hamming/.meta/zcl_hamming.clas.abap
Normal file
27
exercises/practice/hamming/.meta/zcl_hamming.clas.abap
Normal file
@@ -0,0 +1,27 @@
|
||||
CLASS zcl_hamming DEFINITION PUBLIC.
|
||||
PUBLIC SECTION.
|
||||
METHODS hamming_distance
|
||||
IMPORTING
|
||||
first_strand TYPE string
|
||||
second_strand TYPE string
|
||||
RETURNING
|
||||
VALUE(result) TYPE i
|
||||
RAISING
|
||||
cx_static_check.
|
||||
ENDCLASS.
|
||||
|
||||
CLASS zcl_hamming IMPLEMENTATION.
|
||||
|
||||
METHOD hamming_distance.
|
||||
" TODO: Replace with exception when possible
|
||||
ASSERT strlen( first_strand ) = strlen( second_strand ).
|
||||
DATA(offset) = 0.
|
||||
WHILE offset < strlen( first_strand ).
|
||||
IF first_strand+offset(1) <> second_strand+offset(1).
|
||||
result = result + 1.
|
||||
ENDIF.
|
||||
offset = offset + 1.
|
||||
ENDWHILE.
|
||||
ENDMETHOD.
|
||||
|
||||
ENDCLASS.
|
||||
19
exercises/practice/hamming/zcl_hamming.clas.abap
Normal file
19
exercises/practice/hamming/zcl_hamming.clas.abap
Normal file
@@ -0,0 +1,19 @@
|
||||
CLASS zcl_hamming DEFINITION PUBLIC.
|
||||
PUBLIC SECTION.
|
||||
METHODS hamming_distance
|
||||
IMPORTING
|
||||
first_strand TYPE string
|
||||
second_strand TYPE string
|
||||
RETURNING
|
||||
VALUE(result) TYPE i
|
||||
RAISING
|
||||
cx_static_check.
|
||||
ENDCLASS.
|
||||
|
||||
CLASS zcl_hamming IMPLEMENTATION.
|
||||
|
||||
METHOD hamming_distance.
|
||||
" add solution here
|
||||
ENDMETHOD.
|
||||
|
||||
ENDCLASS.
|
||||
93
exercises/practice/hamming/zcl_hamming.clas.testclasses.abap
Normal file
93
exercises/practice/hamming/zcl_hamming.clas.testclasses.abap
Normal file
@@ -0,0 +1,93 @@
|
||||
CLASS ltcl_hamming DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATION SHORT FINAL.
|
||||
|
||||
PRIVATE SECTION.
|
||||
DATA cut TYPE REF TO zcl_hamming.
|
||||
METHODS setup.
|
||||
METHODS test_short_equal FOR TESTING RAISING cx_static_check.
|
||||
METHODS test_short_different FOR TESTING RAISING cx_static_check.
|
||||
METHODS test_long_equal FOR TESTING RAISING cx_static_check.
|
||||
METHODS test_long_different FOR TESTING RAISING cx_static_check.
|
||||
METHODS test_first_longer FOR TESTING RAISING cx_static_check.
|
||||
METHODS test_second_longer FOR TESTING RAISING cx_static_check.
|
||||
METHODS test_both_empty FOR TESTING RAISING cx_static_check.
|
||||
METHODS test_one_empty FOR TESTING RAISING cx_static_check.
|
||||
|
||||
ENDCLASS.
|
||||
|
||||
CLASS ltcl_hamming IMPLEMENTATION.
|
||||
|
||||
METHOD setup.
|
||||
cut = NEW zcl_hamming( ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD test_short_equal.
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
act = cut->hamming_distance(
|
||||
first_strand = 'A'
|
||||
second_strand = 'A' )
|
||||
exp = 0 ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD test_short_different.
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
act = cut->hamming_distance(
|
||||
first_strand = 'A'
|
||||
second_strand = 'G' )
|
||||
exp = 1 ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD test_long_equal.
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
act = cut->hamming_distance(
|
||||
first_strand = 'GACGACGTGCTAAAGATCCTG'
|
||||
second_strand = 'GACGACGTGCTAAAGATCCTG' )
|
||||
exp = 0 ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD test_long_different.
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
act = cut->hamming_distance(
|
||||
first_strand = 'GGACGGATTCTG'
|
||||
second_strand = 'AGGACGGATTCT' )
|
||||
exp = 9 ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD test_first_longer.
|
||||
TRY.
|
||||
cut->hamming_distance(
|
||||
first_strand = 'AACTATCG'
|
||||
second_strand = 'AACTATC' ).
|
||||
cl_abap_unit_assert=>fail( ).
|
||||
CATCH cx_static_check.
|
||||
ENDTRY.
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD test_second_longer.
|
||||
TRY.
|
||||
cut->hamming_distance(
|
||||
first_strand = 'AACTAAC'
|
||||
second_strand = 'AACTAACT' ).
|
||||
cl_abap_unit_assert=>fail( ).
|
||||
CATCH cx_static_check.
|
||||
ENDTRY.
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD test_both_empty.
|
||||
cl_abap_unit_assert=>assert_equals(
|
||||
act = cut->hamming_distance(
|
||||
first_strand = ''
|
||||
second_strand = '' )
|
||||
exp = 0 ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD test_one_empty.
|
||||
TRY.
|
||||
cut->hamming_distance(
|
||||
first_strand = ''
|
||||
second_strand = 'CGT' ).
|
||||
cl_abap_unit_assert=>fail( ).
|
||||
CATCH cx_static_check.
|
||||
ENDTRY.
|
||||
ENDMETHOD.
|
||||
|
||||
ENDCLASS.
|
||||
Reference in New Issue
Block a user