From 519209dfcee19c1d24899b8eed820685753b6d5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20B=20Nagy?= <20251272+BNAndras@users.noreply.github.com> Date: Mon, 5 Aug 2024 21:41:52 -0700 Subject: [PATCH] Add series --- config.json | 8 +++ .../practice/series/.docs/instructions.md | 19 ++++++ exercises/practice/series/.meta/config.json | 19 ++++++ .../practice/series/.meta/example.coffee | 18 ++++++ exercises/practice/series/.meta/tests.toml | 43 +++++++++++++ exercises/practice/series/series.coffee | 4 ++ exercises/practice/series/series.spec.coffee | 60 +++++++++++++++++++ 7 files changed, 171 insertions(+) create mode 100644 exercises/practice/series/.docs/instructions.md create mode 100644 exercises/practice/series/.meta/config.json create mode 100644 exercises/practice/series/.meta/example.coffee create mode 100644 exercises/practice/series/.meta/tests.toml create mode 100644 exercises/practice/series/series.coffee create mode 100644 exercises/practice/series/series.spec.coffee diff --git a/config.json b/config.json index 232a313..d9b8282 100644 --- a/config.json +++ b/config.json @@ -429,6 +429,14 @@ "prerequisites": [], "difficulty": 2 }, + { + "slug": "series", + "name": "Series", + "uuid": "65fbb0f3-b69e-4273-9e70-bf083896d57a", + "practices": [], + "prerequisites": [], + "difficulty": 2 + }, { "slug": "space-age", "name": "Space Age", diff --git a/exercises/practice/series/.docs/instructions.md b/exercises/practice/series/.docs/instructions.md new file mode 100644 index 0000000..fd97a67 --- /dev/null +++ b/exercises/practice/series/.docs/instructions.md @@ -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_. diff --git a/exercises/practice/series/.meta/config.json b/exercises/practice/series/.meta/config.json new file mode 100644 index 0000000..4b8b645 --- /dev/null +++ b/exercises/practice/series/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "BNAndras" + ], + "files": { + "solution": [ + "series.coffee" + ], + "test": [ + "series.spec.coffee" + ], + "example": [ + ".meta/example.coffee" + ] + }, + "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" +} diff --git a/exercises/practice/series/.meta/example.coffee b/exercises/practice/series/.meta/example.coffee new file mode 100644 index 0000000..0ab7f2b --- /dev/null +++ b/exercises/practice/series/.meta/example.coffee @@ -0,0 +1,18 @@ +class Series + @slices: (series, sliceLength) -> + if !series + throw new Error "series cannot be empty" + if sliceLength == 0 + throw new Error "slice length cannot be zero" + if sliceLength < 0 + throw new Error "slice length cannot be negative" + if sliceLength > series.length + throw new Error "slice length cannot be greater than series length" + + + result = [] + for i in [0..series.length - sliceLength] + result.push series.substring(i, i + sliceLength) + result + +module.exports = Series diff --git a/exercises/practice/series/.meta/tests.toml b/exercises/practice/series/.meta/tests.toml new file mode 100644 index 0000000..9696f51 --- /dev/null +++ b/exercises/practice/series/.meta/tests.toml @@ -0,0 +1,43 @@ +# This is an auto-generated file. +# +# Regenerating this file via `configlet sync` will: +# - Recreate every `description` key/value pair +# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications +# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) +# - Preserve any other key/value pair +# +# As user-added comments (using the # character) will be removed when this file +# is regenerated, comments can be added via 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" diff --git a/exercises/practice/series/series.coffee b/exercises/practice/series/series.coffee new file mode 100644 index 0000000..484f1d6 --- /dev/null +++ b/exercises/practice/series/series.coffee @@ -0,0 +1,4 @@ +class Series + @slices: (series, sliceLength) -> + +module.exports = Series diff --git a/exercises/practice/series/series.spec.coffee b/exercises/practice/series/series.spec.coffee new file mode 100644 index 0000000..7ac6861 --- /dev/null +++ b/exercises/practice/series/series.spec.coffee @@ -0,0 +1,60 @@ +Series = require './series' + +describe 'Series', -> + it 'slices of one from one', -> + result = Series.slices '1', 1 + expect(result).toEqual ['1'] + + xit 'slices of one from two', -> + result = Series.slices '12', 1 + expect(result).toEqual ['1', '2'] + + xit 'slices of two', -> + result = Series.slices '35', 2 + expect(result).toEqual ['35'] + + xit 'slices of two overlap', -> + result = Series.slices '9142', 2 + expect(result).toEqual ['91', '14', '42'] + + xit 'slices can include duplicates', -> + result = Series.slices '777777', 3 + expect(result).toEqual ['777', '777', '777', '777'] + + xit 'slices of a long series', -> + result = Series.slices '918493904243', 5 + expect(result).toEqual [ + '91849' + '18493' + '84939' + '49390' + '93904' + '39042' + '90424' + '04243' + ] + + xit 'slice length is too large', -> + expect -> + Series.slices '12345', 6 + .toThrow new Error "slice length cannot be greater than series length" + + xit 'slice length is way too large', -> + expect -> + Series.slices '12345', 42 + .toThrow new Error "slice length cannot be greater than series length" + + xit 'slice length cannot be zero', -> + expect -> + Series.slices '12345', 0 + .toThrow new Error "slice length cannot be zero" + + xit 'slice length cannot be negative', -> + expect -> + Series.slices '123', -1 + .toThrow new Error "slice length cannot be negative" + + xit 'empty series is invalid', -> + expect -> + Series.slices '', 1 + .toThrow new Error "series cannot be empty"