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 @@ -194,6 +194,14 @@
"prerequisites": [],
"difficulty": 5
},
{
"slug": "flatten-array",
"name": "Flatten Array",
"uuid": "a658e1ed-26f4-4f36-9d6c-41d6f23568f9",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "house",
"name": "House",
Expand Down
7 changes: 7 additions & 0 deletions exercises/practice/flatten-array/.docs/instructions.append.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# SQLite-specific instructions

## JSON documentation

[JSON Functions And Operators][json-docs]

[json-docs]: https://www.sqlite.org/json1.html
16 changes: 16 additions & 0 deletions exercises/practice/flatten-array/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Instructions

Take a nested array of any depth and return a fully flattened array.

Note that some language tracks may include null-like values in the input array, and the way these values are represented varies by track.
Such values should be excluded from the flattened array.

Additionally, the input may be of a different data type and contain different types, depending on the track.

Check the test suite for details.

## Example

input: `[1, [2, 6, null], [[null, [4]], 5]]`

output: `[1, 2, 6, 4, 5]`
7 changes: 7 additions & 0 deletions exercises/practice/flatten-array/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Introduction

A shipment of emergency supplies has arrived, but there's a problem.
To protect from damage, the items — flashlights, first-aid kits, blankets — are packed inside boxes, and some of those boxes are nested several layers deep inside other boxes!

To be prepared for an emergency, everything must be easily accessible in one box.
Can you unpack all the supplies and place them into a single box, so they're ready when needed most?
19 changes: 19 additions & 0 deletions exercises/practice/flatten-array/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"jimmytty"
],
"files": {
"solution": [
"flatten-array.sql"
],
"test": [
"flatten-array_test.sql"
],
"example": [
".meta/example.sql"
]
},
"blurb": "Take a nested list and return a single list with all values except nil/null.",
"source": "Interview Question",
"source_url": "https://reference.wolfram.com/language/ref/Flatten.html"
}
8 changes: 8 additions & 0 deletions exercises/practice/flatten-array/.meta/example.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
UPDATE "flatten-array"
SET result = (
SELECT JSON_GROUP_ARRAY(j.value)
FROM JSON_TREE(JSON(array)) j
WHERE j.type <> 'array'
AND j.value NOTNULL
)
;
63 changes: 63 additions & 0 deletions exercises/practice/flatten-array/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# 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.

[8c71dabd-da60-422d-a290-4a571471fb14]
description = "empty"

[d268b919-963c-442d-9f07-82b93f1b518c]
description = "no nesting"

[3f15bede-c856-479e-bb71-1684b20c6a30]
description = "flattens a nested array"

[c84440cc-bb3a-48a6-862c-94cf23f2815d]
description = "flattens array with just integers present"

[d3d99d39-6be5-44f5-a31d-6037d92ba34f]
description = "5 level nesting"

[d572bdba-c127-43ed-bdcd-6222ac83d9f7]
description = "6 level nesting"

[0705a8e5-dc86-4cec-8909-150c5e54fa9c]
description = "null values are omitted from the final result"

[c6cf26de-8ccd-4410-84bd-b9efd88fd2bc]
description = "consecutive null values at the front of the list are omitted from the final result"
include = false

[bc72da10-5f55-4ada-baf3-50e4da02ec8e]
description = "consecutive null values at the front of the array are omitted from the final result"
reimplements = "c6cf26de-8ccd-4410-84bd-b9efd88fd2bc"

[382c5242-587e-4577-b8ce-a5fb51e385a1]
description = "consecutive null values in the middle of the list are omitted from the final result"
include = false

[6991836d-0d9b-4703-80a0-3f1f23eb5981]
description = "consecutive null values in the middle of the array are omitted from the final result"
reimplements = "382c5242-587e-4577-b8ce-a5fb51e385a1"

[ef1d4790-1b1e-4939-a179-51ace0829dbd]
description = "6 level nest list with null values"
include = false

[dc90a09c-5376-449c-a7b3-c2d20d540069]
description = "6 level nested array with null values"
reimplements = "ef1d4790-1b1e-4939-a179-51ace0829dbd"

[85721643-705a-4150-93ab-7ae398e2942d]
description = "all values in nested list are null"
include = false

