Skip to content

Commit 1decab5

Browse files
authored
Add series (#111)
1 parent eda5a82 commit 1decab5

10 files changed

Lines changed: 200 additions & 0 deletions

File tree

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,14 @@
498498
"prerequisites": [],
499499
"difficulty": 4
500500
},
501+
{
502+
"slug": "series",
503+
"name": "Series",
504+
"uuid": "9206db65-e0c9-4ea5-afc2-23910ea6dca2",
505+
"practices": [],
506+
"prerequisites": [],
507+
"difficulty": 4
508+
},
501509
{
502510
"slug": "sieve",
503511
"name": "Sieve",

exercises/practice/series/.busted

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
return {
2+
default = {
3+
ROOT = { '.' }
4+
}
5+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# dummy
2+
3+
## MoonScript-specific Instructions
4+
5+
This exercise requires you to implement the `slices` function as an **iterator**.
6+
Iterators are described in the manual in "The generic for loop" under the [For Statement][lua-for] section.
7+
8+
There is a thorough tutorial at [Lua Iterators][tutorialspoint].
9+
10+
11+
[lua-for]: https://www.lua.org/manual/5.4/manual.html#3.3.5
12+
[tutorialspoint]: https://www.tutorialspoint.com/lua/lua_iterators.htm
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Instructions
2+
3+
Given a string of digits, output all the contiguous substrings of length `n` in that string in the order that they appear.
4+
5+
For example, the string "49142" has the following 3-digit series:
6+
7+
- "491"
8+
- "914"
9+
- "142"
10+
11+
And the following 4-digit series:
12+
13+
- "4914"
14+
- "9142"
15+
16+
And if you ask for a 6-digit series from a 5-digit string, you deserve whatever you get.
17+
18+
Note that these series are only required to occupy _adjacent positions_ in the input;
19+
the digits need not be _numerically consecutive_.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"glennj"
4+
],
5+
"files": {
6+
"solution": [
7+
"series.moon"
8+
],
9+
"test": [
10+
"series_spec.moon"
11+
],
12+
"example": [
13+
".meta/example.moon"
14+
]
15+
},
16+
"blurb": "Given a string of digits, output all the contiguous substrings of length `n` in that string.",
17+
"source": "A subset of the Problem 8 at Project Euler",
18+
"source_url": "https://projecteuler.net/problem=8"
19+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
slices: (series, length) ->
3+
assert length > -1, 'slice length cannot be negative'
4+
assert length > 0, 'slice length cannot be zero'
5+
assert #series > 0, 'series cannot be empty'
6+
assert #series >= length, 'slice length cannot be greater than series length'
7+
8+
-- a "stateful iterator"
9+
idx = 0
10+
last = #series - length
11+
->
12+
if idx <= last
13+
idx += 1
14+
return series\sub(idx, idx + length - 1)
15+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
list_of_words = (list) ->
2+
"{#{table.concat [quote word for word in *list], ', '}}"
3+
4+
{
5+
module_imports: {'slices'},
6+
7+
generate_test: (case, level) ->
8+
cmd = "slices #{quote case.input.series}, #{case.input.sliceLength}"
9+
10+
local lines
11+
if case.expected.error
12+
lines = {
13+
"f = -> #{cmd}"
14+
"assert.has_error f, #{quote case.expected.error}"
15+
}
16+
else
17+
lines = {
18+
"result = [s for s in #{cmd}]"
19+
"expected = #{list_of_words case.expected}"
20+
"assert.are.same expected, result"
21+
}
22+
table.concat [indent line, level for line in *lines], '\n'
23+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[7ae7a46a-d992-4c2a-9c15-a112d125ebad]
13+
description = "slices of one from one"
14+
15+
[3143b71d-f6a5-4221-aeae-619f906244d2]
16+
description = "slices of one from two"
17+
18+
[dbb68ff5-76c5-4ccd-895a-93dbec6d5805]
19+
description = "slices of two"
20+
21+
[19bbea47-c987-4e11-a7d1-e103442adf86]
22+
description = "slices of two overlap"
23+
24+
[8e17148d-ba0a-4007-a07f-d7f87015d84c]
25+
description = "slices can include duplicates"
26+
27+
[bd5b085e-f612-4f81-97a8-6314258278b0]
28+
description = "slices of a long series"
29+
30+
[6d235d85-46cf-4fae-9955-14b6efef27cd]
31+
description = "slice length is too large"
32+
33+
[d7957455-346d-4e47-8e4b-87ed1564c6d7]
34+
description = "slice length is way too large"
35+
36+
[d34004ad-8765-4c09-8ba1-ada8ce776806]
37+
description = "slice length cannot be zero"
38+
39+
[10ab822d-8410-470a-a85d-23fbeb549e54]
40+
description = "slice length cannot be negative"
41+
42+
[c7ed0812-0e4b-4bf3-99c4-28cbbfc246a2]
43+
description = "empty series is invalid"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
slices: (series, length) ->
3+
error 'Implement me'
4+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import slices from require 'series'
2+
3+
describe 'series', ->
4+
it 'slices of one from one', ->
5+
result = [s for s in slices '1', 1]
6+
expected = {'1'}
7+
assert.are.same expected, result
8+
9+
pending 'slices of one from two', ->
10+
result = [s for s in slices '12', 1]
11+
expected = {'1', '2'}
12+
assert.are.same expected, result
13+
14+
pending 'slices of two', ->
15+
result = [s for s in slices '35', 2]
16+
expected = {'35'}
17+
assert.are.same expected, result
18+
19+
pending 'slices of two overlap', ->
20+
result = [s for s in slices '9142', 2]
21+
expected = {'91', '14', '42'}
22+
assert.are.same expected, result
23+
24+
pending 'slices can include duplicates', ->
25+
result = [s for s in slices '777777', 3]
26+
expected = {'777', '777', '777', '777'}
27+
assert.are.same expected, result
28+
29+
pending 'slices of a long series', ->
30+
result = [s for s in slices '918493904243', 5]
31+
expected = {'91849', '18493', '84939', '49390', '93904', '39042', '90424', '04243'}
32+
assert.are.same expected, result
33+
34+
pending 'slice length is too large', ->
35+
f = -> slices '12345', 6
36+
assert.has_error f, 'slice length cannot be greater than series length'
37+
38+
pending 'slice length is way too large', ->
39+
f = -> slices '12345', 42
40+
assert.has_error f, 'slice length cannot be greater than series length'
41+
42+
pending 'slice length cannot be zero', ->
43+
f = -> slices '12345', 0
44+
assert.has_error f, 'slice length cannot be zero'
45+
46+
pending 'slice length cannot be negative', ->
47+
f = -> slices '123', -1
48+
assert.has_error f, 'slice length cannot be negative'
49+
50+
pending 'empty series is invalid', ->
51+
f = -> slices '', 1
52+
assert.has_error f, 'series cannot be empty'

0 commit comments

Comments
 (0)