Skip to content

* add new practice exercise: sublist #122

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

Merged
merged 2 commits into from
Aug 17, 2025
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 @@ -362,6 +362,14 @@
"prerequisites": [],
"difficulty": 8
},
{
"slug": "sublist",
"name": "Sublist",
"uuid": "8491674e-c9e1-4922-a25c-4a5d691158d4",
"practices": [],
"prerequisites": [],
"difficulty": 8
},
{
"slug": "tournament",
"name": "Tournament",
Expand Down
25 changes: 25 additions & 0 deletions exercises/practice/sublist/.docs/instructions.append.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# SQLite-specific instructions

The **list_one** and **list_two** columns contains JSON-enconded list of integers.

Example:

```json
[1, 2, 3]
```

## Table Schema

```sql
CREATE TABLE sublist (
list_one TEXT NOT NULL, -- json array
list_two TEXT NOT NULL, -- json array
result TEXT
);
```

## JSON documentation

[JSON Functions And Operators][json-docs]

[json-docs]: https://www.sqlite.org/json1.htmL
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": [
"jimmytty"
],
"files": {
"solution": [
"sublist.sql"
],
"test": [
"sublist_test.sql"
],
"example": [
".meta/example.sql"
]
},
"blurb": "Write a function to determine if a list is a sublist of another list."
}
49 changes: 49 additions & 0 deletions exercises/practice/sublist/.meta/example.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
UPDATE sublist
SET result = 'equal'
WHERE JSON(list_one) = JSON(list_two)
;

UPDATE sublist
SET result = (
WITH RECURSIVE to_subl (list, i, len, subl) AS (
VALUES(list_two, 0, JSON_ARRAY_LENGTH(list_one) - 1, NULL)
UNION ALL
SELECT list, i + 1, len, (
SELECT JSON_GROUP_ARRAY(value)
FROM (
SELECT j.value
FROM json_each(list) j
WHERE KEY BETWEEN i AND len + i
))
FROM to_subl
WHERE i + len < JSON_ARRAY_LENGTH(list)
) SELECT 'sublist' FROM to_subl WHERE JSON(list_one) = JSON(subl) LIMIT 1
)
WHERE NULLIF(result, '') ISNULL
AND JSON_ARRAY_LENGTH(list_one) < JSON_ARRAY_LENGTH(list_two)
;

UPDATE sublist
SET result = (
WITH RECURSIVE to_subl (list, i, len, subl) AS (
VALUES(list_one, 0, JSON_ARRAY_LENGTH(list_two) - 1, NULL)
UNION ALL
SELECT list, i + 1, len, (
SELECT JSON_GROUP_ARRAY(value)
FROM (
SELECT j.value
FROM json_each(list) j
WHERE KEY BETWEEN i AND len + i
))
FROM to_subl
WHERE i + len < JSON_ARRAY_LENGTH(list)
) SELECT 'superlist' FROM to_subl WHERE JSON(list_two) = JSON(subl) LIMIT 1
)
WHERE NULLIF(result, '') ISNULL
AND JSON_ARRAY_LENGTH(list_one) > JSON_ARRAY_LENGTH(list_two)
;

UPDATE SUBLIST
SET result = 'unequal'
WHERE NULLIF(result, '') ISNULL
;
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"
9 changes: 9 additions & 0 deletions exercises/practice/sublist/create_fixture.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
DROP TABLE IF EXISTS sublist;
CREATE TABLE sublist (
list_one TEXT NOT NULL, -- json array
list_two TEXT NOT NULL, -- json array
result TEXT
);

.mode csv
.import ./data.csv sublist
37 changes: 37 additions & 0 deletions exercises/practice/sublist/create_test_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
DROP TABLE IF EXISTS tests;
CREATE TABLE IF NOT EXISTS tests (
-- uuid and description are taken from the test.toml file
uuid TEXT PRIMARY KEY,
description TEXT NOT NULL,
-- The following section is needed by the online test-runner
status TEXT DEFAULT 'fail',
message TEXT,
output TEXT,
test_code TEXT,
task_id INTEGER DEFAULT NULL,
-- Here are columns for the actual tests
list_one TEXT NOT NULL, -- json array
list_two TEXT NOT NULL, -- json array
expected TEXT NOT NULL
);

