Skip to content

Commit 79a9cf0

Browse files
committed
Exercise: largest-series-product
1 parent d444933 commit 79a9cf0

File tree

16 files changed

+399
-2
lines changed

16 files changed

+399
-2
lines changed

_templates/practice-exercise/practice-exercise-test.red

+13-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,19 @@ foreach test-case cases [
2828
; arguments
2929
append result-execution values-of test-case/input
3030

31-
result: do result-execution
31+
result: try [
32+
catch [
33+
do result-execution
34+
]
35+
]
36+
37+
if error? result [
38+
either result/type = 'user [
39+
result: make map! reduce ['error result/arg1]
40+
] [
41+
print result
42+
]
43+
]
3244

3345
print [
3446
pad/with test-case/description 30 #"."

concepts/errors/.meta/config.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"blurb": "How to handle errors inside application. Different styles and when to apply them.",
3+
"authors": ["loziniak"],
4+
"contributors": []
5+
}

concepts/errors/about.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# About
2+
3+
> Provide more detailed information about the Concept for a student who has completed the corresponding Concept Exercise to learn from and refer back to.

concepts/errors/introduction.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Introduction
2+
3+
> Provide a brief introduction to a student who has not yet completed the corresponding concept exercise.

concepts/errors/links.json

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[
2+
]

concepts/loops/.meta/config.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"blurb": "How to run same code multiple times based on the data.",
3+
"authors": ["loziniak"],
4+
"contributors": []
5+
}

concepts/loops/about.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# About
2+
3+
> Provide more detailed information about the Concept for a student who has completed the corresponding Concept Exercise to learn from and refer back to.

concepts/loops/introduction.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Introduction
2+
3+
> Provide a brief introduction to a student who has not yet completed the corresponding concept exercise.

concepts/loops/links.json

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[
2+
]

config.json

