Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,14 @@
"prerequisites": [],
"difficulty": 2
},
{
"slug": "sublist",
"name": "Sublist",
"uuid": "1de7fdc9-752b-4550-80b7-c9bc4a563921",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "triangle",
"name": "Triangle",
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": [
"BNAndras"
],
"files": {
"solution": [
"sublist.arr"
],
"test": [
"sublist-test.arr"
],
"example": [
".meta/example.arr"
]
},
"blurb": "Determine if a list is a sublist of another list."
}
36 changes: 36 additions & 0 deletions exercises/practice/sublist/.meta/example.arr
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use context starter2024

provide: sublist end

fun sublist(list-one, list-two):
ask:
| list-one == list-two then: "equal"
| contains-sublist(list-two, list-one) then: "sublist"
| contains-sublist(list-one, list-two) then: "superlist"
| otherwise: "unequal"
end
end

fun contains-sublist(outer, inner):
cases(List) inner:
| empty => true
| link(_, _) =>
cases(List) outer:
| empty => false
| link(_, rest) =>
starts-with(outer, inner) or contains-sublist(rest, inner)
end
end
end

fun starts-with(outer, inner):
cases(List) inner:
| empty => true
| link(first-inner, rest-inner) =>
cases(List) outer:
| empty => false
| link(first-outer, rest-outer) =>
(first-outer == first-inner) and starts-with(rest-outer, rest-inner)
end
end
end
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/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "@exercism/pyret",
"description": "Exercism exercises in Pyret.",
"author": "Katrina Owen",
"contributors": [
"BNAndras"
],
"private": true,
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/exercism/pyret"
},
"scripts": {
"test": "pyret *-test.arr"
},
"dependencies": {
"pyret-npm": "0.0.90"
},
"packageManager": "npm@11.13.0"
}
75 changes: 75 additions & 0 deletions exercises/practice/sublist/sublist-test.arr
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use context starter2024

include file("sublist.arr")

check "empty lists":
sublist([list: ], [list: ]) is "equal"
end

check "empty list within non empty list":
sublist([list: ], [list: 1, 2, 3]) is "sublist"
end

check "non empty list contains empty list":
sublist([list: 1, 2, 3], [list: ]) is "superlist"
end

check "list equals itself":
sublist([list: 1, 2, 3], [list: 1, 2, 3]) is "equal"
end

check "different lists":
sublist([list: 1, 2, 3], [list: 2, 3, 4]) is "unequal"
end

check "false start":
sublist([list: 1, 2, 5], [list: 0, 1, 2, 3, 1, 2, 5, 6]) is "sublist"
end

check "consecutive":
sublist([list: 1, 1, 2], [list: 0, 1, 1, 1, 2, 1, 2]) is "sublist"
end

check "sublist at start":
sublist([list: 0, 1, 2], [list: 0, 1, 2, 3, 4, 5]) is "sublist"
end

check "sublist in middle":
sublist([list: 2, 3, 4], [list: 0, 1, 2, 3, 4, 5]) is "sublist"
end

check "sublist at end":
sublist([list: 3, 4, 5], [list: 0, 1, 2, 3, 4, 5]) is "sublist"
end

check "at start of superlist":
sublist([list: 0, 1, 2, 3, 4, 5], [list: 0, 1, 2]) is "superlist"
end

check "in middle of superlist":
sublist([list: 0, 1, 2, 3, 4, 5], [list: 2, 3]) is "superlist"
end

check "at end of superlist":
sublist([list: 0, 1, 2, 3, 4, 5], [list: 3, 4, 5]) is "superlist"
end

check "first list missing element from second list":
sublist([list: 1, 3], [list: 1, 2, 3]) is "unequal"
end

check "second list missing element from first list":
sublist([list: 1, 2, 3], [list: 1, 3]) is "unequal"
end

check "first list missing additional digits from second list":
sublist([list: 1, 2], [list: 1, 22]) is "unequal"
end

check "order matters to a list":
sublist([list: 1, 2, 3], [list: 3, 2, 1]) is "unequal"
end

check "same digits but different numbers":
sublist([list: 1, 0, 1], [list: 10, 1]) is "unequal"
end
7 changes: 7 additions & 0 deletions exercises/practice/sublist/sublist.arr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use context starter2024

provide: sublist end

fun sublist(list-one, list-two):
raise("please implement the sublist function")
end