Skip to content

Commit

Permalink
Add sublist exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
keiravillekode committed Oct 27, 2024
1 parent 77f47cd commit a4eaf22
Show file tree
Hide file tree
Showing 9 changed files with 303 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,14 @@
"math"
]
},
{
"slug": "sublist",
"name": "Sublist",
"uuid": "03a62c09-1e55-4676-b649-4fb870361e0e",
"practices": [],
"prerequisites": [],
"difficulty": 3
},
{
"slug": "twelve-days",
"name": "Twelve Days",
Expand Down
25 changes: 25 additions & 0 deletions exercises/practice/sublist/.docs/instructions.md
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
17 changes: 17 additions & 0 deletions exercises/practice/sublist/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"sublist.wren"
],
"test": [
"sublist.spec.wren"
],
"example": [
".meta/proof.ci.wren"
]
},
"blurb": "Write a function to determine if a list is a sublist of another list."
}
37 changes: 37 additions & 0 deletions exercises/practice/sublist/.meta/proof.ci.wren
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class Sublist {
static sublist(listOne, listTwo) {
if (listOne.count < listTwo.count) {
if (isSublist(listOne, listTwo)) {
return "sublist"
}
} else if (listOne.count > listTwo.count) {
if (isSublist(listTwo, listOne)) {
return "superlist"
}
} else {
if (isSublist(listOne, listTwo, 0)) {
return "equal"
}
}

return "unequal"
}

static isSublist(listOne, listTwo) {
for (offset in 0..(listTwo.count - listOne.count)) {
if (isSublist(listOne, listTwo, offset)) {
return true
}
}
return false
}

static isSublist(listOne, listTwo, offset) {
for (index in 0...listOne.count) {
if (listOne[index] != listTwo[offset + index]) {
return false
}
}
return true
}
}
64 changes: 64 additions & 0 deletions exercises/practice/sublist/.meta/tests.toml
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"
21 changes: 21 additions & 0 deletions exercises/practice/sublist/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Exercism

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
14 changes: 14 additions & 0 deletions exercises/practice/sublist/package.wren
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import "wren-package" for WrenPackage, Dependency
import "os" for Process

class Package is WrenPackage {
construct new() {}
name { "exercism/sublist" }
dependencies {
return [
Dependency.new("wren-testie", "0.3.0", "https://github.com/joshgoebel/wren-testie.git")
]
}
}

Package.new().default()
112 changes: 112 additions & 0 deletions exercises/practice/sublist/sublist.spec.wren
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import "./sublist" for Sublist
import "wren-testie/testie" for Testie, Expect

Testie.test("Sublist") { |do, skip|
do.test("empty lists") {
var listOne = []
var listTwo = []
Expect.value(Sublist.sublist(listOne, listTwo)).toEqual("equal")
}

skip.test("empty list within non empty list") {
var listOne = []
var listTwo = [1, 2, 3]
Expect.value(Sublist.sublist(listOne, listTwo)).toEqual("sublist")
}

skip.test("non empty list contains empty list") {
var listOne = [1, 2, 3]
var listTwo = []
Expect.value(Sublist.sublist(listOne, listTwo)).toEqual("superlist")
}

skip.test("list equals itself") {
var listOne = [1, 2, 3]
var listTwo = [1, 2, 3]
Expect.value(Sublist.sublist(listOne, listTwo)).toEqual("equal")
}

skip.test("different lists") {
var listOne = [1, 2, 3]
var listTwo = [2, 3, 4]
Expect.value(Sublist.sublist(listOne, listTwo)).toEqual("unequal")
}

skip.test("false start") {
var listOne = [1, 2, 5]
var listTwo = [0, 1, 2, 3, 1, 2, 5, 6]
Expect.value(Sublist.sublist(listOne, listTwo)).toEqual("sublist")
}

skip.test("consecutive") {
var listOne = [1, 1, 2]
var listTwo = [0, 1, 1, 1, 2, 1, 2]
Expect.value(Sublist.sublist(listOne, listTwo)).toEqual("sublist")
}

skip.test("sublist at start") {
var listOne = [0, 1, 2]
var listTwo = [0, 1, 2, 3, 4, 5]
Expect.value(Sublist.sublist(listOne, listTwo)).toEqual("sublist")
}

skip.test("sublist in middle") {
var listOne = [2, 3, 4]
var listTwo = [0, 1, 2, 3, 4, 5]
Expect.value(Sublist.sublist(listOne, listTwo)).toEqual("sublist")
}

skip.test("sublist at end") {
var listOne = [3, 4, 5]
var listTwo = [0, 1, 2, 3, 4, 5]
Expect.value(Sublist.sublist(listOne, listTwo)).toEqual("sublist")
}

skip.test("at start of superlist") {
var listOne = [0, 1, 2, 3, 4, 5]
var listTwo = [0, 1, 2]
Expect.value(Sublist.sublist(listOne, listTwo)).toEqual("superlist")
}

skip.test("in middle of superlist") {
var listOne = [0, 1, 2, 3, 4, 5]
var listTwo = [2, 3]
Expect.value(Sublist.sublist(listOne, listTwo)).toEqual("superlist")
}

skip.test("at end of superlist") {
var listOne = [0, 1, 2, 3, 4, 5]
var listTwo = [3, 4, 5]
Expect.value(Sublist.sublist(listOne, listTwo)).toEqual("superlist")
}

skip.test("first list missing element from second list") {
var listOne = [1, 3]
var listTwo = [1, 2, 3]
Expect.value(Sublist.sublist(listOne, listTwo)).toEqual("unequal")
}

skip.test("second list missing element from first list") {
var listOne = [1, 2, 3]
var listTwo = [1, 3]
Expect.value(Sublist.sublist(listOne, listTwo)).toEqual("unequal")
}

skip.test("first list missing additional digits from second list") {
var listOne = [1, 2]
var listTwo = [1, 22]
Expect.value(Sublist.sublist(listOne, listTwo)).toEqual("unequal")
}

skip.test("order matters to a list") {
var listOne = [1, 2, 3]
var listTwo = [3, 2, 1]
Expect.value(Sublist.sublist(listOne, listTwo)).toEqual("unequal")
}

skip.test("same digits but different numbers") {
var listOne = [1, 0, 1]
var listTwo = [10, 1]
Expect.value(Sublist.sublist(listOne, listTwo)).toEqual("unequal")
}
}
5 changes: 5 additions & 0 deletions exercises/practice/sublist/sublist.wren
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Sublist {
static sublist(listOne, listTwo) {
Fiber.abort("Remove this statement and implement this function")
}
}

0 comments on commit a4eaf22

Please sign in to comment.