+24-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
},
3232
{
3333
"slug": "strings",
34-
"name": "String! type",
34+
"name": "String! Type",
3535
"uuid": "a936c324-9503-4acc-968f-ab18b8092ed0"
3636
},
3737
{
@@ -54,6 +54,11 @@
5454
"name": "Conditional Functions",
5555
"uuid": "c493b197-f0d1-4283-ab22-9d0a00d8caf9"
5656
},
57+
{
58+
"slug": "loops",
59+
"name": "Looping Functions",
60+
"uuid": "9843fabf-ea9f-4c21-8442-220b7a922844"
61+
},
5762
{
5863
"slug": "evaluation",
5964
"name": "Code Evaluation Rules",
@@ -68,6 +73,11 @@
6873
"slug": "maps",
6974
"name": "Maps",
7075
"uuid": "7ee99694-6f18-4234-92d4-122b9773ebac"
76+
},
77+
{
78+
"slug": "errors",
79+
"name": "Handling Errors",
80+
"uuid": "d33f88db-be6a-4868-b6c6-4dca70a47cc9"
7181
}
7282
],
7383
"tags": [
@@ -261,6 +271,19 @@
261271
],
262272
"difficulty": 4
263273
},
274+
{
275+
"slug": "largest-series-product",
276+
"name": "Largest Series Product",
277+
"staus": "wip",
278+
"uuid": "3bdc9858-390b-4d82-8669-663db97e6a0a",
279+
"practices": [
280+
"loops"
281+
],
282+
"prerequisites": [
283+
"errors"
284+
],
285+
"difficulty": 5
286+
},
264287
{
265288
"slug": "roman-numerals",
266289
"name": "Roman Numerals",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Instructions
2+
3+
Given a string of digits, calculate the largest product for a contiguous
4+
substring of digits of length n.
5+
6+
For example, for the input `'1027839564'`, the largest product for a
7+
series of 3 digits is 270 (9 \* 5 \* 6), and the largest product for a
8+
series of 5 digits is 7560 (7 \* 8 \* 3 \* 9 \* 5).
9+
10+
Note that these series are only required to occupy *adjacent positions*
11+
in the input; the digits need not be *numerically consecutive*.
12+
13+
For the input `'73167176531330624919225119674426574742355349194934'`,
14+
the largest product for a series of 6 digits is 23520.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"blurb": "Given a string of digits, calculate the largest product for a contiguous substring of digits of length n.",
3+
"authors": [
4+
"loziniak"
5+
],
6+
"contributors": [],
7+
"files": {
8+
"solution": [
9+
"largest-series-product.red"
10+
],
11+
"test": [
12+
"largest-series-product-test.red"
13+
],
14+
"example": [
15+
".meta/example.red"
16+
]
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
Red [
2+
description: {"Largest Series Product" exercise solution for exercism platform}
3+
author: "loziniak"
4+
]
5+
6+
largest-product: function [
7+
digits [string!]
8+
span [integer!]
9+
return: [integer!]
10+
] [
11+
if span > length? digits [
12+
throw #(error: "span must be smaller than string length") ;-- 'throw / 'catch is one method of error control…
13+
]
14+
if span < 0 [
15+
cause-error 'user 'message ["span must be greater than zero"] ;-- …and 'cause-error / 'try is another one.
16+
]
17+
integers: copy []
18+
foreach digit digits [
19+
if any [digit < #"0" digit > #"9"] [
20+
cause-error 'user 'message ["digits input must only contain digits"]
21+
]
22+
append integers to integer! (digit - #"0")
23+
]
24+
25+
max-product: either zero? span [1] [0]
26+
27+
len: length? integers
28+
forall integers [
29+
if (span - 1 + index? integers) > len [
30+
break
31+
]
32+
;probe integers
33+
product: 1
34+
foreach int copy/part integers span [
35+
product: product * int
36+
]
37+
if max-product < product [
38+
max-product: product
39+
]
40+
]
41+
42+
max-product
43+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
[canonical-tests]
2+
3+
# finds the largest product if span equals length
4+
"7c82f8b7-e347-48ee-8a22-f672323324d4" = true
5+
6+
# can find the largest product of 2 with numbers in order
7+
"88523f65-21ba-4458-a76a-b4aaf6e4cb5e" = true
8+
9+
# can find the largest product of 2
10+
"f1376b48-1157-419d-92c2-1d7e36a70b8a" = true
11+
12+
# can find the largest product of 3 with numbers in order
13+
"46356a67-7e02-489e-8fea-321c2fa7b4a4" = true
14+
15+
# can find the largest product of 3
16+
"a2dcb54b-2b8f-4993-92dd-5ce56dece64a" = true
17+
18+
# can find the largest product of 5 with numbers in order
19+
"673210a3-33cd-4708-940b-c482d7a88f9d" = true
20+
21+
# can get the largest product of a big number
22+
"02acd5a6-3bbf-46df-8282-8b313a80a7c9" = true
23+
24+
# reports zero if the only digits are zero
25+
"76dcc407-21e9-424c-a98e-609f269622b5" = true
26+
27+
# reports zero if all spans include zero
28+
"6ef0df9f-52d4-4a5d-b210-f6fae5f20e19" = true
29+
30+
# rejects span longer than string length
31+
"5d81aaf7-4f67-4125-bf33-11493cc7eab7" = true
32+
33+
# reports 1 for empty string and empty product (0 span)
34+
"06bc8b90-0c51-4c54-ac22-3ec3893a079e" = true
35+
36+
# reports 1 for nonempty string and empty product (0 span)
37+
"3ec0d92e-f2e2-4090-a380-70afee02f4c0" = true
38+
39+
# rejects empty string and nonzero span
40+
"6d96c691-4374-4404-80ee-2ea8f3613dd4" = true
41+
42+
# rejects invalid character in digits
43+
"7a38f2d6-3c35-45f6-8d6f-12e6e32d4d74" = true
44+
45+
# rejects negative span
46+
"5fe3c0e5-a945-49f2-b584-f0814b4dd1ef" = true
47+

0 commit comments

Comments
 (0)