Skip to content

Commit

Permalink
Add series exercise (#375)
Browse files Browse the repository at this point in the history
* Add series exercise

* reformat
  • Loading branch information
keiravillekode authored May 9, 2024
1 parent 69e1812 commit e5ca36b
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 0 deletions.
11 changes: 11 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,17 @@
"math"
]
},
{
"slug": "series",
"name": "Series",
"uuid": "2e10ca92-6232-418e-b9ea-506ac1eba195",
"practices": [],
"prerequisites": [],
"difficulty": 3,
"topics": [
"strings"
]
},
{
"slug": "largest-series-product",
"name": "Largest Series Product",
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.el"
],
"test": [
"series-test.el"
],
"example": [
".meta/example.el"
]
},
"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"
}
22 changes: 22 additions & 0 deletions exercises/practice/series/.meta/example.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
;;; series.el --- Series (exercism) -*- lexical-binding: t; -*-

;;; Commentary:

;;; Code:



(defun slices (series slice-length)
(cond
((equal "" series) (error "series cannot be empty"))
((> slice-length (length series)) (error "slice length cannot be greater than series length"))
((= slice-length 0) (error "slice length cannot be zero"))
((< slice-length 0) (error "slice length cannot be negative"))
(t (mapcar (lambda (start)
(substring series start (+ start slice-length)))
(number-sequence 0 (- (length series) slice-length))))))


(provide 'series)
;;; series.el ends here

43 changes: 43 additions & 0 deletions exercises/practice/series/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -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"
64 changes: 64 additions & 0 deletions exercises/practice/series/series-test.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
;;; series-test.el --- Tests for Series (exercism) -*- lexical-binding: t; -*-

;;; Commentary:

;;; Code:

(load-file "series.el")
(declare-function slices "series.el" (series slice-length))


(ert-deftest slices-of-one-from-one ()
(should (equal '("1") (slices "1" 1))))


(ert-deftest slices-of-one-from-two ()
(should (equal '("1" "2") (slices "12" 1))))


(ert-deftest slices-of-two ()
(should (equal '("35") (slices "35" 2))))


(ert-deftest slices-of-two-overlap ()
(should (equal '("91" "14" "42") (slices "9142" 2))))


(ert-deftest slices-can-include-duplicates ()
(should (equal '("777" "777" "777" "777") (slices "777777" 3))))


(ert-deftest slices-of-a-long-series ()
(should (equal '("91849"
"18493"
"84939"
"49390"
"93904"
"39042"
"90424"
"04243")
(slices "918493904243" 5))))


(ert-deftest slice-length-is-too-large ()
(should-error (slices "12345" 6)))


(ert-deftest slice-length-is-way-too-large ()
(should-error (slices "12345" 42)))


(ert-deftest slice-length-cannot-be-zero ()
(should-error (slices "12345" 0)))


(ert-deftest slice-length-cannot-be-negative ()
(should-error (slices "123" -1)))


(ert-deftest empty-series-is-invalid ()
(should-error (slices "" 1)))


(provide 'series-test)
;;; series-test.el ends here
14 changes: 14 additions & 0 deletions exercises/practice/series/series.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
;;; series.el --- Series (exercism) -*- lexical-binding: t; -*-

;;; Commentary:

;;; Code:


(defun slices (series slice-length)
(error "Delete this S-Expression and write your own implementation"))


(provide 'series)
;;; series.el ends here

0 comments on commit e5ca36b

Please sign in to comment.