New exercise: Grains (#44)
* new exercise grains * change config.json to random place * add missing bracket in config.sjon * remove solution from template * change from decfloat to p * change from decfloat to p * linting * add abapGit Config Co-authored-by: Lars Hvam <larshp@hotmail.com> Co-authored-by: Marc Bernard <59966492+mbtools@users.noreply.github.com>
This commit is contained in:
10
config.json
10
config.json
@@ -77,6 +77,14 @@
|
|||||||
"prerequisites": [],
|
"prerequisites": [],
|
||||||
"difficulty": 1
|
"difficulty": 1
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"slug": "grains",
|
||||||
|
"name": "Grains",
|
||||||
|
"uuid": "ec0e08bc-c3c1-4e3e-9f62-29a4740e22dc",
|
||||||
|
"practices": [],
|
||||||
|
"prerequisites": [],
|
||||||
|
"difficulty": 1
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"slug": "kindergarten-garden",
|
"slug": "kindergarten-garden",
|
||||||
"name": "Kindergarten Garden",
|
"name": "Kindergarten Garden",
|
||||||
@@ -269,4 +277,4 @@
|
|||||||
"used_for/backends",
|
"used_for/backends",
|
||||||
"used_for/financial_systems"
|
"used_for/financial_systems"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
28
exercises/practice/grains/.docs/instructions.md
Normal file
28
exercises/practice/grains/.docs/instructions.md
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
# Description
|
||||||
|
|
||||||
|
Calculate the number of grains of wheat on a chessboard given that the number
|
||||||
|
on each square doubles.
|
||||||
|
|
||||||
|
There once was a wise servant who saved the life of a prince. The king
|
||||||
|
promised to pay whatever the servant could dream up. Knowing that the
|
||||||
|
king loved chess, the servant told the king he would like to have grains
|
||||||
|
of wheat. One grain on the first square of a chess board, with the number
|
||||||
|
of grains doubling on each successive square.
|
||||||
|
|
||||||
|
There are 64 squares on a chessboard (where square 1 has one grain, square 2 has two grains, and so on).
|
||||||
|
|
||||||
|
Write code that shows:
|
||||||
|
|
||||||
|
- how many grains were on a given square, and
|
||||||
|
- the total number of grains on the chessboard
|
||||||
|
|
||||||
|
## For bonus points
|
||||||
|
|
||||||
|
Did you get the tests passing and the code clean? If you want to, these
|
||||||
|
are some additional things you could try:
|
||||||
|
|
||||||
|
- Optimize for speed.
|
||||||
|
- Optimize for readability.
|
||||||
|
|
||||||
|
Then please share your thoughts in a comment on the submission. Did this
|
||||||
|
experiment make the code better? Worse? Did you learn anything from it?
|
||||||
14
exercises/practice/grains/.meta/config.json
Normal file
14
exercises/practice/grains/.meta/config.json
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"blurb": "Calculate the number of grains of wheat on a chessboard given that the number on each square doubles.",
|
||||||
|
"authors": ["marianfoo"],
|
||||||
|
"contributors": [
|
||||||
|
],
|
||||||
|
"files": {
|
||||||
|
"solution": ["zcl_grains.clas.abap"],
|
||||||
|
"test": ["zcl_grains.clas.testclasses.abap"],
|
||||||
|
"example": [".meta/zcl_grains.clas.abap"]
|
||||||
|
},
|
||||||
|
"source": "JavaRanch Cattle Drive, exercise 6",
|
||||||
|
"source_url": "http://www.javaranch.com/grains.jsp"
|
||||||
|
|
||||||
|
}
|
||||||
44
exercises/practice/grains/.meta/zcl_grains.clas.abap
Normal file
44
exercises/practice/grains/.meta/zcl_grains.clas.abap
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
CLASS zcl_grains DEFINITION
|
||||||
|
PUBLIC
|
||||||
|
FINAL
|
||||||
|
CREATE PUBLIC .
|
||||||
|
|
||||||
|
PUBLIC SECTION.
|
||||||
|
TYPES type_result TYPE p LENGTH 16 DECIMALS 0.
|
||||||
|
METHODS square
|
||||||
|
IMPORTING
|
||||||
|
input TYPE i
|
||||||
|
RETURNING
|
||||||
|
VALUE(result) TYPE type_result
|
||||||
|
RAISING
|
||||||
|
cx_parameter_invalid.
|
||||||
|
METHODS total
|
||||||
|
RETURNING
|
||||||
|
VALUE(result) TYPE type_result
|
||||||
|
RAISING
|
||||||
|
cx_parameter_invalid.
|
||||||
|
PROTECTED SECTION.
|
||||||
|
PRIVATE SECTION.
|
||||||
|
|
||||||
|
ENDCLASS.
|
||||||
|
|
||||||
|
|
||||||
|
CLASS zcl_grains IMPLEMENTATION.
|
||||||
|
METHOD square.
|
||||||
|
IF input < 1 OR input > 64.
|
||||||
|
RAISE EXCEPTION TYPE cx_parameter_invalid.
|
||||||
|
ENDIF.
|
||||||
|
result = 1.
|
||||||
|
DO input - 1 TIMES.
|
||||||
|
result = result * 2.
|
||||||
|
ENDDO.
|
||||||
|
ENDMETHOD.
|
||||||
|
|
||||||
|
METHOD total.
|
||||||
|
DO 64 TIMES.
|
||||||
|
result = result + square( sy-index ).
|
||||||
|
ENDDO.
|
||||||
|
ENDMETHOD.
|
||||||
|
|
||||||
|
|
||||||
|
ENDCLASS.
|
||||||
10
exercises/practice/grains/package.devc.xml
Normal file
10
exercises/practice/grains/package.devc.xml
Normal 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: Grains</CTEXT>
|
||||||
|
</DEVC>
|
||||||
|
</asx:values>
|
||||||
|
</asx:abap>
|
||||||
|
</abapGit>
|
||||||
36
exercises/practice/grains/zcl_grains.clas.abap
Normal file
36
exercises/practice/grains/zcl_grains.clas.abap
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
CLASS zcl_grains DEFINITION
|
||||||
|
PUBLIC
|
||||||
|
FINAL
|
||||||
|
CREATE PUBLIC .
|
||||||
|
|
||||||
|
PUBLIC SECTION.
|
||||||
|
TYPES type_result TYPE p LENGTH 16 DECIMALS 0.
|
||||||
|
METHODS square
|
||||||
|
IMPORTING
|
||||||
|
input TYPE i
|
||||||
|
RETURNING
|
||||||
|
VALUE(result) TYPE type_result
|
||||||
|
RAISING
|
||||||
|
cx_parameter_invalid.
|
||||||
|
METHODS total
|
||||||
|
RETURNING
|
||||||
|
VALUE(result) TYPE type_result
|
||||||
|
RAISING
|
||||||
|
cx_parameter_invalid.
|
||||||
|
PROTECTED SECTION.
|
||||||
|
PRIVATE SECTION.
|
||||||
|
|
||||||
|
ENDCLASS.
|
||||||
|
|
||||||
|
|
||||||
|
CLASS zcl_grains IMPLEMENTATION.
|
||||||
|
METHOD square.
|
||||||
|
" add solution here
|
||||||
|
ENDMETHOD.
|
||||||
|
|
||||||
|
METHOD total.
|
||||||
|
" add solution here
|
||||||
|
ENDMETHOD.
|
||||||
|
|
||||||
|
|
||||||
|
ENDCLASS.
|
||||||
112
exercises/practice/grains/zcl_grains.clas.testclasses.abap
Normal file
112
exercises/practice/grains/zcl_grains.clas.testclasses.abap
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
CLASS ltcl_grains DEFINITION FOR TESTING DURATION SHORT RISK LEVEL HARMLESS FINAL.
|
||||||
|
|
||||||
|
PRIVATE SECTION.
|
||||||
|
DATA cut TYPE REF TO zcl_grains.
|
||||||
|
METHODS setup.
|
||||||
|
METHODS test_square_1 FOR TESTING RAISING cx_static_check.
|
||||||
|
METHODS test_square_2 FOR TESTING RAISING cx_static_check.
|
||||||
|
METHODS test_square_3 FOR TESTING RAISING cx_static_check.
|
||||||
|
METHODS test_square_4 FOR TESTING RAISING cx_static_check.
|
||||||
|
METHODS test_square_16 FOR TESTING RAISING cx_static_check.
|
||||||
|
METHODS test_square_32 FOR TESTING RAISING cx_static_check.
|
||||||
|
METHODS test_square_64 FOR TESTING RAISING cx_static_check.
|
||||||
|
METHODS test_square_0 FOR TESTING RAISING cx_static_check.
|
||||||
|
METHODS test_square_minus_1 FOR TESTING RAISING cx_static_check.
|
||||||
|
METHODS test_square_65 FOR TESTING RAISING cx_static_check.
|
||||||
|
METHODS test_total FOR TESTING RAISING cx_static_check.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
ENDCLASS.
|
||||||
|
|
||||||
|
CLASS ltcl_grains IMPLEMENTATION.
|
||||||
|
|
||||||
|
METHOD setup.
|
||||||
|
cut = NEW zcl_grains( ).
|
||||||
|
ENDMETHOD.
|
||||||
|
|
||||||
|
"grains on square 1
|
||||||
|
METHOD test_square_1.
|
||||||
|
cl_abap_unit_assert=>assert_equals(
|
||||||
|
act = cut->square( 1 )
|
||||||
|
exp = 1 ).
|
||||||
|
ENDMETHOD.
|
||||||
|
|
||||||
|
"grains on square 2
|
||||||
|
METHOD test_square_2.
|
||||||
|
cl_abap_unit_assert=>assert_equals(
|
||||||
|
act = cut->square( 2 )
|
||||||
|
exp = 2 ).
|
||||||
|
ENDMETHOD.
|
||||||
|
|
||||||
|
"grains on square 3
|
||||||
|
METHOD test_square_3.
|
||||||
|
cl_abap_unit_assert=>assert_equals(
|
||||||
|
act = cut->square( 3 )
|
||||||
|
exp = 4 ).
|
||||||
|
ENDMETHOD.
|
||||||
|
|
||||||
|
"grains on square 4
|
||||||
|
METHOD test_square_4.
|
||||||
|
cl_abap_unit_assert=>assert_equals(
|
||||||
|
act = cut->square( 4 )
|
||||||
|
exp = 8 ).
|
||||||
|
ENDMETHOD.
|
||||||
|
|
||||||
|
"grains on square 16
|
||||||
|
METHOD test_square_16.
|
||||||
|
cl_abap_unit_assert=>assert_equals(
|
||||||
|
act = cut->square( 16 )
|
||||||
|
exp = 32768 ).
|
||||||
|
ENDMETHOD.
|
||||||
|
|
||||||
|
"grains on square 32
|
||||||
|
METHOD test_square_32.
|
||||||
|
cl_abap_unit_assert=>assert_equals(
|
||||||
|
act = cut->square( 32 )
|
||||||
|
exp = 2147483648 ).
|
||||||
|
ENDMETHOD.
|
||||||
|
|
||||||
|
"grains on square 64
|
||||||
|
METHOD test_square_64.
|
||||||
|
cl_abap_unit_assert=>assert_equals(
|
||||||
|
act = cut->square( 64 )
|
||||||
|
exp = 9223372036854775808 ).
|
||||||
|
ENDMETHOD.
|
||||||
|
|
||||||
|
"square 0 raises an exception
|
||||||
|
METHOD test_square_0.
|
||||||
|
TRY.
|
||||||
|
cut->square( 0 ).
|
||||||
|
cl_abap_unit_assert=>fail( ).
|
||||||
|
CATCH cx_parameter_invalid.
|
||||||
|
ENDTRY.
|
||||||
|
ENDMETHOD.
|
||||||
|
|
||||||
|
"negative square raises an exception
|
||||||
|
METHOD test_square_minus_1.
|
||||||
|
TRY.
|
||||||
|
cut->square( -1 ).
|
||||||
|
cl_abap_unit_assert=>fail( ).
|
||||||
|
CATCH cx_parameter_invalid.
|
||||||
|
ENDTRY.
|
||||||
|
ENDMETHOD.
|
||||||
|
|
||||||
|
"square greater than 64 raises an exception
|
||||||
|
METHOD test_square_65.
|
||||||
|
TRY.
|
||||||
|
cut->square( 65 ).
|
||||||
|
cl_abap_unit_assert=>fail( ).
|
||||||
|
CATCH cx_parameter_invalid.
|
||||||
|
ENDTRY.
|
||||||
|
ENDMETHOD.
|
||||||
|
|
||||||
|
"returns the total number of grains on the board
|
||||||
|
METHOD test_total.
|
||||||
|
cl_abap_unit_assert=>assert_equals(
|
||||||
|
act = cut->total( )
|
||||||
|
exp = 18446744073709551615 ).
|
||||||
|
ENDMETHOD.
|
||||||
|
|
||||||
|
|
||||||
|
ENDCLASS.
|
||||||
17
exercises/practice/grains/zcl_grains.clas.xml
Normal file
17
exercises/practice/grains/zcl_grains.clas.xml
Normal 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_GRAINS</CLSNAME>
|
||||||
|
<LANGU>D</LANGU>
|
||||||
|
<DESCRIPT>Exercism: Grains</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>
|
||||||
Reference in New Issue
Block a user