Add largest-series-product exercise (#152)
This commit is contained in:
@@ -316,6 +316,14 @@
|
||||
"practices": [],
|
||||
"prerequisites": [],
|
||||
"difficulty": 4
|
||||
},
|
||||
{
|
||||
"slug": "largest-series-product",
|
||||
"name": "Largest Series Product",
|
||||
"uuid": "16a61395-cffb-45ff-85ea-ef78b6f1859f",
|
||||
"practices": [],
|
||||
"prerequisites": [],
|
||||
"difficulty": 4
|
||||
}
|
||||
]
|
||||
},
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
# Instructions
|
||||
|
||||
Your task is to look for patterns in the long sequence of digits in the encrypted signal.
|
||||
|
||||
The technique you're going to use here is called the largest series product.
|
||||
|
||||
Let's define a few terms, first.
|
||||
|
||||
- **input**: the sequence of digits that you need to analyze
|
||||
- **series**: a sequence of adjacent digits (those that are next to each other) that is contained within the input
|
||||
- **span**: how many digits long each series is
|
||||
- **product**: what you get when you multiply numbers together
|
||||
|
||||
Let's work through an example, with the input `"63915"`.
|
||||
|
||||
- To form a series, take adjacent digits in the original input.
|
||||
- If you are working with a span of `3`, there will be three possible series:
|
||||
- `"639"`
|
||||
- `"391"`
|
||||
- `"915"`
|
||||
- Then we need to calculate the product of each series:
|
||||
- The product of the series `"639"` is 162 (`6 × 3 × 9 = 162`)
|
||||
- The product of the series `"391"` is 27 (`3 × 9 × 1 = 27`)
|
||||
- The product of the series `"915"` is 45 (`9 × 1 × 5 = 45`)
|
||||
- 162 is bigger than both 27 and 45, so the largest series product of `"63915"` is from the series `"639"`.
|
||||
So the answer is **162**.
|
||||
@@ -0,0 +1,5 @@
|
||||
# Introduction
|
||||
|
||||
You work for a government agency that has intercepted a series of encrypted communication signals from a group of bank robbers.
|
||||
The signals contain a long sequence of digits.
|
||||
Your team needs to use various digital signal processing techniques to analyze the signals and identify any patterns that may indicate the planning of a heist.
|
||||
19
exercises/practice/largest-series-product/.meta/config.json
Normal file
19
exercises/practice/largest-series-product/.meta/config.json
Normal file
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"authors": [
|
||||
"erikschierboom"
|
||||
],
|
||||
"files": {
|
||||
"solution": [
|
||||
"largest-series-product.8th"
|
||||
],
|
||||
"test": [
|
||||
"test.8th"
|
||||
],
|
||||
"example": [
|
||||
".meta/example.8th"
|
||||
]
|
||||
},
|
||||
"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": "https://projecteuler.net/problem=8"
|
||||
}
|
||||
21
exercises/practice/largest-series-product/.meta/example.8th
Normal file
21
exercises/practice/largest-series-product/.meta/example.8th
Normal file
@@ -0,0 +1,21 @@
|
||||
: digit? \ n -- T
|
||||
'0 '9 between
|
||||
;
|
||||
|
||||
: digits \ s -- a
|
||||
( dup digit? if '0 n:- a:push else 2drop null break ;then ) a:new s:reduce
|
||||
;
|
||||
|
||||
: slices \ a n -- a
|
||||
( ' n:* 1 a:reduce ) rot 1 a:map+
|
||||
;
|
||||
|
||||
: largest \ a -- n
|
||||
' n:cmp a:sort -1 a:_@
|
||||
;
|
||||
|
||||
: largest-product \ s n -- n
|
||||
swap digits null? if nip ;then
|
||||
slices null? if drop 1 ;then
|
||||
largest
|
||||
;
|
||||
60
exercises/practice/largest-series-product/.meta/tests.toml
Normal file
60
exercises/practice/largest-series-product/.meta/tests.toml
Normal file
@@ -0,0 +1,60 @@
|
||||
# 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.
|
||||
|
||||
[7c82f8b7-e347-48ee-8a22-f672323324d4]
|
||||
description = "finds the largest product if span equals length"
|
||||
|
||||
[88523f65-21ba-4458-a76a-b4aaf6e4cb5e]
|
||||
description = "can find the largest product of 2 with numbers in order"
|
||||
|
||||
[f1376b48-1157-419d-92c2-1d7e36a70b8a]
|
||||
description = "can find the largest product of 2"
|
||||
|
||||
[46356a67-7e02-489e-8fea-321c2fa7b4a4]
|
||||
description = "can find the largest product of 3 with numbers in order"
|
||||
|
||||
[a2dcb54b-2b8f-4993-92dd-5ce56dece64a]
|
||||
description = "can find the largest product of 3"
|
||||
|
||||
[673210a3-33cd-4708-940b-c482d7a88f9d]
|
||||
description = "can find the largest product of 5 with numbers in order"
|
||||
|
||||
[02acd5a6-3bbf-46df-8282-8b313a80a7c9]
|
||||
description = "can get the largest product of a big number"
|
||||
|
||||
[76dcc407-21e9-424c-a98e-609f269622b5]
|
||||
description = "reports zero if the only digits are zero"
|
||||
|
||||
[6ef0df9f-52d4-4a5d-b210-f6fae5f20e19]
|
||||
description = "reports zero if all spans include zero"
|
||||
|
||||
[5d81aaf7-4f67-4125-bf33-11493cc7eab7]
|
||||
description = "rejects span longer than string length"
|
||||
|
||||
[06bc8b90-0c51-4c54-ac22-3ec3893a079e]
|
||||
description = "reports 1 for empty string and empty product (0 span)"
|
||||
|
||||
[3ec0d92e-f2e2-4090-a380-70afee02f4c0]
|
||||
description = "reports 1 for nonempty string and empty product (0 span)"
|
||||
|
||||
[6d96c691-4374-4404-80ee-2ea8f3613dd4]
|
||||
description = "rejects empty string and nonzero span"
|
||||
|
||||
[7a38f2d6-3c35-45f6-8d6f-12e6e32d4d74]
|
||||
description = "rejects invalid character in digits"
|
||||
|
||||
[5fe3c0e5-a945-49f2-b584-f0814b4dd1ef]
|
||||
description = "rejects negative span"
|
||||
include = false
|
||||
|
||||
[c859f34a-9bfe-4897-9c2f-6d7f8598e7f0]
|
||||
description = "rejects negative span"
|
||||
reimplements = "5fe3c0e5-a945-49f2-b584-f0814b4dd1ef"
|
||||
@@ -0,0 +1,3 @@
|
||||
: largest-product \ s n -- n
|
||||
|
||||
;
|
||||
162
exercises/practice/largest-series-product/libs/exercism/test
Normal file
162
exercises/practice/largest-series-product/libs/exercism/test
Normal file
@@ -0,0 +1,162 @@
|
||||
needs console/loaded
|
||||
|
||||
\ -----------------------------------------------------------------
|
||||
|
||||
ns: test
|
||||
|
||||
-1 var, test-count
|
||||
var tests-passed
|
||||
var tests-failed
|
||||
var tests-skipped
|
||||
true var, run-test
|
||||
|
||||
\ Some utility words
|
||||
|
||||
: array-insens-dot \ x -- \\ removes leading space on stringified arrays
|
||||
dup >kind ns:a n:=
|
||||
if
|
||||
>s s:trim
|
||||
then
|
||||
.
|
||||
;
|
||||
|
||||
: test-passed \ s x x -- \\ test name, expected value, actual value
|
||||
2drop
|
||||
1 tests-passed n:+!
|
||||
con:green con:onBlack . space " ... OK" . con:white con:onBlack cr
|
||||
;
|
||||
|
||||
: test-skipped \ s --
|
||||
1 tests-skipped n:+!
|
||||
con:cyan con:onBlack . space " ... SKIPPED" . con:white con:onBlack cr
|
||||
;
|
||||
|
||||
: test-failed \ s x x -- \\ test name, expected value, actual value
|
||||
1 tests-failed n:+!
|
||||
rot
|
||||
con:red con:onBlack . space " ... FAIL" . con:white con:onBlack cr
|
||||
" Actual: «" . array-insens-dot "»" . cr
|
||||
" Expected: «" . array-insens-dot "»" . cr cr
|
||||
;
|
||||
|
||||
: isword? \ x -- x f
|
||||
dup >kind ns:w n:=
|
||||
;
|
||||
|
||||
: run-test? \ -- T
|
||||
run-test @ if true else "RUN_ALL_TESTS" getenv n:>bool then
|
||||
;
|
||||
|
||||
\ Num passed + num skipped + num failed should == num tests
|
||||
: all-tests-run? \ -- T
|
||||
tests-passed @ tests-skipped @ tests-failed @ n:+ n:+
|
||||
test-count @ n:=
|
||||
;
|
||||
|
||||
\ returns true if x is a date, false otherwise
|
||||
: date? \ x -- x T
|
||||
dup >kind ns:d n:=
|
||||
;
|
||||
|
||||
\ adapted from 8th forum -- https://8th-dev.com/forum/index.php/topic,2745.0.html
|
||||
: eq? \ x x -- T
|
||||
\ are the items the same kind?
|
||||
2dup >kind swap >kind n:=
|
||||
!if 2drop false ;then
|
||||
|
||||
\ same kind: try different comparators
|
||||
number? if n:= ;then
|
||||
string? if s:= ;then
|
||||
array? if ' eq? a:= 2nip ;then
|
||||
map? if ' eq? m:= 2nip ;then
|
||||
date? if d:= ;then
|
||||
|
||||
\ otherwise fall back to 'lazy evaluation'
|
||||
l: =
|
||||
;
|
||||
|
||||
: check-depth \ ... n -- ...
|
||||
dup>r
|
||||
n:1+ depth n:=
|
||||
!if
|
||||
con:red con:onBlack
|
||||
"PANIC: expected stack depth to be " . r> . cr
|
||||
"Stack is:" . cr
|
||||
.s cr
|
||||
255 die
|
||||
then
|
||||
rdrop
|
||||
;
|
||||
|
||||
\ -----------------------------------------------------------------
|
||||
|
||||
\ status report at end of run
|
||||
( all-tests-run?
|
||||
!if con:red con:onBlack "... FAIL - not all tests completed" . con:white con:onBlack cr then
|
||||
) onexit
|
||||
|
||||
\ Print a summary of the tests run
|
||||
( con:white con:onBlack
|
||||
test-count @ . space "tests planned - " .
|
||||
tests-passed @ . space "passed - " .
|
||||
tests-skipped @ . space "skipped - " .
|
||||
tests-failed @ . space "failed" . cr
|
||||
) onexit
|
||||
|
||||
\ -----------------------------------------------------------------
|
||||
\ The public-facing words
|
||||
\ -----------------------------------------------------------------
|
||||
|
||||
: equal? \ s x w -- | s w x --
|
||||
run-test? !if 2drop test-skipped ;; then
|
||||
isword? !if swap then
|
||||
w:exec
|
||||
3 check-depth
|
||||
2dup \ so test-failed can show actual and expected
|
||||
eq? if test-passed else test-failed then
|
||||
;
|
||||
|
||||
: true? \ s w --
|
||||
run-test? !if drop test-skipped ;; then
|
||||
w:exec
|
||||
2 check-depth
|
||||
true swap dup \ so test-failed can show actual and expected
|
||||
if test-passed else test-failed then
|
||||
;
|
||||
|
||||
: false? \ s w --
|
||||
run-test? !if drop test-skipped ;; then
|
||||
w:exec
|
||||
2 check-depth
|
||||
false swap dup \ so test-failed can show actual and expected
|
||||
!if test-passed else test-failed then
|
||||
;
|
||||
|
||||
: null? \ s w --
|
||||
run-test? !if drop test-skipped ;; then
|
||||
w:exec
|
||||
2 check-depth
|
||||
null swap dup \ so test-failed can show actual and expected
|
||||
G:null? nip if test-passed else test-failed then
|
||||
;
|
||||
|
||||
: SKIP-REST-OF-TESTS false run-test ! ;
|
||||
|
||||
: tests \ n --
|
||||
test-count !
|
||||
;
|
||||
|
||||
\ Set the exit status:
|
||||
\ 0 = all OK
|
||||
\ 1 = not all tests were run (some error occurred)
|
||||
\ 2 = some tests failed
|
||||
: end-of-tests \ --
|
||||
all-tests-run?
|
||||
if
|
||||
tests-failed @ 0 n:= if 0 else 2 then
|
||||
else
|
||||
1
|
||||
then
|
||||
die
|
||||
;
|
||||
|
||||
80
exercises/practice/largest-series-product/test.8th
Normal file
80
exercises/practice/largest-series-product/test.8th
Normal file
@@ -0,0 +1,80 @@
|
||||
"largest-series-product.8th" f:include
|
||||
needs exercism/test
|
||||
with: test
|
||||
15 tests
|
||||
|
||||
"finds the largest product if span equals length"
|
||||
( "29" 2 largest-product )
|
||||
18
|
||||
equal?
|
||||
|
||||
SKIP-REST-OF-TESTS
|
||||
|
||||
"can find the largest product of 2 with numbers in order"
|
||||
( "0123456789" 2 largest-product )
|
||||
72
|
||||
equal?
|
||||
|
||||
"can find the largest product of 2"
|
||||
( "576802143" 2 largest-product )
|
||||
48
|
||||
equal?
|
||||
|
||||
"can find the largest product of 3 with numbers in order"
|
||||
( "0123456789" 3 largest-product )
|
||||
504
|
||||
equal?
|
||||
|
||||
"can find the largest product of 3"
|
||||
( "1027839564" 3 largest-product )
|
||||
270
|
||||
equal?
|
||||
|
||||
"can find the largest product of 5 with numbers in order"
|
||||
( "0123456789" 5 largest-product )
|
||||
15120
|
||||
equal?
|
||||
|
||||
"can get the largest product of a big number"
|
||||
( "73167176531330624919225119674426574742355349194934" 6 largest-product )
|
||||
23520
|
||||
equal?
|
||||
|
||||
"reports zero if the only digits are zero"
|
||||
( "0000" 2 largest-product )
|
||||
0
|
||||
equal?
|
||||
|
||||
"reports zero if all spans include zero"
|
||||
( "99099" 3 largest-product )
|
||||
0
|
||||
equal?
|
||||
|
||||
"rejects span longer than string length"
|
||||
( "123" 4 largest-product )
|
||||
null?
|
||||
|
||||
"reports 1 for empty string and empty product (0 span)"
|
||||
( "" 0 largest-product )
|
||||
1
|
||||
equal?
|
||||
|
||||
"reports 1 for nonempty string and empty product (0 span)"
|
||||
( "123" 0 largest-product )
|
||||
1
|
||||
equal?
|
||||
|
||||
"rejects empty string and nonzero span"
|
||||
( "" 1 largest-product )
|
||||
null?
|
||||
|
||||
"rejects invalid character in digits"
|
||||
( "1234a5" 2 largest-product )
|
||||
null?
|
||||
|
||||
"rejects negative span"
|
||||
( "12345" -1 largest-product )
|
||||
null?
|
||||
|
||||
end-of-tests
|
||||
;with
|
||||
Reference in New Issue
Block a user