Skip to content

Commit

Permalink
Exercise: largest-series-product
Browse files Browse the repository at this point in the history
  • Loading branch information
loziniak committed Dec 4, 2021
1 parent d444933 commit 79a9cf0
Show file tree
Hide file tree
Showing 16 changed files with 399 additions and 2 deletions.
14 changes: 13 additions & 1 deletion _templates/practice-exercise/practice-exercise-test.red
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,19 @@ foreach test-case cases [
; arguments
append result-execution values-of test-case/input

result: do result-execution
result: try [
catch [
do result-execution
]
]

if error? result [
either result/type = 'user [
result: make map! reduce ['error result/arg1]
] [
print result
]
]

print [
pad/with test-case/description 30 #"."
Expand Down
5 changes: 5 additions & 0 deletions concepts/errors/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"blurb": "How to handle errors inside application. Different styles and when to apply them.",
"authors": ["loziniak"],
"contributors": []
}
3 changes: 3 additions & 0 deletions concepts/errors/about.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# About

> Provide more detailed information about the Concept for a student who has completed the corresponding Concept Exercise to learn from and refer back to.
3 changes: 3 additions & 0 deletions concepts/errors/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Introduction

> Provide a brief introduction to a student who has not yet completed the corresponding concept exercise.
2 changes: 2 additions & 0 deletions concepts/errors/links.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[
]
5 changes: 5 additions & 0 deletions concepts/loops/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"blurb": "How to run same code multiple times based on the data.",
"authors": ["loziniak"],
"contributors": []
}
3 changes: 3 additions & 0 deletions concepts/loops/about.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# About

> Provide more detailed information about the Concept for a student who has completed the corresponding Concept Exercise to learn from and refer back to.
3 changes: 3 additions & 0 deletions concepts/loops/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Introduction

> Provide a brief introduction to a student who has not yet completed the corresponding concept exercise.
2 changes: 2 additions & 0 deletions concepts/loops/links.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[
]
25 changes: 24 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
{
"slug": "strings",
"name": "String! type",
"name": "String! Type",
"uuid": "a936c324-9503-4acc-968f-ab18b8092ed0"
},
{
Expand All @@ -54,6 +54,11 @@
"name": "Conditional Functions",
"uuid": "c493b197-f0d1-4283-ab22-9d0a00d8caf9"
},
{
"slug": "loops",
"name": "Looping Functions",
"uuid": "9843fabf-ea9f-4c21-8442-220b7a922844"
},
{
"slug": "evaluation",
"name": "Code Evaluation Rules",
Expand All @@ -68,6 +73,11 @@
"slug": "maps",
"name": "Maps",
"uuid": "7ee99694-6f18-4234-92d4-122b9773ebac"
},
{
"slug": "errors",
"name": "Handling Errors",
"uuid": "d33f88db-be6a-4868-b6c6-4dca70a47cc9"
}
],
"tags": [
Expand Down Expand Up @@ -261,6 +271,19 @@
],
"difficulty": 4
},
{
"slug": "largest-series-product",
"name": "Largest Series Product",
"staus": "wip",
"uuid": "3bdc9858-390b-4d82-8669-663db97e6a0a",
"practices": [
"loops"
],
"prerequisites": [
"errors"
],
"difficulty": 5
},
{
"slug": "roman-numerals",
"name": "Roman Numerals",
Expand Down
14 changes: 14 additions & 0 deletions exercises/practice/largest-series-product/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# 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.
18 changes: 18 additions & 0 deletions exercises/practice/largest-series-product/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"blurb": "Given a string of digits, calculate the largest product for a contiguous substring of digits of length n.",
"authors": [
"loziniak"
],
"contributors": [],
"files": {
"solution": [
"largest-series-product.red"
],
"test": [
"largest-series-product-test.red"
],
"example": [
".meta/example.red"
]
}
}
43 changes: 43 additions & 0 deletions exercises/practice/largest-series-product/.meta/example.red
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
Red [
description: {"Largest Series Product" exercise solution for exercism platform}
author: "loziniak"
]

largest-product: function [
digits [string!]
span [integer!]
return: [integer!]
] [
if span > length? digits [
throw #(error: "span must be smaller than string length") ;-- 'throw / 'catch is one method of error control…
]
if span < 0 [
cause-error 'user 'message ["span must be greater than zero"] ;-- …and 'cause-error / 'try is another one.
]
integers: copy []
foreach digit digits [
if any [digit < #"0" digit > #"9"] [
cause-error 'user 'message ["digits input must only contain digits"]
]
append integers to integer! (digit - #"0")
]

max-product: either zero? span [1] [0]

len: length? integers
forall integers [
if (span - 1 + index? integers) > len [
break
]
;probe integers
product: 1
foreach int copy/part integers span [
product: product * int
]
if max-product < product [
max-product: product
]
]

max-product
]
47 changes: 47 additions & 0 deletions exercises/practice/largest-series-product/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
[canonical-tests]

# finds the largest product if span equals length
"7c82f8b7-e347-48ee-8a22-f672323324d4" = true

# can find the largest product of 2 with numbers in order
"88523f65-21ba-4458-a76a-b4aaf6e4cb5e" = true

# can find the largest product of 2
"f1376b48-1157-419d-92c2-1d7e36a70b8a" = true

# can find the largest product of 3 with numbers in order
"46356a67-7e02-489e-8fea-321c2fa7b4a4" = true

# can find the largest product of 3
"a2dcb54b-2b8f-4993-92dd-5ce56dece64a" = true

# can find the largest product of 5 with numbers in order
"673210a3-33cd-4708-940b-c482d7a88f9d" = true

# can get the largest product of a big number
"02acd5a6-3bbf-46df-8282-8b313a80a7c9" = true

# reports zero if the only digits are zero
"76dcc407-21e9-424c-a98e-609f269622b5" = true

# reports zero if all spans include zero
"6ef0df9f-52d4-4a5d-b210-f6fae5f20e19" = true

# rejects span longer than string length
"5d81aaf7-4f67-4125-bf33-11493cc7eab7" = true

# reports 1 for empty string and empty product (0 span)
"06bc8b90-0c51-4c54-ac22-3ec3893a079e" = true

# reports 1 for nonempty string and empty product (0 span)
"3ec0d92e-f2e2-4090-a380-70afee02f4c0" = true

# rejects empty string and nonzero span
"6d96c691-4374-4404-80ee-2ea8f3613dd4" = true

# rejects invalid character in digits
"7a38f2d6-3c35-45f6-8d6f-12e6e32d4d74" = true

# rejects negative span
"5fe3c0e5-a945-49f2-b584-f0814b4dd1ef" = true

Loading

0 comments on commit 79a9cf0

Please sign in to comment.