From d067e42cc240c95bcb655fa03dd5d537ad5f2cb2 Mon Sep 17 00:00:00 2001 From: Marian Zeis <13335743+marianfoo@users.noreply.github.com> Date: Wed, 8 Jun 2022 14:00:38 +0200 Subject: [PATCH] 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 Co-authored-by: Marc Bernard <59966492+mbtools@users.noreply.github.com> --- config.json | 10 +- .../practice/grains/.docs/instructions.md | 28 +++++ exercises/practice/grains/.meta/config.json | 14 +++ .../grains/.meta/zcl_grains.clas.abap | 44 +++++++ exercises/practice/grains/package.devc.xml | 10 ++ .../practice/grains/zcl_grains.clas.abap | 36 ++++++ .../grains/zcl_grains.clas.testclasses.abap | 112 ++++++++++++++++++ exercises/practice/grains/zcl_grains.clas.xml | 17 +++ 8 files changed, 270 insertions(+), 1 deletion(-) create mode 100644 exercises/practice/grains/.docs/instructions.md create mode 100644 exercises/practice/grains/.meta/config.json create mode 100644 exercises/practice/grains/.meta/zcl_grains.clas.abap create mode 100644 exercises/practice/grains/package.devc.xml create mode 100644 exercises/practice/grains/zcl_grains.clas.abap create mode 100644 exercises/practice/grains/zcl_grains.clas.testclasses.abap create mode 100644 exercises/practice/grains/zcl_grains.clas.xml diff --git a/config.json b/config.json index e5c3941..c63646c 100644 --- a/config.json +++ b/config.json @@ -77,6 +77,14 @@ "prerequisites": [], "difficulty": 1 }, + { + "slug": "grains", + "name": "Grains", + "uuid": "ec0e08bc-c3c1-4e3e-9f62-29a4740e22dc", + "practices": [], + "prerequisites": [], + "difficulty": 1 + }, { "slug": "kindergarten-garden", "name": "Kindergarten Garden", @@ -269,4 +277,4 @@ "used_for/backends", "used_for/financial_systems" ] -} +} \ No newline at end of file diff --git a/exercises/practice/grains/.docs/instructions.md b/exercises/practice/grains/.docs/instructions.md new file mode 100644 index 0000000..4c70b70 --- /dev/null +++ b/exercises/practice/grains/.docs/instructions.md @@ -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? \ No newline at end of file diff --git a/exercises/practice/grains/.meta/config.json b/exercises/practice/grains/.meta/config.json new file mode 100644 index 0000000..8c1a36e --- /dev/null +++ b/exercises/practice/grains/.meta/config.json @@ -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" + +} \ No newline at end of file diff --git a/exercises/practice/grains/.meta/zcl_grains.clas.abap b/exercises/practice/grains/.meta/zcl_grains.clas.abap new file mode 100644 index 0000000..0cdbc6a --- /dev/null +++ b/exercises/practice/grains/.meta/zcl_grains.clas.abap @@ -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. \ No newline at end of file diff --git a/exercises/practice/grains/package.devc.xml b/exercises/practice/grains/package.devc.xml new file mode 100644 index 0000000..e765ef5 --- /dev/null +++ b/exercises/practice/grains/package.devc.xml @@ -0,0 +1,10 @@ + + + + + + Exercism: Grains + + + + diff --git a/exercises/practice/grains/zcl_grains.clas.abap b/exercises/practice/grains/zcl_grains.clas.abap new file mode 100644 index 0000000..c6c0c2a --- /dev/null +++ b/exercises/practice/grains/zcl_grains.clas.abap @@ -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. diff --git a/exercises/practice/grains/zcl_grains.clas.testclasses.abap b/exercises/practice/grains/zcl_grains.clas.testclasses.abap new file mode 100644 index 0000000..45bd9e9 --- /dev/null +++ b/exercises/practice/grains/zcl_grains.clas.testclasses.abap @@ -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. diff --git a/exercises/practice/grains/zcl_grains.clas.xml b/exercises/practice/grains/zcl_grains.clas.xml new file mode 100644 index 0000000..199e674 --- /dev/null +++ b/exercises/practice/grains/zcl_grains.clas.xml @@ -0,0 +1,17 @@ + + + + + + ZCL_GRAINS + D + Exercism: Grains + 1 + X + X + X + X + + + +