From 5c5979403fcf09e3239b160c409428ebfc605295 Mon Sep 17 00:00:00 2001 From: kapitaali Date: Mon, 3 Jun 2024 10:34:41 +0300 Subject: [PATCH] adding knapsack --- config.json | 8 + .../practice/knapsack/.docs/instructions.md | 37 ++++ exercises/practice/knapsack/.meta/config.json | 26 +++ .../practice/knapsack/.meta/proof.ci.cob | 85 ++++++++ .../practice/knapsack/bin/fetch-cobolcheck | 63 ++++++ .../knapsack/bin/fetch-cobolcheck.ps1 | 28 +++ exercises/practice/knapsack/config.properties | 183 ++++++++++++++++++ exercises/practice/knapsack/src/knapsack.cob | 17 ++ exercises/practice/knapsack/test.ps1 | 21 ++ exercises/practice/knapsack/test.sh | 20 ++ .../knapsack/tst/knapsack/knapsack.cut | 137 +++++++++++++ 11 files changed, 625 insertions(+) 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/proof.ci.cob create mode 100755 exercises/practice/knapsack/bin/fetch-cobolcheck create mode 100644 exercises/practice/knapsack/bin/fetch-cobolcheck.ps1 create mode 100644 exercises/practice/knapsack/config.properties create mode 100644 exercises/practice/knapsack/src/knapsack.cob create mode 100644 exercises/practice/knapsack/test.ps1 create mode 100755 exercises/practice/knapsack/test.sh create mode 100644 exercises/practice/knapsack/tst/knapsack/knapsack.cut diff --git a/config.json b/config.json index ad3426d8..fdc5a521 100644 --- a/config.json +++ b/config.json @@ -369,6 +369,14 @@ "prerequisites": [], "difficulty": 3 }, + { + "slug": "knapsack", + "name": "Knapsack", + "uuid": "0408328a-3e04-4e2a-b5bf-fad131747a09", + "practices": [], + "prerequisites": [], + "difficulty": 3 + }, { "slug": "meetup", "name": "Meetup", diff --git a/exercises/practice/knapsack/.docs/instructions.md b/exercises/practice/knapsack/.docs/instructions.md new file mode 100644 index 00000000..812d1010 --- /dev/null +++ b/exercises/practice/knapsack/.docs/instructions.md @@ -0,0 +1,37 @@ +# 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..8d5fd0db --- /dev/null +++ b/exercises/practice/knapsack/.meta/config.json @@ -0,0 +1,26 @@ +{ + "authors": [ + "kapitaali" + ], + "files": { + "solution": [ + "src/knapsack.cob" + ], + "test": [ + "tst/knapsack/knapsack.cut" + ], + "example": [ + ".meta/proof.ci.cob" + ], + "invalidator": [ + "test.ps1", + "test.sh", + "bin/fetch-cobolcheck", + "bin/fetch-cobolcheck.ps1", + "config.properties" + ] + }, + "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/proof.ci.cob b/exercises/practice/knapsack/.meta/proof.ci.cob new file mode 100644 index 00000000..b6c357cc --- /dev/null +++ b/exercises/practice/knapsack/.meta/proof.ci.cob @@ -0,0 +1,85 @@ + IDENTIFICATION DIVISION. + PROGRAM-ID. KNAPSACK. + AUTHOR. kapitaali. + ENVIRONMENT DIVISION. + CONFIGURATION SECTION. + REPOSITORY. + FUNCTION ALL INTRINSIC. + + DATA DIVISION. + WORKING-STORAGE SECTION. + 01 WS-INPUTS. + 05 MAXIMUM-WEIGHT PIC 999. + 05 NO-OF-ITEMS PIC 99. + + 01 KNAPSACKTABLE. + 05 TABLEROW OCCURS 1 TO 20 DEPENDING ON NO-OF-ITEMS + INDEXED BY INDX. + 10 WS-WEIGHT PIC 9999. + 10 WS-VALUE PIC 9999. + + 01 WS-OUTPUTS. + 05 WS-RESULT PIC 9999. + + 01 MY-VARS. + 05 A PIC 9999. + 05 B PIC 9999. + 05 C PIC 9999. + 05 I PIC 9999. + 05 J PIC 9999. + 05 ITEMS-PLUS1 PIC 9999. + 05 WEIGHT-PLUS1 PIC 9999. + + 01 MY-TABLE. + 05 W OCCURS 1 TO 9999 DEPENDING ON MAXIMUM-WEIGHT. + 10 ITEMS OCCURS 20 TIMES. + 15 M PIC 9999. + + + PROCEDURE DIVISION. + + + INIT-MY-TABLE. + IF MAXIMUM-WEIGHT = 0 + MOVE 1 TO MAXIMUM-WEIGHT + INITIALIZE MY-TABLE + MOVE 0 TO MAXIMUM-WEIGHT + ELSE + INITIALIZE MY-TABLE + END-IF. + + + MAXIMUM-VALUE. + PERFORM INIT-MY-TABLE. + MOVE ZEROES TO MY-TABLE. + MOVE ZEROES TO MY-VARS. + INITIALIZE WS-RESULT. + SET INDX TO NO-OF-ITEMS. + IF NO-OF-ITEMS = 1 + IF WS-WEIGHT(1) > MAXIMUM-WEIGHT + EXIT PROGRAM + END-IF + END-IF. + SORT TABLEROW DESCENDING WS-VALUE WS-WEIGHT. + PERFORM VARYING I FROM 1 BY 1 UNTIL I > NO-OF-ITEMS + PERFORM VARYING J FROM 1 BY 1 UNTIL J > MAXIMUM-WEIGHT + IF WS-WEIGHT(I) > J THEN + COMPUTE C = I - 1 + IF C = 0 + MOVE 0 TO M(J,I) + ELSE + MOVE M(J, C) TO M(J,I) + END-IF + ELSE + COMPUTE B = J - WS-WEIGHT(I) + IF C > 0 AND B > 0 + COMPUTE A = WS-VALUE(I) + M(B, C) + COMPUTE M(J,I) = MAX(M(J, C), A) + ELSE + COMPUTE A = WS-VALUE(I) + COMPUTE M(J,I) = MAX(0, A) + END-IF + END-IF + END-PERFORM + END-PERFORM. + MOVE M(MAXIMUM-WEIGHT, NO-OF-ITEMS) TO WS-RESULT. diff --git a/exercises/practice/knapsack/bin/fetch-cobolcheck b/exercises/practice/knapsack/bin/fetch-cobolcheck new file mode 100755 index 00000000..64230fe2 --- /dev/null +++ b/exercises/practice/knapsack/bin/fetch-cobolcheck @@ -0,0 +1,63 @@ +#!/usr/bin/env bash + +# This file is a copy of the +# https://github.com/exercism/configlet/blob/main/scripts/fetch-configlet file. +# Please submit bugfixes/improvements to the above file to ensure that all tracks benefit from the changes. + +# set -eo pipefail + +readonly LATEST='https://api.github.com/repos/0xE282B0/cobol-check/releases/latest' + +case "$(uname)" in + Darwin*) os='mac' ;; + Linux*) os='linux' ;; + Windows*) os='windows' ;; + MINGW*) os='windows' ;; + MSYS_NT-*) os='windows' ;; + *) os='linux' ;; +esac + +case "${os}" in + windows*) ext='.exe' ;; + *) ext='' ;; +esac + +arch="$(uname -m)" + +curlopts=( + --silent + --show-error + --fail + --location + --retry 3 +) + +if [[ -n "${GITHUB_TOKEN}" ]]; then + curlopts+=(--header "authorization: Bearer ${GITHUB_TOKEN}") +fi + +suffix="${os}-${arch}${ext}" + +get_download_url() { + curl "${curlopts[@]}" --header 'Accept: application/vnd.github.v3+json' "${LATEST}" | + grep "\"browser_download_url\": \".*/download/.*/cobol-check.*${suffix}\"$" | + cut -d'"' -f4 +} + +main() { + if [[ -d ./bin ]]; then + output_dir="./bin" + elif [[ $PWD == */bin ]]; then + output_dir="$PWD" + else + echo "Error: no ./bin directory found. This script should be ran from a repo root." >&2 + return 1 + fi + + output_path="${output_dir}/cobolcheck${ext}" + download_url="$(get_download_url)" + curl "${curlopts[@]}" --output "${output_path}" "${download_url}" + chmod +x "${output_path}" +} + +main diff --git a/exercises/practice/knapsack/bin/fetch-cobolcheck.ps1 b/exercises/practice/knapsack/bin/fetch-cobolcheck.ps1 new file mode 100644 index 00000000..261f5f45 --- /dev/null +++ b/exercises/practice/knapsack/bin/fetch-cobolcheck.ps1 @@ -0,0 +1,28 @@ +# This file is a copy of the +# https://github.com/exercism/configlet/blob/main/scripts/fetch-configlet.ps1 file. +# Please submit bugfixes/improvements to the above file to ensure that all tracks +# benefit from the changes. + +$ErrorActionPreference = "Stop" +$ProgressPreference = "SilentlyContinue" + +$requestOpts = @{ + Headers = If ($env:GITHUB_TOKEN) { @{ Authorization = "Bearer ${env:GITHUB_TOKEN}" } } Else { @{ } } + MaximumRetryCount = 3 + RetryIntervalSec = 1 +} + +$arch = If ([Environment]::Is64BitOperatingSystem) { "amd64" } Else { "x86" } +$fileName = "cobol-check-windows-$arch.exe" + +Function Get-DownloadUrl { + $latestUrl = "https://api.github.com/repos/0xE282B0/cobol-check/releases/latest" + Invoke-RestMethod -Uri $latestUrl -PreserveAuthorizationOnRedirect @requestOpts + | Select-Object -ExpandProperty assets + | Where-Object { $_.browser_download_url -match $FileName } + | Select-Object -ExpandProperty browser_download_url +} + +$downloadUrl = Get-DownloadUrl +$outputFile = Join-Path -Path $PSScriptRoot -ChildPath "cobolcheck.exe" +Invoke-WebRequest -Uri $downloadUrl -OutFile $outputFile @requestOpts \ No newline at end of file diff --git a/exercises/practice/knapsack/config.properties b/exercises/practice/knapsack/config.properties new file mode 100644 index 00000000..2c5a6c8e --- /dev/null +++ b/exercises/practice/knapsack/config.properties @@ -0,0 +1,183 @@ +# Configuration settings for Cobol Check + +#--------------------------------------------------------------------------------------------------------------------- +# This configuration - echoed to console when Cobol Check is executed, for information only. +#--------------------------------------------------------------------------------------------------------------------- +config.loaded = production + +#--------------------------------------------------------------------------------------------------------------------- +# Prefix for field names and paragraph names in the test management code that cobol-check +# inserts into programs to be tested. The default is "UT-". If this conflicts with names +# in the programs to be tested, you can override it with a value you specify here. +# The value must be 3 characters or less. Cannot be empty. +#--------------------------------------------------------------------------------------------------------------------- +cobolcheck.prefix = UT- + +#--------------------------------------------------------------------------------------------------------------------- +# Tags written in the generated test code in the form of a comment, when a code injection starts and ends. +# Default is null, which will prevent the tags from appearing. Any other value will appear as comments +# surrounding the injected code. +# Examples: +# cobolcheck.injectedCodeTag.start = ###INJECT START### +# cobolcheck.injectedCodeTag.end = ###INJECT END### +#--------------------------------------------------------------------------------------------------------------------- +cobolcheck.injectedCodeTag.start = null +cobolcheck.injectedCodeTag.end = null + +#--------------------------------------------------------------------------------------------------------------------- +# A tag written at the start of entities stubbed by default. Recommended value-length <= 4. +# Note: The tag will appear only when cobolcheck stubs lines by default. +# This is the case for CALLs and batch file IO verbs. +# Default is null, which will prevent the tag from appearing. +#--------------------------------------------------------------------------------------------------------------------- +cobolcheck.stub.comment.tag = null + +#--------------------------------------------------------------------------------------------------------------------- +# Determines if cobolcheck should generate code, such that decimal point is comma. +# The default is "false". The value should be set to "true" if the compiler is set to +# read decimal points as commas. If the cobol source program sets DECIMAL-POINT IS COMMA, +# this configuration will be overwritten. +# Example: 1,385,481.00 (decimalPointIsComma = false) +# Example: 1.385.481,00 (decimalPointIsComma = true) +#--------------------------------------------------------------------------------------------------------------------- +cobolcheck.decimalPointIsComma = false + +#--------------------------------------------------------------------------------------------------------------------- +# If the source program contains rules as the first line follwed by CBL, the given value will be appended +# to this. +# If no CBL is found in source, it will be added along with the given value +# default is null, which will make no changes. +#--------------------------------------------------------------------------------------------------------------------- +cobolcheck.append.rules = null + +#--------------------------------------------------------------------------------------------------------------------- +# Path for the generated Cobol test code +# Default: ./ +#--------------------------------------------------------------------------------------------------------------------- +cobolcheck.test.program.path = ./ + +#--------------------------------------------------------------------------------------------------------------------- +# Suffix to append to the name of each program under test to produce the name of the corresponding +# test program that contains the merged test code. +# Example: For program ABCXYZ4, if suffix is T.CBL then the test program name will be ABCXYZ4T.CBL. +#--------------------------------------------------------------------------------------------------------------------- +cobolcheck.test.program.name = test.cob + +#--------------------------------------------------------------------------------------------------------------------- +# Path for the generated testsuite parse error log +# Default: ./ +#--------------------------------------------------------------------------------------------------------------------- +testsuite.parser.error.log.path = ./ + +#--------------------------------------------------------------------------------------------------------------------- +# Name of the generated testsuite parse error log file - with extension +# Default: ParserErrorLog.txt +#--------------------------------------------------------------------------------------------------------------------- +testsuite.parser.error.log.name = ParserErrorLog.txt + +#--------------------------------------------------------------------------------------------------------------------- +# The charset that cobolcheck will use when reading- and writing to files. +# See https://docs.oracle.com/javase/8/docs/technotes/guides/intl/encoding.doc.html, for a list of +# valid values. +# Default value for each OS is , which will use the default encoding for the OS. +#--------------------------------------------------------------------------------------------------------------------- +cobolcheck.file.encoding.linux = default +cobolcheck.file.encoding.macosx = default +cobolcheck.file.encoding.windows = default +cobolcheck.file.encoding.zos = default +cobolcheck.file.encoding.unix = default + +#--------------------------------------------------------------------------------------------------------------------- +# Sets permissions for all files generated by Cobol Check, for all users. +# If read, write and execute permissions are set, all users can perform said actions on all files +# that Cobol Check generates. +# Value can be any permutation of the letters: 'rwx' (read, write, execute) or none - meaning no permissions. +# Default value: rx +#--------------------------------------------------------------------------------------------------------------------- +generated.files.permission.all = rx + +#--------------------------------------------------------------------------------------------------------------------- +# Determines if Cobol Check should run the generated test program. +# Default is true. +# If set to false, Cobol Check will generate the code, but not run it. If more than one program +# is given as a command line option, the generated test file will be overwritten. Thus if set to false, +# only one program should be given at a time. +#--------------------------------------------------------------------------------------------------------------------- +cobolcheck.test.run = false + +#--------------------------------------------------------------------------------------------------------------------- +# These settings are to locate the application code under test in *your* Cobol project directory tree. +# Can be absolute path or relative to project root. +#--------------------------------------------------------------------------------------------------------------------- +application.source.directory = src +application.copybook.directory = cpy + +#--------------------------------------------------------------------------------------------------------------------- +# Location of test suite input file(s). This can also be passed on command-line option --test-suite-path. +#--------------------------------------------------------------------------------------------------------------------- +test.suite.directory = tst + +#--------------------------------------------------------------------------------------------------------------------- +# Location of test output. File extension is determined by a given format. +#--------------------------------------------------------------------------------------------------------------------- +test.results.file = output/testResults + +#--------------------------------------------------------------------------------------------------------------------- +# Determines the format of the test results written to the output file. +# Supported formats: txt, xml, html. +#--------------------------------------------------------------------------------------------------------------------- +test.results.format = txt + +#--------------------------------------------------------------------------------------------------------------------- +# Determines the format style of the test results written to the output file. +# The style controls the hierarchy and structure of data and naming of the 'object' that is written +# in a given format. Format: txt and style: directOutput are exclusive. txt cannot use any other style +# than directOutput, and directOutput cannot be used with any other format than txt. +# Other formats and styles can be used interchangeably. +# Supported styles: directOutput, JUnit, tableDocument, tableEmbed +#--------------------------------------------------------------------------------------------------------------------- +test.results.format.style = directOutput + +#--------------------------------------------------------------------------------------------------------------------- +# If application source filenames have a suffix, specify it here without the period or dot. +#--------------------------------------------------------------------------------------------------------------------- +application.source.filename.suffix = CBL,cbl,COB,cob + +#--------------------------------------------------------------------------------------------------------------------- +# If application copybook filenames have a suffix, specify it here without the period or dot +# e.g. application.copybook.filename.suffix = CBL +#--------------------------------------------------------------------------------------------------------------------- +application.copybook.filename.suffix = CBL,cbl,COB,cob + +#--------------------------------------------------------------------------------------------------------------------- +# Optional override of system default Locale for log messages and exception messages. +#--------------------------------------------------------------------------------------------------------------------- +#locale.language = ja +#locale.country = +#locale.variant = + +#--------------------------------------------------------------------------------------------------------------------- +# Cobol Check concatenates multiple test suite input files into a single file for the Generator. +# This is the relative or absolute path of the concatenated file. If not specified, the default +# is "./ALLTESTS" relative to the directory in which Cobol Check was started. +#--------------------------------------------------------------------------------------------------------------------- +concatenated.test.suites = ./ALLTESTS + +#--------------------------------------------------------------------------------------------------------------------- +# Shell scripts and JCL files for executing your test suites. +# Cobol Check will invoke one of these after creating the copy of the program under test that contains +# test code generated from your test suites. +# Unix and Mac OS X are both treated as unix. Most unices can run the linux script. +# Unix is the default. +# You can write custom scripts/JCL for your environment, for instance if you are using a different Cobol compiler. +# The first element in these names reflects the first few characters returned by Java's System.getProperty("os.name"). +# Cobol Check will select one of these entries based on which platform it thinks it's running on. +#--------------------------------------------------------------------------------------------------------------------- + +cobolcheck.script.directory = scripts +linux.process = linux_gnucobol_run_tests +osx.process = linux_gnucobol_run_tests +freebsd.process = linux_gnucobol_run_tests +windows.process = windows_gnucobol_run_tests.cmd +zos.process = +unix.process = linux_gnucobol_run_tests diff --git a/exercises/practice/knapsack/src/knapsack.cob b/exercises/practice/knapsack/src/knapsack.cob new file mode 100644 index 00000000..1b2aa12a --- /dev/null +++ b/exercises/practice/knapsack/src/knapsack.cob @@ -0,0 +1,17 @@ + IDENTIFICATION DIVISION. + PROGRAM-ID. KNAPSACK. + ENVIRONMENT DIVISION. + DATA DIVISION. + WORKING-STORAGE SECTION. + 01 WS-INPUTS. + 05 MAXIMUM-WEIGHT PIC 999. + 05 NO-OF-ITEMS PIC 99. + 05 KNAPSACKTABLE OCCURS 1 TO 20 DEPENDING ON NO-OF-ITEMS. + 10 WS-WEIGHT PIC 999. + 10 WS-VALUE PIC 999. + 01 WS-OUTPUTS. + 05 WS-RESULT PIC 99999. + + PROCEDURE DIVISION. + + MAXIMUM-VALUE. \ No newline at end of file diff --git a/exercises/practice/knapsack/test.ps1 b/exercises/practice/knapsack/test.ps1 new file mode 100644 index 00000000..b54fdc61 --- /dev/null +++ b/exercises/practice/knapsack/test.ps1 @@ -0,0 +1,21 @@ +$slug=Split-Path $PSScriptRoot -Leaf +$cobolcheck = "$PSScriptRoot\bin\cobolcheck.exe" +$cobolcheckCmd = Get-Command "cobolcheck.exe" -ErrorAction SilentlyContinue + +if ($cobolcheckCmd) { + $cobolcheck = $cobolcheckCmd.Path + Write-Output "Found cobolcheck, using $cobolcheck" +} elseif (![System.IO.File]::Exists("$cobolcheck")){ + Write-Output "Cobolcheck not found. Trying to fetch it." + & "$PSScriptRoot\bin\fetch-cobolcheck.ps1" +} + +Write-Output "Run cobolcheck." +Set-Location $PSScriptRoot + +Invoke-Expression "$cobolcheck -p $slug" +Invoke-Expression "cobc -xj test.cob" + +if ($Lastexitcode -ne 0) { + exit $Lastexitcode +} diff --git a/exercises/practice/knapsack/test.sh b/exercises/practice/knapsack/test.sh new file mode 100755 index 00000000..ed4a27a1 --- /dev/null +++ b/exercises/practice/knapsack/test.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env bash +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )/ +SLUG=${1:-$(basename "${SCRIPT_DIR}")} +COBOLCHECK=./bin/cobolcheck + +WHICH_COBOLCHECK=$(which cobolcheck) +if [[ $? -eq 0 ]] ; then + echo "Found cobolcheck, using $COBOLCHECK" + COBOLCHECK=$WHICH_COBOLCHECK +elif [ ! -f $SCRIPT_DIR/bin/cobolcheck ]; then + echo "Cobolcheck not found, try to fetch it." + cd $SCRIPT_DIR/bin/ + bash fetch-cobolcheck +fi +cd $SCRIPT_DIR +$COBOLCHECK -p $SLUG + +# compile and run +echo "COMPILE AND RUN TEST" +cobc -xj test.cob diff --git a/exercises/practice/knapsack/tst/knapsack/knapsack.cut b/exercises/practice/knapsack/tst/knapsack/knapsack.cut new file mode 100644 index 00000000..30f2c0be --- /dev/null +++ b/exercises/practice/knapsack/tst/knapsack/knapsack.cut @@ -0,0 +1,137 @@ +TestCase "no items" + MOVE 1 TO NO-OF-ITEMS + INITIALIZE KNAPSACKTABLE + MOVE 100 TO MAXIMUM-WEIGHT + PERFORM MAXIMUM-VALUE + EXPECT WS-RESULT = 0 + + +TestCase "one item, too heavy" + MOVE 1 TO NO-OF-ITEMS + INITIALIZE KNAPSACKTABLE + MOVE 10 TO MAXIMUM-WEIGHT + MOVE 100 TO WS-WEIGHT(1) + MOVE 1 TO WS-VALUE(1) + PERFORM MAXIMUM-VALUE + EXPECT WS-RESULT = 0 + + +TestCase "five items (cannot be greedy by weight)" + MOVE 5 TO NO-OF-ITEMS + INITIALIZE KNAPSACKTABLE + MOVE ZERO TO WS-RESULT + MOVE 10 TO MAXIMUM-WEIGHT + MOVE 2 TO WS-WEIGHT(1) + MOVE 5 TO WS-VALUE(1) + MOVE 2 TO WS-WEIGHT(2) + MOVE 5 TO WS-VALUE(2) + MOVE 2 TO WS-WEIGHT(3) + MOVE 5 TO WS-VALUE(3) + MOVE 2 TO WS-WEIGHT(4) + MOVE 5 TO WS-VALUE(4) + MOVE 10 TO WS-WEIGHT(5) + MOVE 21 TO WS-VALUE(5) + PERFORM MAXIMUM-VALUE + EXPECT WS-RESULT = 21 + + +TestCase "five items (cannot be greedy by value)" + MOVE 5 TO NO-OF-ITEMS + INITIALIZE KNAPSACKTABLE + MOVE ZERO TO WS-RESULT + MOVE 10 TO MAXIMUM-WEIGHT + MOVE 2 TO WS-WEIGHT(1) + MOVE 20 TO WS-VALUE(1) + MOVE 2 TO WS-WEIGHT(2) + MOVE 20 TO WS-VALUE(2) + MOVE 2 TO WS-WEIGHT(3) + MOVE 20 TO WS-VALUE(3) + MOVE 2 TO WS-WEIGHT(4) + MOVE 20 TO WS-VALUE(4) + MOVE 10 TO WS-WEIGHT(5) + MOVE 50 TO WS-VALUE(5) + PERFORM MAXIMUM-VALUE + EXPECT WS-RESULT = 80 + + +TestCase "example knapsack" + MOVE 4 TO NO-OF-ITEMS + INITIALIZE KNAPSACKTABLE + MOVE ZERO TO WS-RESULT + MOVE 10 TO MAXIMUM-WEIGHT + MOVE 5 TO WS-WEIGHT(1) + MOVE 10 TO WS-VALUE(1) + MOVE 4 TO WS-WEIGHT(2) + MOVE 40 TO WS-VALUE(2) + MOVE 6 TO WS-WEIGHT(3) + MOVE 30 TO WS-VALUE(3) + MOVE 4 TO WS-WEIGHT(4) + MOVE 50 TO WS-VALUE(4) + PERFORM MAXIMUM-VALUE + EXPECT WS-RESULT = 90 + + +TestCase "8 items" + MOVE 8 TO NO-OF-ITEMS + INITIALIZE KNAPSACKTABLE + MOVE ZERO TO WS-RESULT + MOVE 104 TO MAXIMUM-WEIGHT + MOVE 25 TO WS-WEIGHT(1) + MOVE 350 TO WS-VALUE(1) + MOVE 35 TO WS-WEIGHT(2) + MOVE 400 TO WS-VALUE(2) + MOVE 45 TO WS-WEIGHT(3) + MOVE 450 TO WS-VALUE(3) + MOVE 5 TO WS-WEIGHT(4) + MOVE 20 TO WS-VALUE(4) + MOVE 25 TO WS-WEIGHT(5) + MOVE 70 TO WS-VALUE(5) + MOVE 3 TO WS-WEIGHT(6) + MOVE 8 TO WS-VALUE(6) + MOVE 2 TO WS-WEIGHT(7) + MOVE 5 TO WS-VALUE(7) + MOVE 2 TO WS-WEIGHT(8) + MOVE 5 TO WS-VALUE(8) + PERFORM MAXIMUM-VALUE + EXPECT WS-RESULT = 900 + + +TestCase "15 items" + MOVE 15 TO NO-OF-ITEMS + INITIALIZE KNAPSACKTABLE + MOVE ZERO TO WS-RESULT + MOVE 750 TO MAXIMUM-WEIGHT + MOVE 70 TO WS-WEIGHT(1) + MOVE 135 TO WS-VALUE(1) + MOVE 73 TO WS-WEIGHT(2) + MOVE 139 TO WS-VALUE(2) + MOVE 77 TO WS-WEIGHT(3) + MOVE 149 TO WS-VALUE(3) + MOVE 80 TO WS-WEIGHT(4) + MOVE 150 TO WS-VALUE(4) + MOVE 82 TO WS-WEIGHT(5) + MOVE 156 TO WS-VALUE(5) + MOVE 87 TO WS-WEIGHT(6) + MOVE 163 TO WS-VALUE(6) + MOVE 90 TO WS-WEIGHT(7) + MOVE 173 TO WS-VALUE(7) + MOVE 94 TO WS-WEIGHT(8) + MOVE 184 TO WS-VALUE(8) + MOVE 98 TO WS-WEIGHT(9) + MOVE 192 TO WS-VALUE(9) + MOVE 106 TO WS-WEIGHT(10) + MOVE 201 TO WS-VALUE(10) + MOVE 110 TO WS-WEIGHT(11) + MOVE 210 TO WS-VALUE(11) + MOVE 113 TO WS-WEIGHT(12) + MOVE 214 TO WS-VALUE(12) + MOVE 115 TO WS-WEIGHT(13) + MOVE 221 TO WS-VALUE(13) + MOVE 118 TO WS-WEIGHT(14) + MOVE 229 TO WS-VALUE(14) + MOVE 120 TO WS-WEIGHT(15) + MOVE 240 TO WS-VALUE(15) + PERFORM MAXIMUM-VALUE + EXPECT WS-RESULT = 1458 + +