Skip to content

Commit

Permalink
Add series exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
keiravillekode committed May 1, 2024
1 parent 479ca6e commit f51fb73
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,14 @@
"prerequisites": [],
"difficulty": 3
},
{
"slug": "series",
"name": "Series",
"uuid": "a0477a3c-547e-4cde-a23e-a917fa408284",
"practices": [],
"prerequisites": [],
"difficulty": 3
},
{
"slug": "etl",
"name": "ETL",
Expand Down
19 changes: 19 additions & 0 deletions exercises/practice/series/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Instructions

Given a string of digits, output all the contiguous substrings of length `n` in that string in the order that they appear.

For example, the string "49142" has the following 3-digit series:

- "491"
- "914"
- "142"

And the following 4-digit series:

- "4914"
- "9142"

And if you ask for a 6-digit series from a 5-digit string, you deserve whatever you get.

Note that these series are only required to occupy _adjacent positions_ in the input;
the digits need not be _numerically consecutive_.
19 changes: 19 additions & 0 deletions exercises/practice/series/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"series.v"
],
"test": [
"run_test.v"
],
"example": [
".meta/example.v"
]
},
"blurb": "Given a string of digits, output all the contiguous substrings of length `n` in that string.",
"source": "A subset of the Problem 8 at Project Euler",
"source_url": "https://projecteuler.net/problem=8"
}
21 changes: 21 additions & 0 deletions exercises/practice/series/.meta/example.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module main

fn slices(series string, slice_length int) ![]string {
if series.len == 0 {
return error('series cannot be empty')
}
if slice_length > series.len {
return error('slice length cannot be greater than series length')
}
if slice_length == 0 {
return error('slice length cannot be zero')
}
if slice_length < 0 {
return error('slice length cannot be negative')
}
mut result := []string{}
for i in 0 .. (series.len - slice_length + 1) {
result << series[i..(i + slice_length)]
}
return result
}
36 changes: 36 additions & 0 deletions exercises/practice/series/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# This is an auto-generated file. Regular comments will be removed when this
# file is regenerated. Regenerating will not touch any manually added keys,
# so comments can be added in a "comment" key.

[7ae7a46a-d992-4c2a-9c15-a112d125ebad]
description = "slices of one from one"

[3143b71d-f6a5-4221-aeae-619f906244d2]
description = "slices of one from two"

[dbb68ff5-76c5-4ccd-895a-93dbec6d5805]
description = "slices of two"

[19bbea47-c987-4e11-a7d1-e103442adf86]
description = "slices of two overlap"

[8e17148d-ba0a-4007-a07f-d7f87015d84c]
description = "slices can include duplicates"

[bd5b085e-f612-4f81-97a8-6314258278b0]
description = "slices of a long series"

[6d235d85-46cf-4fae-9955-14b6efef27cd]
description = "slice length is too large"

[d7957455-346d-4e47-8e4b-87ed1564c6d7]
description = "slice length is way too large"

[d34004ad-8765-4c09-8ba1-ada8ce776806]
description = "slice length cannot be zero"

[10ab822d-8410-470a-a85d-23fbeb549e54]
description = "slice length cannot be negative"

[c7ed0812-0e4b-4bf3-99c4-28cbbfc246a2]
description = "empty series is invalid"
71 changes: 71 additions & 0 deletions exercises/practice/series/run_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
module main

fn test_slices_of_one_from_one() {
expected := ['1']
assert slices('1', 1)! == expected
}

fn test_slices_of_one_from_two() {
expected := ['1', '2']
assert slices('12', 1)! == expected
}

fn test_slices_of_two() {
expected := ['35']
assert slices('35', 2)! == expected
}

fn test_slices_of_two_overlap() {
expected := ['91', '14', '42']
assert slices('9142', 2)! == expected
}

fn test_slices_can_include_duplicates() {
expected := ['777', '777', '777', '777']
assert slices('777777', 3)! == expected
}

fn test_slices_of_a_long_series() {
expected := ['91849', '18493', '84939', '49390', '93904', '39042', '90424', '04243']
assert slices('918493904243', 5)! == expected
}

fn test_slice_length_is_too_large() {
if res := slices('12345', 6) {
assert false, 'slice length too large should return an error'
} else {
assert err.msg() == 'slice length cannot be greater than series length'
}
}

fn test_slice_length_is_way_too_large() {
if res := slices('12345', 42) {
assert false, 'slice length way too large should return an error'
} else {
assert err.msg() == 'slice length cannot be greater than series length'
}
}

fn test_slice_length_cannot_be_zero() {
if res := slices('12345', 0) {
assert false, 'slice length cannot be zero should return an error'
} else {
assert err.msg() == 'slice length cannot be zero'
}
}

fn test_slice_length_cannot_be_negative() {
if res := slices('123', -1) {
assert false, 'slice length cannot be negative should return an error'
} else {
assert err.msg() == 'slice length cannot be negative'
}
}

fn test_empty_series_is_invalid() {
if res := slices('', 1) {
assert false, 'empty series invalid should return an error'
} else {
assert err.msg() == 'series cannot be empty'
}
}
4 changes: 4 additions & 0 deletions exercises/practice/series/series.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module main

fn slices(series string, slice_length int) ![]string {
}

0 comments on commit f51fb73

Please sign in to comment.