[51f5d9af-8f7f-4fb5-a156-69e8282cb275]
description = "all values in nested array are null"
reimplements = "85721643-705a-4150-93ab-7ae398e2942d"
11 changes: 11 additions & 0 deletions exercises/practice/flatten-array/create_fixture.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
DROP TABLE IF EXISTS "flatten-array";
CREATE TABLE "flatten-array" (
array TEXT NOT NULL, -- json array
result TEXT -- json array
);

.mode csv
.import ./data.csv "flatten-array"

UPDATE "flatten-array" SET result = NULL;

30 changes: 30 additions & 0 deletions exercises/practice/flatten-array/create_test_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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
array TEXT NOT NULL, -- json array
expected TEXT NOT NULL -- json array
);

INSERT INTO tests (uuid, description, array, expected)
VALUES
('8c71dabd-da60-422d-a290-4a571471fb14', 'empty', '[]', '[]'),
('d268b919-963c-442d-9f07-82b93f1b518c', 'no nesting', '[0,1,2]', '[0,1,2]'),
('3f15bede-c856-479e-bb71-1684b20c6a30', 'flattens a nested array', '[[[]]]', '[]'),
('c84440cc-bb3a-48a6-862c-94cf23f2815d', 'flattens array with just integers present', '[1,[2,3,4,5,6,7],8]', '[1,2,3,4,5,6,7,8]'),
('d3d99d39-6be5-44f5-a31d-6037d92ba34f', '5 level nesting', '[0,2,[[2,3],8,100,4,[[[50]]]],-2]', '[0,2,2,3,8,100,4,50,-2]'),
('d572bdba-c127-43ed-bdcd-6222ac83d9f7', '6 level nesting', '[1,[2,[[3]],[4,[[5]]],6,7],8]', '[1,2,3,4,5,6,7,8]'),
('0705a8e5-dc86-4cec-8909-150c5e54fa9c', 'null values are omitted from the final result', '[1,2,null]', '[1,2]'),
('bc72da10-5f55-4ada-baf3-50e4da02ec8e', 'consecutive null values at the front of the array are omitted from the final result', '[null,null,3]', '[3]'),
('6991836d-0d9b-4703-80a0-3f1f23eb5981', 'consecutive null values in the middle of the array are omitted from the final result', '[1,null,null,4]', '[1,4]'),
('dc90a09c-5376-449c-a7b3-c2d20d540069', '6 level nested array with null values', '[0,2,[[2,3],8,[[100]],null,[[null]]],-2]', '[0,2,2,3,8,100,-2]'),
('51f5d9af-8f7f-4fb5-a156-69e8282cb275', 'all values in nested array are null', '[null,[[[null]]],null,null,[[null,null],null],null]', '[]');

11 changes: 11 additions & 0 deletions exercises/practice/flatten-array/data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"[]",
"[0,1,2]",
"[[[]]]",
"[1,[2,3,4,5,6,7],8]",
"[0,2,[[2,3],8,100,4,[[[50]]]],-2]",
"[1,[2,[[3]],[4,[[5]]],6,7],8]",
"[1,2,null]",
"[null,null,3]",
"[1,null,null,4]",
"[0,2,[[2,3],8,[[100]],null,[[null]]],-2]",
"[null,[[[null]]],null,null,[[null,null],null],null]",
5 changes: 5 additions & 0 deletions exercises/practice/flatten-array/flatten-array.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Schema:
-- CREATE TABLE "flatten-array" (
-- array TEXT NOT NULL, -- json array
-- result TEXT -- json array
-- );
39 changes: 39 additions & 0 deletions exercises/practice/flatten-array/flatten-array_test.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
-- 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 ./flatten-array.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 array, result FROM "flatten-array") AS actual
WHERE (actual.array, JSON(actual.result)) =
(tests.array, JSON(tests.expected));

-- Update message for failed tests to give helpful information:
UPDATE tests
SET message = (
'Result for "' || tests.array || '"' || ' is <'
|| COALESCE(actual.result, 'NULL')
|| '> but should be <' || tests.expected || '>'
)
FROM (SELECT array, result FROM "flatten-array") AS actual
WHERE actual.array = tests.array 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;