From b43dde6d175dbd6655586a0ce0567c41ce8e7ea1 Mon Sep 17 00:00:00 2001 From: bau-mann <17111602+bau-mann@users.noreply.github.com> Date: Fri, 28 Oct 2022 20:07:28 +0200 Subject: [PATCH 1/9] Fix EOL crypto-square --- .../crypto-square/.docs/instructions.md | 154 +++++++++--------- .../practice/crypto-square/.meta/config.json | 24 +-- .../.meta/zcl_crypto_square.clas.abap | 116 ++++++------- 3 files changed, 147 insertions(+), 147 deletions(-) diff --git a/exercises/practice/crypto-square/.docs/instructions.md b/exercises/practice/crypto-square/.docs/instructions.md index 34707c10..fe949323 100644 --- a/exercises/practice/crypto-square/.docs/instructions.md +++ b/exercises/practice/crypto-square/.docs/instructions.md @@ -1,78 +1,78 @@ -# Description - -Implement the classic method for composing secret messages called a square code. - -Given an English text, output the encoded version of that text. - -First, the input is normalized: the spaces and punctuation are removed -from the English text and the message is down-cased. - -Then, the normalized characters are broken into rows. These rows can be -regarded as forming a rectangle when printed with intervening newlines. - -For example, the sentence - -```text -"If man was meant to stay on the ground, god would have given us roots." -``` - -is normalized to: - -```text -"ifmanwasmeanttostayonthegroundgodwouldhavegivenusroots" -``` - -The plaintext should be organized in to a rectangle. The size of the -rectangle should be decided by the length of the message. - -If `c` is the number of columns and `r` is the number of rows, then for -the rectangle `r` x `c` find the smallest possible integer `c` such that: - -- `r * c >= length(message)`, -- and `c >= r`, -- and `c - r <= 1`. - -Our normalized text is 54 characters long, dictating a rectangle with -`c = 8` and `r = 7`: - -```text -"ifmanwas" -"meanttos" -"tayonthe" -"groundgo" -"dwouldha" -"vegivenu" -"sroots " -``` - -The coded message is obtained by reading down the columns going left to -right. - -The message above is coded as: - -```text -"imtgdvsfearwermayoogoanouuiontnnlvtwttddesaohghnsseoau" -``` - -Output the encoded text in chunks that fill perfect rectangles `(r X c)`, -with `c` chunks of `r` length, separated by spaces. For phrases that are -`n` characters short of the perfect rectangle, pad each of the last `n` -chunks with a single trailing space. - -```text -"imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau " -``` - -Notice that were we to stack these, we could visually decode the -ciphertext back in to the original message: - -```text -"imtgdvs" -"fearwer" -"mayoogo" -"anouuio" -"ntnnlvt" -"wttddes" -"aohghn " -"sseoau " +# Description + +Implement the classic method for composing secret messages called a square code. + +Given an English text, output the encoded version of that text. + +First, the input is normalized: the spaces and punctuation are removed +from the English text and the message is down-cased. + +Then, the normalized characters are broken into rows. These rows can be +regarded as forming a rectangle when printed with intervening newlines. + +For example, the sentence + +```text +"If man was meant to stay on the ground, god would have given us roots." +``` + +is normalized to: + +```text +"ifmanwasmeanttostayonthegroundgodwouldhavegivenusroots" +``` + +The plaintext should be organized in to a rectangle. The size of the +rectangle should be decided by the length of the message. + +If `c` is the number of columns and `r` is the number of rows, then for +the rectangle `r` x `c` find the smallest possible integer `c` such that: + +- `r * c >= length(message)`, +- and `c >= r`, +- and `c - r <= 1`. + +Our normalized text is 54 characters long, dictating a rectangle with +`c = 8` and `r = 7`: + +```text +"ifmanwas" +"meanttos" +"tayonthe" +"groundgo" +"dwouldha" +"vegivenu" +"sroots " +``` + +The coded message is obtained by reading down the columns going left to +right. + +The message above is coded as: + +```text +"imtgdvsfearwermayoogoanouuiontnnlvtwttddesaohghnsseoau" +``` + +Output the encoded text in chunks that fill perfect rectangles `(r X c)`, +with `c` chunks of `r` length, separated by spaces. For phrases that are +`n` characters short of the perfect rectangle, pad each of the last `n` +chunks with a single trailing space. + +```text +"imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau " +``` + +Notice that were we to stack these, we could visually decode the +ciphertext back in to the original message: + +```text +"imtgdvs" +"fearwer" +"mayoogo" +"anouuio" +"ntnnlvt" +"wttddes" +"aohghn " +"sseoau " ``` \ No newline at end of file diff --git a/exercises/practice/crypto-square/.meta/config.json b/exercises/practice/crypto-square/.meta/config.json index e8677e39..fb82f05a 100644 --- a/exercises/practice/crypto-square/.meta/config.json +++ b/exercises/practice/crypto-square/.meta/config.json @@ -1,13 +1,13 @@ -{ - "blurb": "Implement the classic method for composing secret messages called a square code.", - "authors": ["dwivedirahul44"], - "contributors": [ - ], - "files": { - "solution": ["zcl_crypto_square.clas.abap"], - "test": ["zcl_crypto_square.clas.testclasses.abap"], - "example": [".meta/zcl_crypto_square.clas.abap"] - }, - "source": "J Dalbey's Programming Practice problems", - "source_url": "http://users.csc.calpoly.edu/~jdalbey/103/Projects/ProgrammingPractice.html" +{ + "blurb": "Implement the classic method for composing secret messages called a square code.", + "authors": ["dwivedirahul44"], + "contributors": [ + ], + "files": { + "solution": ["zcl_crypto_square.clas.abap"], + "test": ["zcl_crypto_square.clas.testclasses.abap"], + "example": [".meta/zcl_crypto_square.clas.abap"] + }, + "source": "J Dalbey's Programming Practice problems", + "source_url": "http://users.csc.calpoly.edu/~jdalbey/103/Projects/ProgrammingPractice.html" } \ No newline at end of file diff --git a/exercises/practice/crypto-square/.meta/zcl_crypto_square.clas.abap b/exercises/practice/crypto-square/.meta/zcl_crypto_square.clas.abap index 3d7959eb..8fc38cba 100644 --- a/exercises/practice/crypto-square/.meta/zcl_crypto_square.clas.abap +++ b/exercises/practice/crypto-square/.meta/zcl_crypto_square.clas.abap @@ -1,58 +1,58 @@ -CLASS zcl_crypto_square DEFINITION - PUBLIC - FINAL - CREATE PUBLIC. - - PUBLIC SECTION. - METHODS encode IMPORTING plain_text TYPE string - RETURNING VALUE(crypto_text) TYPE string. - PROTECTED SECTION. - PRIVATE SECTION. -ENDCLASS. - - - -CLASS zcl_crypto_square IMPLEMENTATION. - METHOD encode. - DATA: - column TYPE i VALUE 2, - row TYPE i VALUE 1. - - DATA(str) = replace( val = plain_text - regex = '[[:punct:]]' - with = ' ' - occ = 0 ). - CONDENSE str NO-GAPS. - DATA(str_len) = strlen( str ). - IF str_len = 0. - RETURN. - ENDIF. - WHILE ( column * row ) < str_len. - IF column > row. - row += 1. - ELSE. - column += 1. - ENDIF. - ENDWHILE. - - crypto_text = REDUCE string( - INIT txt TYPE string - FOR i = 0 UNTIL i = column - FOR j = 0 UNTIL j = row - NEXT txt &&= |{ - "check offset beyond string length? - COND string( WHEN ( ( j * column ) + i ) >= str_len - "add space if , more than one row in square - THEN COND string( WHEN row > 1 - THEN ` ` - ELSE `` ) - ELSE to_lower( substring( val = str - off = ( ( j * column ) + i ) - len = 1 ) ) ) - }{ COND string( WHEN j + 1 = row - AND i + 1 < column - "add succeeding space for each letter group - AND row > 1 THEN ` ` - ELSE `` ) }| ). - ENDMETHOD. -ENDCLASS. +CLASS zcl_crypto_square DEFINITION + PUBLIC + FINAL + CREATE PUBLIC. + + PUBLIC SECTION. + METHODS encode IMPORTING plain_text TYPE string + RETURNING VALUE(crypto_text) TYPE string. + PROTECTED SECTION. + PRIVATE SECTION. +ENDCLASS. + + + +CLASS zcl_crypto_square IMPLEMENTATION. + METHOD encode. + DATA: + column TYPE i VALUE 2, + row TYPE i VALUE 1. + + DATA(str) = replace( val = plain_text + regex = '[[:punct:]]' + with = ' ' + occ = 0 ). + CONDENSE str NO-GAPS. + DATA(str_len) = strlen( str ). + IF str_len = 0. + RETURN. + ENDIF. + WHILE ( column * row ) < str_len. + IF column > row. + row += 1. + ELSE. + column += 1. + ENDIF. + ENDWHILE. + + crypto_text = REDUCE string( + INIT txt TYPE string + FOR i = 0 UNTIL i = column + FOR j = 0 UNTIL j = row + NEXT txt &&= |{ + "check offset beyond string length? + COND string( WHEN ( ( j * column ) + i ) >= str_len + "add space if , more than one row in square + THEN COND string( WHEN row > 1 + THEN ` ` + ELSE `` ) + ELSE to_lower( substring( val = str + off = ( ( j * column ) + i ) + len = 1 ) ) ) + }{ COND string( WHEN j + 1 = row + AND i + 1 < column + "add succeeding space for each letter group + AND row > 1 THEN ` ` + ELSE `` ) }| ). + ENDMETHOD. +ENDCLASS. From b47e2f02f6569ac2ff58bcc10cf812941144bfa3 Mon Sep 17 00:00:00 2001 From: bau-mann <17111602+bau-mann@users.noreply.github.com> Date: Sat, 29 Oct 2022 10:54:32 +0200 Subject: [PATCH 2/9] Knapsack Stub --- config.json | 10 +++++- .../practice/knapsack/.docs/instructions.md | 32 +++++++++++++++++++ exercises/practice/knapsack/.meta/config.json | 11 +++++++ exercises/practice/knapsack/.meta/tests.toml | 31 ++++++++++++++++++ .../knapsack/.meta/zcl_knapsack.clas.abap | 0 exercises/practice/knapsack/package.devc.xml | 10 ++++++ .../practice/knapsack/zcl_knapsack.clas.abap | 0 .../zcl_knapsack.clas.testclasses.abap | 0 .../practice/knapsack/zcl_knapsack.clas.xml | 17 ++++++++++ 9 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 exercises/practice/knapsack/.docs/instructions.md create mode 100644 exercises/practice/knapsack/.meta/config.json create mode 100644 exercises/practice/knapsack/.meta/tests.toml create mode 100644 exercises/practice/knapsack/.meta/zcl_knapsack.clas.abap create mode 100644 exercises/practice/knapsack/package.devc.xml create mode 100644 exercises/practice/knapsack/zcl_knapsack.clas.abap create mode 100644 exercises/practice/knapsack/zcl_knapsack.clas.testclasses.abap create mode 100644 exercises/practice/knapsack/zcl_knapsack.clas.xml diff --git a/config.json b/config.json index 08873ebb..d4e6b28d 100644 --- a/config.json +++ b/config.json @@ -281,6 +281,14 @@ "prerequisites": [], "difficulty": 4 }, + { + "slug": "knapsack", + "name": "Knapsack", + "uuid": "2f9877d5-eec5-4048-a868-975d028147ef", + "practices": [], + "prerequisites": [], + "difficulty": 5 + }, { "slug": "book-store", "name": "Book Store", @@ -336,7 +344,7 @@ "practices": [], "prerequisites": [], "difficulty": 3 - } + } ] }, "concepts": [ diff --git a/exercises/practice/knapsack/.docs/instructions.md b/exercises/practice/knapsack/.docs/instructions.md new file mode 100644 index 00000000..1dbbca91 --- /dev/null +++ b/exercises/practice/knapsack/.docs/instructions.md @@ -0,0 +1,32 @@ +# Instructions + +In this exercise, let's try to solve a classic problem. + +Bob is a thief. +After months of careful planning, he finally manages to crack the security systems of a high-class apartment. + +In front of him are many items, each with a value (v) and weight (w). +Bob, of course, wants to maximize the total value he can get; he would gladly take all of the items if he could. +However, to his horror, he realizes that the knapsack he carries with him can only hold so much weight (W). + +Given a knapsack with a specific carrying capacity (W), help Bob determine the maximum value he can get from the items in the house. +Note that Bob can take only one of each item. + +All values given will be strictly positive. +Items will be represented as a list of pairs, `wi` and `vi`, where the first element `wi` is the weight of the *i*th item and `vi` is the value for that item. + +For example: + +Items: [ + { "weight": 5, "value": 10 }, + { "weight": 4, "value": 40 }, + { "weight": 6, "value": 30 }, + { "weight": 4, "value": 50 } +] + +Knapsack Limit: 10 + +For the above, the first item has weight 5 and value 10, the second item has weight 4 and value 40, and so on. + +In this example, Bob should take the second and fourth item to maximize his value, which, in this case, is 90. +He cannot get more than 90 as his knapsack has a weight limit of 10. diff --git a/exercises/practice/knapsack/.meta/config.json b/exercises/practice/knapsack/.meta/config.json new file mode 100644 index 00000000..1a4c5165 --- /dev/null +++ b/exercises/practice/knapsack/.meta/config.json @@ -0,0 +1,11 @@ +{ + "authors": [], + "files": { + "solution": [], + "test": [], + "example": [] + }, + "blurb": "Given a knapsack that can only carry a certain weight, determine which items to put in the knapsack in order to maximize their combined value.", + "source": "Wikipedia", + "source_url": "https://en.wikipedia.org/wiki/Knapsack_problem" +} diff --git a/exercises/practice/knapsack/.meta/tests.toml b/exercises/practice/knapsack/.meta/tests.toml new file mode 100644 index 00000000..febc7b26 --- /dev/null +++ b/exercises/practice/knapsack/.meta/tests.toml @@ -0,0 +1,31 @@ +# This is an auto-generated file. +# +# Regenerating this file via `configlet sync` will: +# - Recreate every `description` key/value pair +# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications +# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) +# - Preserve any other key/value pair +# +# As user-added comments (using the # character) will be removed when this file +# is regenerated, comments can be added via a `comment` key. + +[a4d7d2f0-ad8a-460c-86f3-88ba709d41a7] +description = "no items" + +[1d39e98c-6249-4a8b-912f-87cb12e506b0] +description = "one item, too heavy" + +[833ea310-6323-44f2-9d27-a278740ffbd8] +description = "five items (cannot be greedy by weight)" + +[277cdc52-f835-4c7d-872b-bff17bab2456] +description = "five items (cannot be greedy by value)" + +[81d8e679-442b-4f7a-8a59-7278083916c9] +description = "example knapsack" + +[f23a2449-d67c-4c26-bf3e-cde020f27ecc] +description = "8 items" + +[7c682ae9-c385-4241-a197-d2fa02c81a11] +description = "15 items" diff --git a/exercises/practice/knapsack/.meta/zcl_knapsack.clas.abap b/exercises/practice/knapsack/.meta/zcl_knapsack.clas.abap new file mode 100644 index 00000000..e69de29b diff --git a/exercises/practice/knapsack/package.devc.xml b/exercises/practice/knapsack/package.devc.xml new file mode 100644 index 00000000..5c226240 --- /dev/null +++ b/exercises/practice/knapsack/package.devc.xml @@ -0,0 +1,10 @@ + + + + + + Exercism: Knapsack + + + + diff --git a/exercises/practice/knapsack/zcl_knapsack.clas.abap b/exercises/practice/knapsack/zcl_knapsack.clas.abap new file mode 100644 index 00000000..e69de29b diff --git a/exercises/practice/knapsack/zcl_knapsack.clas.testclasses.abap b/exercises/practice/knapsack/zcl_knapsack.clas.testclasses.abap new file mode 100644 index 00000000..e69de29b diff --git a/exercises/practice/knapsack/zcl_knapsack.clas.xml b/exercises/practice/knapsack/zcl_knapsack.clas.xml new file mode 100644 index 00000000..1a07ad1f --- /dev/null +++ b/exercises/practice/knapsack/zcl_knapsack.clas.xml @@ -0,0 +1,17 @@ + + + + + + ZCL_KNAPSACK + E + Exercism: Knapsack + 1 + X + X + X + X + + + + From 6e3575ca1c7d87285848c69dcb91743a55551528 Mon Sep 17 00:00:00 2001 From: bau-mann <17111602+bau-mann@users.noreply.github.com> Date: Sat, 10 Dec 2022 21:56:47 +0100 Subject: [PATCH 3/9] Implementation knapsack exercise --- .../knapsack/.meta/zcl_knapsack.clas.abap | 62 ++++++++++ .../practice/knapsack/zcl_knapsack.clas.abap | 31 +++++ .../zcl_knapsack.clas.testclasses.abap | 111 ++++++++++++++++++ 3 files changed, 204 insertions(+) diff --git a/exercises/practice/knapsack/.meta/zcl_knapsack.clas.abap b/exercises/practice/knapsack/.meta/zcl_knapsack.clas.abap index e69de29b..c6ed05a1 100644 --- a/exercises/practice/knapsack/.meta/zcl_knapsack.clas.abap +++ b/exercises/practice/knapsack/.meta/zcl_knapsack.clas.abap @@ -0,0 +1,62 @@ +CLASS zcl_knapsack DEFINITION + PUBLIC + FINAL + CREATE PUBLIC . + + PUBLIC SECTION. + + TYPES: BEGIN OF item_type, + weight TYPE i, + value TYPE i, + END OF item_type. + TYPES items_type TYPE TABLE OF item_type. + + METHODS get_max_possible_value + IMPORTING weight_limit TYPE i + items TYPE items_type + RETURNING VALUE(max_value) TYPE i. + + PROTECTED SECTION. + PRIVATE SECTION. +ENDCLASS. + + + +CLASS zcl_knapsack IMPLEMENTATION. + + + METHOD get_max_possible_value. + + TYPES int_table TYPE STANDARD TABLE OF i WITH NON-UNIQUE KEY table_line. + DATA matrix TYPE STANDARD TABLE OF int_table. + + " Implementation of dynamic programming approach + " https://en.wikipedia.org/wiki/Knapsack_problem#0-1_knapsack_problem + + DO lines( items ) + 1 TIMES. + APPEND VALUE #( ) TO matrix REFERENCE INTO DATA(item_line). + DO weight_limit + 1 TIMES. + APPEND VALUE #( ) TO item_line->*. + ENDDO. + ENDDO. + + DATA i TYPE i VALUE 2. + DATA j TYPE i. + + DO lines( items ) TIMES. + j = 1. + DO weight_limit + 1 TIMES. + matrix[ i ][ j ] = nmax( val1 = matrix[ i - 1 ][ j ] " current item not included + val2 = COND i( WHEN items[ i - 1 ]-weight <= j - 1 " weight limit met? + THEN matrix[ i - 1 ][ j - items[ i - 1 ]-weight ] " item included + + items[ i - 1 ]-value + ELSE 0 ) ). " weight limit exceeded + j += 1. + ENDDO. + i += 1. + ENDDO. + + max_value = matrix[ lines( items ) + 1 ][ weight_limit + 1 ]. + + ENDMETHOD. +ENDCLASS. diff --git a/exercises/practice/knapsack/zcl_knapsack.clas.abap b/exercises/practice/knapsack/zcl_knapsack.clas.abap index e69de29b..125387d2 100644 --- a/exercises/practice/knapsack/zcl_knapsack.clas.abap +++ b/exercises/practice/knapsack/zcl_knapsack.clas.abap @@ -0,0 +1,31 @@ +CLASS zcl_knapsack DEFINITION + PUBLIC + FINAL + CREATE PUBLIC . + + PUBLIC SECTION. + + TYPES: BEGIN OF item_type, + weight TYPE i, + value TYPE i, + END OF item_type. + TYPES items_type TYPE TABLE OF item_type. + + METHODS get_max_possible_value + IMPORTING weight_limit TYPE i + items TYPE items_type + RETURNING VALUE(max_value) TYPE i. + + PROTECTED SECTION. + PRIVATE SECTION. +ENDCLASS. + + + +CLASS zcl_knapsack IMPLEMENTATION. + + + METHOD get_max_possible_value. + " add solution here + ENDMETHOD. +ENDCLASS. diff --git a/exercises/practice/knapsack/zcl_knapsack.clas.testclasses.abap b/exercises/practice/knapsack/zcl_knapsack.clas.testclasses.abap index e69de29b..c98bb1f7 100644 --- a/exercises/practice/knapsack/zcl_knapsack.clas.testclasses.abap +++ b/exercises/practice/knapsack/zcl_knapsack.clas.testclasses.abap @@ -0,0 +1,111 @@ +CLASS ltcl_knapsack DEFINITION FINAL FOR TESTING + DURATION SHORT + RISK LEVEL HARMLESS. + + PRIVATE SECTION. + + DATA cut TYPE REF TO zcl_knapsack. + + METHODS setup. + + METHODS no_items FOR TESTING RAISING cx_static_check. + METHODS one_item_too_heavy FOR TESTING RAISING cx_static_check. + "! five items (cannot be greedy by weight) + METHODS five_items_cannot_be_greedy_1 FOR TESTING RAISING cx_static_check. + "! five items (cannot be greedy by value) + METHODS five_items_cannot_be_greedy_2 FOR TESTING RAISING cx_static_check. + METHODS example_knapsack FOR TESTING RAISING cx_static_check. + METHODS eight_items FOR TESTING RAISING cx_static_check. + METHODS fifteen_items FOR TESTING RAISING cx_static_check. + +ENDCLASS. + + +CLASS ltcl_knapsack IMPLEMENTATION. + + METHOD setup. + cut = NEW #( ). + ENDMETHOD. + + METHOD no_items. + cl_abap_unit_assert=>assert_equals( + exp = 0 + act = cut->get_max_possible_value( weight_limit = 100 + items = VALUE #( ) ) ). + ENDMETHOD. + + METHOD one_item_too_heavy. + cl_abap_unit_assert=>assert_equals( + exp = 0 + act = cut->get_max_possible_value( weight_limit = 10 + items = VALUE #( ( weight = 100 value = 1 ) ) ) ). + ENDMETHOD. + + METHOD five_items_cannot_be_greedy_1. + cl_abap_unit_assert=>assert_equals( + exp = 21 + act = cut->get_max_possible_value( weight_limit = 10 + items = VALUE #( ( weight = 2 value = 5 ) + ( weight = 2 value = 5 ) + ( weight = 2 value = 5 ) + ( weight = 2 value = 5 ) + ( weight = 10 value = 21 ) ) ) ). + ENDMETHOD. + + METHOD five_items_cannot_be_greedy_2. + cl_abap_unit_assert=>assert_equals( + exp = 80 + act = cut->get_max_possible_value( weight_limit = 10 + items = VALUE #( ( weight = 2 value = 20 ) + ( weight = 2 value = 20 ) + ( weight = 2 value = 20 ) + ( weight = 2 value = 20 ) + ( weight = 10 value = 50 ) ) ) ). + ENDMETHOD. + + METHOD example_knapsack. + cl_abap_unit_assert=>assert_equals( + exp = 90 + act = cut->get_max_possible_value( weight_limit = 10 + items = VALUE #( ( weight = 5 value = 10 ) + ( weight = 4 value = 40 ) + ( weight = 6 value = 30 ) + ( weight = 4 value = 50 ) ) ) ). + ENDMETHOD. + + METHOD eight_items. + cl_abap_unit_assert=>assert_equals( + exp = 900 + act = cut->get_max_possible_value( weight_limit = 104 + items = VALUE #( ( weight = 25 value = 350 ) + ( weight = 35 value = 400 ) + ( weight = 45 value = 450 ) + ( weight = 5 value = 20 ) + ( weight = 25 value = 70 ) + ( weight = 3 value = 8 ) + ( weight = 2 value = 5 ) + ( weight = 2 value = 5 ) ) ) ). + ENDMETHOD. + + METHOD fifteen_items. + cl_abap_unit_assert=>assert_equals( + exp = 1458 + act = cut->get_max_possible_value( weight_limit = 750 + items = VALUE #( ( weight = 70 value = 135 ) + ( weight = 73 value = 139 ) + ( weight = 77 value = 149 ) + ( weight = 80 value = 150 ) + ( weight = 82 value = 156 ) + ( weight = 87 value = 163 ) + ( weight = 90 value = 173 ) + ( weight = 94 value = 184 ) + ( weight = 98 value = 192 ) + ( weight = 106 value = 201 ) + ( weight = 110 value = 210 ) + ( weight = 113 value = 214 ) + ( weight = 115 value = 221 ) + ( weight = 118 value = 229 ) + ( weight = 120 value = 240 ) ) ) ). + ENDMETHOD. + +ENDCLASS. From 83c8e374bd4999d7ce50d29f1d3d3e98a15bc761 Mon Sep 17 00:00:00 2001 From: bau-mann <17111602+bau-mann@users.noreply.github.com> Date: Sat, 10 Dec 2022 22:17:06 +0100 Subject: [PATCH 4/9] fix confix.json --- exercises/practice/knapsack/.meta/config.json | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/exercises/practice/knapsack/.meta/config.json b/exercises/practice/knapsack/.meta/config.json index 1a4c5165..7feb724d 100644 --- a/exercises/practice/knapsack/.meta/config.json +++ b/exercises/practice/knapsack/.meta/config.json @@ -1,9 +1,15 @@ { "authors": [], "files": { - "solution": [], - "test": [], - "example": [] + "solution": [ + "zcl_knapsack.clas.abap" + ], + "test": [ + "zcl_knapsack.testclasses.clas.abap" + ], + "example": [ + ".meta/zcl_knapsack.clas.abap" + ] }, "blurb": "Given a knapsack that can only carry a certain weight, determine which items to put in the knapsack in order to maximize their combined value.", "source": "Wikipedia", From d4865cb2477080befae38f0ce668e862a1592be5 Mon Sep 17 00:00:00 2001 From: bau-mann <17111602+bau-mann@users.noreply.github.com> Date: Sat, 10 Dec 2022 22:18:13 +0100 Subject: [PATCH 5/9] abaplint check_comments --- .../practice/knapsack/.meta/zcl_knapsack.clas.abap | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/exercises/practice/knapsack/.meta/zcl_knapsack.clas.abap b/exercises/practice/knapsack/.meta/zcl_knapsack.clas.abap index c6ed05a1..c6b2636d 100644 --- a/exercises/practice/knapsack/.meta/zcl_knapsack.clas.abap +++ b/exercises/practice/knapsack/.meta/zcl_knapsack.clas.abap @@ -46,11 +46,13 @@ CLASS zcl_knapsack IMPLEMENTATION. DO lines( items ) TIMES. j = 1. DO weight_limit + 1 TIMES. - matrix[ i ][ j ] = nmax( val1 = matrix[ i - 1 ][ j ] " current item not included - val2 = COND i( WHEN items[ i - 1 ]-weight <= j - 1 " weight limit met? - THEN matrix[ i - 1 ][ j - items[ i - 1 ]-weight ] " item included + " Include current item only if it yields a value increase + " Including item is only an option if current weight limit is met + matrix[ i ][ j ] = nmax( val1 = matrix[ i - 1 ][ j ] + val2 = COND i( WHEN items[ i - 1 ]-weight <= j - 1 + THEN matrix[ i - 1 ][ j - items[ i - 1 ]-weight ] + items[ i - 1 ]-value - ELSE 0 ) ). " weight limit exceeded + ELSE 0 ) ). j += 1. ENDDO. i += 1. From 165d6ac58b2521e19d830f1eb62371b907f26cd9 Mon Sep 17 00:00:00 2001 From: bau-mann <17111602+bau-mann@users.noreply.github.com> Date: Sat, 10 Dec 2022 22:20:58 +0100 Subject: [PATCH 6/9] fix config.json testclass reference --- exercises/practice/knapsack/.meta/config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/knapsack/.meta/config.json b/exercises/practice/knapsack/.meta/config.json index 7feb724d..0c57d590 100644 --- a/exercises/practice/knapsack/.meta/config.json +++ b/exercises/practice/knapsack/.meta/config.json @@ -5,7 +5,7 @@ "zcl_knapsack.clas.abap" ], "test": [ - "zcl_knapsack.testclasses.clas.abap" + "zcl_knapsack.clas.testclasses.abap" ], "example": [ ".meta/zcl_knapsack.clas.abap" From a661c7a0eba319e80a6c3ea07a764826b9470d69 Mon Sep 17 00:00:00 2001 From: bau-mann <17111602+bau-mann@users.noreply.github.com> Date: Tue, 17 Jan 2023 19:58:31 +0100 Subject: [PATCH 7/9] Formating zcl_knapsack --- exercises/practice/knapsack/zcl_knapsack.clas.abap | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/knapsack/zcl_knapsack.clas.abap b/exercises/practice/knapsack/zcl_knapsack.clas.abap index 125387d2..a795d5ec 100644 --- a/exercises/practice/knapsack/zcl_knapsack.clas.abap +++ b/exercises/practice/knapsack/zcl_knapsack.clas.abap @@ -24,8 +24,8 @@ ENDCLASS. CLASS zcl_knapsack IMPLEMENTATION. - METHOD get_max_possible_value. " add solution here ENDMETHOD. + ENDCLASS. From b1d7124c083830f6b96cd91c1650e8d8012c8354 Mon Sep 17 00:00:00 2001 From: bau-mann <17111602+bau-mann@users.noreply.github.com> Date: Tue, 17 Jan 2023 20:06:03 +0100 Subject: [PATCH 8/9] Update config.json --- config.json | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/config.json b/config.json index bf28084c..ac863d04 100644 --- a/config.json +++ b/config.json @@ -352,7 +352,15 @@ "practices": [], "prerequisites": [], "difficulty": 3 - } + }, + { + "slug": "knapsack", + "name": "Knapsack", + "uuid": "2f9877d5-eec5-4048-a868-975d028147ef", + "practices": [], + "prerequisites": [], + "difficulty": 5 + } ] }, "concepts": [ From 4e8925b28a6a0115bbcdac29468c02066f0761ec Mon Sep 17 00:00:00 2001 From: Marc Bernard <59966492+mbtools@users.noreply.github.com> Date: Wed, 4 Oct 2023 09:26:58 -0400 Subject: [PATCH 9/9] Delete exercises/practice/knapsack/.meta/tests.toml not necessary for abap --- exercises/practice/knapsack/.meta/tests.toml | 31 -------------------- 1 file changed, 31 deletions(-) delete mode 100644 exercises/practice/knapsack/.meta/tests.toml diff --git a/exercises/practice/knapsack/.meta/tests.toml b/exercises/practice/knapsack/.meta/tests.toml deleted file mode 100644 index febc7b26..00000000 --- a/exercises/practice/knapsack/.meta/tests.toml +++ /dev/null @@ -1,31 +0,0 @@ -# This is an auto-generated file. -# -# Regenerating this file via `configlet sync` will: -# - Recreate every `description` key/value pair -# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications -# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) -# - Preserve any other key/value pair -# -# As user-added comments (using the # character) will be removed when this file -# is regenerated, comments can be added via a `comment` key. - -[a4d7d2f0-ad8a-460c-86f3-88ba709d41a7] -description = "no items" - -[1d39e98c-6249-4a8b-912f-87cb12e506b0] -description = "one item, too heavy" - -[833ea310-6323-44f2-9d27-a278740ffbd8] -description = "five items (cannot be greedy by weight)" - -[277cdc52-f835-4c7d-872b-bff17bab2456] -description = "five items (cannot be greedy by value)" - -[81d8e679-442b-4f7a-8a59-7278083916c9] -description = "example knapsack" - -[f23a2449-d67c-4c26-bf3e-cde020f27ecc] -description = "8 items" - -[7c682ae9-c385-4241-a197-d2fa02c81a11] -description = "15 items"