-
-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
series: add exercise #346
Merged
Merged
series: add exercise #346
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"authors": [ | ||
"erikschierboom" | ||
], | ||
"files": { | ||
"solution": [ | ||
"series.R" | ||
], | ||
"test": [ | ||
"test_series.R" | ||
], | ||
"example": [ | ||
".meta/example.R" | ||
] | ||
}, | ||
"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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
slices <- function(series, slice_length) { | ||
stopifnot(nchar(series) > 0 & slice_length %in% 1:nchar(series)) | ||
|
||
slice_indices <- 1:(nchar(series) - slice_length + 1) | ||
sapply(slice_indices, function(i) | ||
substr(series, i, i + slice_length - 1)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
slices <- function(series, slice_length) { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
source("./series.R") | ||
library(testthat) | ||
|
||
test_that("Slices of one from one", { | ||
expect_equal(slices("1", 1), c("1")) | ||
}) | ||
|
||
test_that("Slices of one from two", { | ||
expect_equal(slices("12", 1), c("1", "2")) | ||
}) | ||
|
||
test_that("Slices of two", { | ||
expect_equal(slices("35", 2), c("35")) | ||
}) | ||
|
||
test_that("Slices of two overlap", { | ||
expect_equal(slices("9142", 2), c("91", "14", "42")) | ||
}) | ||
|
||
test_that("Slices can include duplicates", { | ||
expect_equal(slices("777777", 3), c("777", "777", "777", "777")) | ||
}) | ||
|
||
test_that("Slices of a long series", { | ||
expect_equal( | ||
slices("918493904243", 5), | ||
c( | ||
"91849", | ||
"18493", | ||
"84939", | ||
"49390", | ||
"93904", | ||
"39042", | ||
"90424", | ||
"04243" | ||
) | ||
) | ||
}) | ||
|
||
test_that("Slice length is too large", { | ||
expect_error(slices("12345", 6)) | ||
}) | ||
|
||
test_that("Slice length is way too large", { | ||
expect_error(slices("12345", 42)) | ||
}) | ||
|
||
test_that("Slice length cannot be zero", { | ||
expect_error(slices("12345", 0)) | ||
}) | ||
|
||
test_that("Slice length cannot be negative", { | ||
expect_error(slices("123", -1)) | ||
}) | ||
|
||
test_that("Empty series is invalid", { | ||
expect_error(slices("", 1)) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine adding this, but you do know that
topics
aren't used by the website, right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haha, it did cross my mind that if you hadn't included it, it was quite likely because it's not really needed or being used anywhere, but was more efficient for me to suggest it and wait and see how you responded than for me to try and check if or how it might be used later. If there's no value in adding it (now or for later) then obviously fine with leaving it out.