Skip to content

Commit 38960f4

Browse files
authored
Add series (#331)
1 parent 9aec939 commit 38960f4

File tree

7 files changed

+171
-0
lines changed

7 files changed

+171
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,14 @@
429429
"prerequisites": [],
430430
"difficulty": 2
431431
},
432+
{
433+
"slug": "series",
434+
"name": "Series",
435+
"uuid": "65fbb0f3-b69e-4273-9e70-bf083896d57a",
436+
"practices": [],
437+
"prerequisites": [],
438+
"difficulty": 2
439+
},
432440
{
433441
"slug": "space-age",
434442
"name": "Space Age",
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+
"BNAndras"
4+
],
5+
"files": {
6+
"solution": [
7+
"series.coffee"
8+
],
9+
"test": [
10+
"series.spec.coffee"
11+
],
12+
"example": [
13+
".meta/example.coffee"
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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Series
2+
@slices: (series, sliceLength) ->
3+
if !series
4+
throw new Error "series cannot be empty"
5+
if sliceLength == 0
6+
throw new Error "slice length cannot be zero"
7+
if sliceLength < 0
8+
throw new Error "slice length cannot be negative"
9+
if sliceLength > series.length
10+
throw new Error "slice length cannot be greater than series length"
11+
12+
13+
result = []
14+
for i in [0..series.length - sliceLength]
15+
result.push series.substring(i, i + sliceLength)
16+
result
17+
18+
module.exports = Series
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+
class Series
2+
@slices: (series, sliceLength) ->
3+
4+
module.exports = Series
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
Series = require './series'
2+
3+
describe 'Series', ->
4+
it 'slices of one from one', ->
5+
result = Series.slices '1', 1
6+
expect(result).toEqual ['1']
7+
8+
xit 'slices of one from two', ->
9+
result = Series.slices '12', 1
10+
expect(result).toEqual ['1', '2']
11+
12+
xit 'slices of two', ->
13+
result = Series.slices '35', 2
14+
expect(result).toEqual ['35']
15+
16+
xit 'slices of two overlap', ->
17+
result = Series.slices '9142', 2
18+
expect(result).toEqual ['91', '14', '42']
19+
20+
xit 'slices can include duplicates', ->
21+
result = Series.slices '777777', 3
22+
expect(result).toEqual ['777', '777', '777', '777']
23+
24+
xit 'slices of a long series', ->
25+
result = Series.slices '918493904243', 5
26+
expect(result).toEqual [
27+
'91849'
28+
'18493'
29+
'84939'
30+
'49390'
31+
'93904'
32+
'39042'
33+
'90424'
34+
'04243'
35+
]
36+
37+
xit 'slice length is too large', ->
38+
expect ->
39+
Series.slices '12345', 6
40+
.toThrow new Error "slice length cannot be greater than series length"
41+
42+
xit 'slice length is way too large', ->
43+
expect ->
44+
Series.slices '12345', 42
45+
.toThrow new Error "slice length cannot be greater than series length"
46+
47+
xit 'slice length cannot be zero', ->
48+
expect ->
49+
Series.slices '12345', 0
50+
.toThrow new Error "slice length cannot be zero"
51+
52+
xit 'slice length cannot be negative', ->
53+
expect ->
54+
Series.slices '123', -1
55+
.toThrow new Error "slice length cannot be negative"
56+
57+
xit 'empty series is invalid', ->
58+
expect ->
59+
Series.slices '', 1
60+
.toThrow new Error "series cannot be empty"

0 commit comments

Comments
 (0)