Skip to content

Commit

Permalink
adding largest-series-product
Browse files Browse the repository at this point in the history
  • Loading branch information
kapitaali committed Jun 6, 2024
1 parent 5e2a62d commit 0b196a1
Show file tree
Hide file tree
Showing 11 changed files with 575 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,14 @@
"prerequisites": [],
"difficulty": 4
},
{
"slug": "largest-series-product",
"name": "Largest Series Product",
"uuid": "bd8035be-13a9-43c6-a535-55a54176b08d",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "allergies",
"name": "Allergies",
Expand Down
18 changes: 18 additions & 0 deletions exercises/practice/largest-series-product/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Instructions

Given a string of digits, calculate the largest product for a contiguous
substring of digits of length n.

For example, for the input `'1027839564'`, the largest product for a
series of 3 digits is 270 (9 \* 5 \* 6), and the largest product for a
series of 5 digits is 7560 (7 \* 8 \* 3 \* 9 \* 5).

Note that these series are only required to occupy *adjacent positions*
in the input; the digits need not be *numerically consecutive*.

For the input `'73167176531330624919225119674426574742355349194934'`,
the largest product for a series of 6 digits is 23520.

For a series of zero digits, the largest product is 1 because 1 is the multiplicative identity.
(You don't need to know what a multiplicative identity is to solve this problem;
it just means that multiplying a number by 1 gives you the same number.)
26 changes: 26 additions & 0 deletions exercises/practice/largest-series-product/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"authors": [
"kapitaali"
],
"files": {
"solution": [
"src/largest-series-product.cob"
],
"test": [
"tst/largest-series-product/largest-series-product.cut"
],
"example": [
".meta/proof.ci.cob"
],
"invalidator": [
"test.ps1",
"test.sh",
"bin/fetch-cobolcheck",
"bin/fetch-cobolcheck.ps1",
"config.properties"
]
},
"blurb": "Given a string of digits, calculate the largest product for a contiguous substring of digits of length n.",
"source": "A variation on Problem 8 at Project Euler",
"source_url": "http://projecteuler.net/problem=8"
}
73 changes: 73 additions & 0 deletions exercises/practice/largest-series-product/.meta/proof.ci.cob
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
IDENTIFICATION DIVISION.
PROGRAM-ID. LARGEST-SERIES-PRODUCT.
AUTHOR. kapitaali.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-INPUTVARS.
05 WS-SPAN PIC S999.
05 WS-DIGITS PIC X(60).
01 WS-OUTPUTVARS.
05 WS-RESULT PIC 9(9).
05 WS-ERROR PIC X(40).

01 MY-WORKVARS.
05 X PIC 9.
05 A PIC 9999.
05 B PIC 9(9).
05 D PIC 9999.
05 C-LEN PIC 9999.
05 STR PIC 9(9).

PROCEDURE DIVISION.


CHECK-FOR-CHARS.
IF WS-DIGITS IS NOT NUMERIC
MOVE "digits input must only contain digits" TO WS-ERROR
EXIT PROGRAM
END-IF.


COUNT-PRODUCT.
MOVE 0 TO WS-RESULT.
COMPUTE C-LEN = C-LEN - WS-SPAN + 1.
PERFORM VARYING A FROM 1 BY 1 UNTIL A > C-LEN
MOVE FUNCTION NUMVAL(WS-DIGITS(A:WS-SPAN)) TO STR
MOVE FUNCTION REVERSE(STR) TO STR
MOVE 1 TO B
PERFORM VARYING D FROM 1 BY 1 UNTIL D > WS-SPAN
MOVE STR(D:1) TO X
COMPUTE B = B * X
END-PERFORM
IF B > WS-RESULT
MOVE B TO WS-RESULT
END-IF
END-PERFORM.


LARGEST-PRODUCT.
PERFORM CHECK-FOR-CHARS.
MOVE FUNCTION LENGTH(FUNCTION TRIM(WS-DIGITS TRAILING))
TO C-LEN.
MOVE 1 TO WS-RESULT.
EVALUATE WS-SPAN
WHEN > C-LEN
MOVE "span must be smaller than string length"
TO WS-ERROR
WHEN < 0
MOVE "span must not be negative" TO WS-ERROR
WHEN 0
MOVE 1 TO WS-RESULT
WHEN = C-LEN
MOVE 1 TO B
MOVE FUNCTION NUMVAL(WS-DIGITS) TO STR
MOVE FUNCTION REVERSE(STR) TO STR
PERFORM VARYING D FROM 1 BY 1 UNTIL D > WS-SPAN
MOVE STR(D:1) TO X
COMPUTE B = B * X
END-PERFORM
MOVE B TO WS-RESULT
WHEN OTHER
PERFORM COUNT-PRODUCT
END-EVALUATE.
63 changes: 63 additions & 0 deletions exercises/practice/largest-series-product/bin/fetch-cobolcheck
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions exercises/practice/largest-series-product/bin/fetch-cobolcheck.ps1
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit 0b196a1

Please sign in to comment.