-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1733535
commit 5a6f785
Showing
8 changed files
with
247 additions
and
0 deletions.
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,4 @@ | ||
# Instructions append | ||
|
||
Whilst the instructions refer to working with lists, in R the vector type is a more common collection type. | ||
Hence, you should read "vector" whenever "list" is mentioned. |
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,25 @@ | ||
# Instructions | ||
|
||
Given any two lists `A` and `B`, determine if: | ||
|
||
- List `A` is equal to list `B`; or | ||
- List `A` contains list `B` (`A` is a superlist of `B`); or | ||
- List `A` is contained by list `B` (`A` is a sublist of `B`); or | ||
- None of the above is true, thus lists `A` and `B` are unequal | ||
|
||
Specifically, list `A` is equal to list `B` if both lists have the same values in the same order. | ||
List `A` is a superlist of `B` if `A` contains a contiguous sub-sequence of values equal to `B`. | ||
List `A` is a sublist of `B` if `B` contains a contiguous sub-sequence of values equal to `A`. | ||
|
||
Examples: | ||
|
||
- If `A = []` and `B = []` (both lists are empty), then `A` and `B` are equal | ||
- If `A = [1, 2, 3]` and `B = []`, then `A` is a superlist of `B` | ||
- If `A = []` and `B = [1, 2, 3]`, then `A` is a sublist of `B` | ||
- If `A = [1, 2, 3]` and `B = [1, 2, 3, 4, 5]`, then `A` is a sublist of `B` | ||
- If `A = [3, 4, 5]` and `B = [1, 2, 3, 4, 5]`, then `A` is a sublist of `B` | ||
- If `A = [3, 4]` and `B = [1, 2, 3, 4, 5]`, then `A` is a sublist of `B` | ||
- If `A = [1, 2, 3]` and `B = [1, 2, 3]`, then `A` and `B` are equal | ||
- If `A = [1, 2, 3, 4, 5]` and `B = [2, 3, 4]`, then `A` is a superlist of `B` | ||
- If `A = [1, 2, 4]` and `B = [1, 2, 3, 4, 5]`, then `A` and `B` are unequal | ||
- If `A = [1, 2, 3]` and `B = [1, 3, 2]`, then `A` and `B` are unequal |
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,17 @@ | ||
{ | ||
"authors": [ | ||
"erikschierboom" | ||
], | ||
"files": { | ||
"solution": [ | ||
"sublist.R" | ||
], | ||
"test": [ | ||
"test_sublist.R" | ||
], | ||
"example": [ | ||
".meta/example.R" | ||
] | ||
}, | ||
"blurb": "Write a function to determine if a list is a sublist of another list." | ||
} |
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,16 @@ | ||
compare <- function(vec_one, vec_two) { | ||
switch(sign(length(vec_one) - length(vec_two)) + 2, | ||
ifelse(is.subvector(vec_one, vec_two), "subvector", "unequal"), | ||
ifelse( identical(vec_one, vec_two), "equal", "unequal"), | ||
ifelse(is.subvector(vec_two, vec_one), "supervector", "unequal")) | ||
} | ||
|
||
is.subvector <- function(vec_one, vec_two) { | ||
for (i in seq_len(length(vec_two)-length(vec_one) + 1)) { | ||
if (all(vec_one == vec_two[i:(i + length(vec_one) - 1)])) { | ||
return(TRUE) | ||
} | ||
} | ||
|
||
FALSE | ||
} |
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,64 @@ | ||
# 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. | ||
|
||
[97319c93-ebc5-47ab-a022-02a1980e1d29] | ||
description = "empty lists" | ||
|
||
[de27dbd4-df52-46fe-a336-30be58457382] | ||
description = "empty list within non empty list" | ||
|
||
[5487cfd1-bc7d-429f-ac6f-1177b857d4fb] | ||
description = "non empty list contains empty list" | ||
|
||
[1f390b47-f6b2-4a93-bc23-858ba5dda9a6] | ||
description = "list equals itself" | ||
|
||
[7ed2bfb2-922b-4363-ae75-f3a05e8274f5] | ||
description = "different lists" | ||
|
||
[3b8a2568-6144-4f06-b0a1-9d266b365341] | ||
description = "false start" | ||
|
||
[dc39ed58-6311-4814-be30-05a64bc8d9b1] | ||
description = "consecutive" | ||
|
||
[d1270dab-a1ce-41aa-b29d-b3257241ac26] | ||
description = "sublist at start" | ||
|
||
[81f3d3f7-4f25-4ada-bcdc-897c403de1b6] | ||
description = "sublist in middle" | ||
|
||
[43bcae1e-a9cf-470e-923e-0946e04d8fdd] | ||
description = "sublist at end" | ||
|
||
[76cf99ed-0ff0-4b00-94af-4dfb43fe5caa] | ||
description = "at start of superlist" | ||
|
||
[b83989ec-8bdf-4655-95aa-9f38f3e357fd] | ||
description = "in middle of superlist" | ||
|
||
[26f9f7c3-6cf6-4610-984a-662f71f8689b] | ||
description = "at end of superlist" | ||
|
||
[0a6db763-3588-416a-8f47-76b1cedde31e] | ||
description = "first list missing element from second list" | ||
|
||
[83ffe6d8-a445-4a3c-8795-1e51a95e65c3] | ||
description = "second list missing element from first list" | ||
|
||
[7bc76cb8-5003-49ca-bc47-cdfbe6c2bb89] | ||
description = "first list missing additional digits from second list" | ||
|
||
[0d7ee7c1-0347-45c8-9ef5-b88db152b30b] | ||
description = "order matters to a list" | ||
|
||
[5f47ce86-944e-40f9-9f31-6368aad70aa6] | ||
description = "same digits but different numbers" |
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 @@ | ||
compare <- function(vec_one, vec_two) { | ||
|
||
} |
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,110 @@ | ||
source("./sublist.R") | ||
library(testthat) | ||
|
||
test_that("Empty vectors", { | ||
vector_one <- c() | ||
vector_two <- c() | ||
expect_equal(compare(vector_one, vector_two), "equal") | ||
}) | ||
|
||
test_that("Empty vector within non empty vector", { | ||
vector_one <- c() | ||
vector_two <- c(1, 2, 3) | ||
expect_equal(compare(vector_one, vector_two), "subvector") | ||
}) | ||
|
||
test_that("Non empty vector contains empty vector", { | ||
vector_one <- c(1, 2, 3) | ||
vector_two <- c() | ||
expect_equal(compare(vector_one, vector_two), "supervector") | ||
}) | ||
|
||
test_that("vector equals itself", { | ||
vector_one <- c(1, 2, 3) | ||
vector_two <- c(1, 2, 3) | ||
expect_equal(compare(vector_one, vector_two), "equal") | ||
}) | ||
|
||
test_that("Different vectors", { | ||
vector_one <- c(1, 2, 3) | ||
vector_two <- c(2, 3, 4) | ||
expect_equal(compare(vector_one, vector_two), "unequal") | ||
}) | ||
|
||
test_that("False start", { | ||
vector_one <- c(1, 2, 5) | ||
vector_two <- c(0, 1, 2, 3, 1, 2, 5, 6) | ||
expect_equal(compare(vector_one, vector_two), "subvector") | ||
}) | ||
|
||
test_that("Consecutive", { | ||
vector_one <- c(1, 1, 2) | ||
vector_two <- c(0, 1, 1, 1, 2, 1, 2) | ||
expect_equal(compare(vector_one, vector_two), "subvector") | ||
}) | ||
|
||
test_that("Subvector at start", { | ||
vector_one <- c(0, 1, 2) | ||
vector_two <- c(0, 1, 2, 3, 4, 5) | ||
expect_equal(compare(vector_one, vector_two), "subvector") | ||
}) | ||
|
||
test_that("Subvector in middle", { | ||
vector_one <- c(2, 3, 4) | ||
vector_two <- c(0, 1, 2, 3, 4, 5) | ||
expect_equal(compare(vector_one, vector_two), "subvector") | ||
}) | ||
|
||
test_that("Subvector at end", { | ||
vector_one <- c(3, 4, 5) | ||
vector_two <- c(0, 1, 2, 3, 4, 5) | ||
expect_equal(compare(vector_one, vector_two), "subvector") | ||
}) | ||
|
||
test_that("At start of supervector", { | ||
vector_one <- c(0, 1, 2, 3, 4, 5) | ||
vector_two <- c(0, 1, 2) | ||
expect_equal(compare(vector_one, vector_two), "supervector") | ||
}) | ||
|
||
test_that("In middle of supervector", { | ||
vector_one <- c(0, 1, 2, 3, 4, 5) | ||
vector_two <- c(2, 3) | ||
expect_equal(compare(vector_one, vector_two), "supervector") | ||
}) | ||
|
||
test_that("At end of supervector", { | ||
vector_one <- c(0, 1, 2, 3, 4, 5) | ||
vector_two <- c(3, 4, 5) | ||
expect_equal(compare(vector_one, vector_two), "supervector") | ||
}) | ||
|
||
test_that("First vector missing element from second vector", { | ||
vector_one <- c(1, 3) | ||
vector_two <- c(1, 2, 3) | ||
expect_equal(compare(vector_one, vector_two), "unequal") | ||
}) | ||
|
||
test_that("Second vector missing element from first vector", { | ||
vector_one <- c(1, 2, 3) | ||
vector_two <- c(1, 3) | ||
expect_equal(compare(vector_one, vector_two), "unequal") | ||
}) | ||
|
||
test_that("First vector missing additional digits from second vector", { | ||
vector_one <- c(1, 2) | ||
vector_two <- c(1, 22) | ||
expect_equal(compare(vector_one, vector_two), "unequal") | ||
}) | ||
|
||
test_that("Order matters to a vector", { | ||
vector_one <- c(1, 2, 3) | ||
vector_two <- c(3, 2, 1) | ||
expect_equal(compare(vector_one, vector_two), "unequal") | ||
}) | ||
|
||
test_that("Same digits but different numbers", { | ||
vector_one <- c(1, 0, 1) | ||
vector_two <- c(10, 1) | ||
expect_equal(compare(vector_one, vector_two), "unequal") | ||
}) |