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
|
||||
}
|
||||
]
|
||||
},
|
||||
|
||||
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