INSERT INTO tests (uuid, description, list_one, list_two, expected)
VALUES
('97319c93-ebc5-47ab-a022-02a1980e1d29', 'empty lists', '[]', '[]', 'equal'),
('de27dbd4-df52-46fe-a336-30be58457382', 'empty list within non empty list', '[]', '[1,2,3]', 'sublist'),
('5487cfd1-bc7d-429f-ac6f-1177b857d4fb', 'non empty list contains empty list', '[1,2,3]', '[]', 'superlist'),
('1f390b47-f6b2-4a93-bc23-858ba5dda9a6', 'list equals itself', '[1,2,3]', '[1,2,3]', 'equal'),
('7ed2bfb2-922b-4363-ae75-f3a05e8274f5', 'different lists', '[1,2,3]', '[2,3,4]', 'unequal'),
('3b8a2568-6144-4f06-b0a1-9d266b365341', 'false start', '[1,2,5]', '[0,1,2,3,1,2,5,6]', 'sublist'),
('dc39ed58-6311-4814-be30-05a64bc8d9b1', 'consecutive', '[1,1,2]', '[0,1,1,1,2,1,2]', 'sublist'),
('d1270dab-a1ce-41aa-b29d-b3257241ac26', 'sublist at start', '[0,1,2]', '[0,1,2,3,4,5]', 'sublist'),
('81f3d3f7-4f25-4ada-bcdc-897c403de1b6', 'sublist in middle', '[2,3,4]', '[0,1,2,3,4,5]', 'sublist'),
('43bcae1e-a9cf-470e-923e-0946e04d8fdd', 'sublist at end', '[3,4,5]', '[0,1,2,3,4,5]', 'sublist'),
('76cf99ed-0ff0-4b00-94af-4dfb43fe5caa', 'at start of superlist', '[0,1,2,3,4,5]', '[0,1,2]', 'superlist'),
('b83989ec-8bdf-4655-95aa-9f38f3e357fd', 'in middle of superlist', '[0,1,2,3,4,5]', '[2,3]', 'superlist'),
('26f9f7c3-6cf6-4610-984a-662f71f8689b', 'at end of superlist', '[0,1,2,3,4,5]', '[3,4,5]', 'superlist'),
('0a6db763-3588-416a-8f47-76b1cedde31e', 'first list missing element from second list', '[1,3]', '[1,2,3]', 'unequal'),
('83ffe6d8-a445-4a3c-8795-1e51a95e65c3', 'second list missing element from first list', '[1,2,3]', '[1,3]', 'unequal'),
('7bc76cb8-5003-49ca-bc47-cdfbe6c2bb89', 'first list missing additional digits from second list', '[1,2]', '[1,22]', 'unequal'),
('0d7ee7c1-0347-45c8-9ef5-b88db152b30b', 'order matters to a list', '[1,2,3]', '[3,2,1]', 'unequal'),
('5f47ce86-944e-40f9-9f31-6368aad70aa6', 'same digits but different numbers', '[1,0,1]', '[10,1]', 'unequal');
18 changes: 18 additions & 0 deletions exercises/practice/sublist/data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"[]","[]",""
"[]","[1,2,3]",""
"[1,2,3]","[]",""
"[1,2,3]","[1,2,3]",""
"[1,2,3]","[2,3,4]",""
"[1,2,5]","[0,1,2,3,1,2,5,6]",""
"[1,1,2]","[0,1,1,1,2,1,2]",""
"[0,1,2]","[0,1,2,3,4,5]",""
"[2,3,4]","[0,1,2,3,4,5]",""
"[3,4,5]","[0,1,2,3,4,5]",""
"[0,1,2,3,4,5]","[0,1,2]",""
"[0,1,2,3,4,5]","[2,3]",""
"[0,1,2,3,4,5]","[3,4,5]",""
"[1,3]","[1,2,3]",""
"[1,2,3]","[1,3]",""
"[1,2]","[1,22]",""
"[1,2,3]","[3,2,1]",""
"[1,0,1]","[10,1]",""
9 changes: 9 additions & 0 deletions exercises/practice/sublist/sublist.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- Schema:
-- DROP TABLE IF EXISTS sublist;
-- CREATE TABLE sublist (
-- list_one TEXT NOT NULL, -- json array
-- list_two TEXT NOT NULL, -- json array
-- result TEXT
-- );
--
-- Task: update the sublist table and set the result based on the list_one and list_two.
40 changes: 40 additions & 0 deletions exercises/practice/sublist/sublist_test.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
-- Create database:
.read ./create_fixture.sql

-- Read user student solution and save any output as markdown in user_output.md:
.mode markdown
.output user_output.md
.read ./sublist.sql
.output

-- Create a clean testing environment:
.read ./create_test_table.sql

-- Comparison of user input and the tests updates the status for each test:
UPDATE tests
SET status = 'pass'
FROM (SELECT list_one, list_two, result FROM sublist) AS actual
WHERE (actual.list_one, actual.list_two, actual.result) = (tests.list_one, tests.list_two, tests.expected);

-- Update message for failed tests to give helpful information:
UPDATE tests
SET message = (
'Result for "'
|| PRINTF('list_one=%s, list_two=%s', tests.list_one, tests.list_two)
|| '"'
|| ' is <' || COALESCE(actual.result, 'NULL')
|| '> but should be <' || tests.expected || '>'
)
FROM (SELECT list_one, list_two, result FROM sublist) AS actual
WHERE (actual.list_one, actual.list_two) = (tests.list_one, tests.list_two) AND tests.status = 'fail';

-- Save results to ./output.json (needed by the online test-runner)
.mode json
.once './output.json'
SELECT description, status, message, output, test_code, task_id
FROM tests;

-- Display test results in readable form for the student:
.mode table
SELECT description, status, message
FROM tests;