From 36f2ece03bb88656c65caefbe10fc5e793e1c579 Mon Sep 17 00:00:00 2001 From: Ronaldo Ferreira de Lima Date: Sat, 30 Aug 2025 01:05:26 -0300 Subject: [PATCH] * add practice exercise: flatten-array --- config.json | 8 +++ .../.docs/instructions.append.md | 7 +++ .../flatten-array/.docs/instructions.md | 16 +++++ .../flatten-array/.docs/introduction.md | 7 +++ .../practice/flatten-array/.meta/config.json | 19 ++++++ .../practice/flatten-array/.meta/example.sql | 8 +++ .../practice/flatten-array/.meta/tests.toml | 63 +++++++++++++++++++ .../practice/flatten-array/create_fixture.sql | 11 ++++ .../flatten-array/create_test_table.sql | 30 +++++++++ exercises/practice/flatten-array/data.csv | 11 ++++ .../practice/flatten-array/flatten-array.sql | 5 ++ .../flatten-array/flatten-array_test.sql | 39 ++++++++++++ 12 files changed, 224 insertions(+) create mode 100644 exercises/practice/flatten-array/.docs/instructions.append.md create mode 100644 exercises/practice/flatten-array/.docs/instructions.md create mode 100644 exercises/practice/flatten-array/.docs/introduction.md create mode 100644 exercises/practice/flatten-array/.meta/config.json create mode 100644 exercises/practice/flatten-array/.meta/example.sql create mode 100644 exercises/practice/flatten-array/.meta/tests.toml create mode 100644 exercises/practice/flatten-array/create_fixture.sql create mode 100644 exercises/practice/flatten-array/create_test_table.sql create mode 100644 exercises/practice/flatten-array/data.csv create mode 100644 exercises/practice/flatten-array/flatten-array.sql create mode 100644 exercises/practice/flatten-array/flatten-array_test.sql diff --git a/config.json b/config.json index 75d82da..daf2394 100644 --- a/config.json +++ b/config.json @@ -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", diff --git a/exercises/practice/flatten-array/.docs/instructions.append.md b/exercises/practice/flatten-array/.docs/instructions.append.md new file mode 100644 index 0000000..27d1e44 --- /dev/null +++ b/exercises/practice/flatten-array/.docs/instructions.append.md @@ -0,0 +1,7 @@ +# SQLite-specific instructions + +## JSON documentation + +[JSON Functions And Operators][json-docs] + +[json-docs]: https://www.sqlite.org/json1.html diff --git a/exercises/practice/flatten-array/.docs/instructions.md b/exercises/practice/flatten-array/.docs/instructions.md new file mode 100644 index 0000000..b5b8271 --- /dev/null +++ b/exercises/practice/flatten-array/.docs/instructions.md @@ -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]` diff --git a/exercises/practice/flatten-array/.docs/introduction.md b/exercises/practice/flatten-array/.docs/introduction.md new file mode 100644 index 0000000..a314857 --- /dev/null +++ b/exercises/practice/flatten-array/.docs/introduction.md @@ -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? diff --git a/exercises/practice/flatten-array/.meta/config.json b/exercises/practice/flatten-array/.meta/config.json new file mode 100644 index 0000000..795b898 --- /dev/null +++ b/exercises/practice/flatten-array/.meta/config.json @@ -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" +} diff --git a/exercises/practice/flatten-array/.meta/example.sql b/exercises/practice/flatten-array/.meta/example.sql new file mode 100644 index 0000000..cb3cf1f --- /dev/null +++ b/exercises/practice/flatten-array/.meta/example.sql @@ -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 + ) +; diff --git a/exercises/practice/flatten-array/.meta/tests.toml b/exercises/practice/flatten-array/.meta/tests.toml new file mode 100644 index 0000000..44acf17 --- /dev/null +++ b/exercises/practice/flatten-array/.meta/tests.toml @@ -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" diff --git a/exercises/practice/flatten-array/create_fixture.sql b/exercises/practice/flatten-array/create_fixture.sql new file mode 100644 index 0000000..e0665ed --- /dev/null +++ b/exercises/practice/flatten-array/create_fixture.sql @@ -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; + diff --git a/exercises/practice/flatten-array/create_test_table.sql b/exercises/practice/flatten-array/create_test_table.sql new file mode 100644 index 0000000..4033904 --- /dev/null +++ b/exercises/practice/flatten-array/create_test_table.sql @@ -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]', '[]'); + diff --git a/exercises/practice/flatten-array/data.csv b/exercises/practice/flatten-array/data.csv new file mode 100644 index 0000000..b4f9688 --- /dev/null +++ b/exercises/practice/flatten-array/data.csv @@ -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]", diff --git a/exercises/practice/flatten-array/flatten-array.sql b/exercises/practice/flatten-array/flatten-array.sql new file mode 100644 index 0000000..f0280a9 --- /dev/null +++ b/exercises/practice/flatten-array/flatten-array.sql @@ -0,0 +1,5 @@ +-- Schema: +-- CREATE TABLE "flatten-array" ( +-- array TEXT NOT NULL, -- json array +-- result TEXT -- json array +-- ); diff --git a/exercises/practice/flatten-array/flatten-array_test.sql b/exercises/practice/flatten-array/flatten-array_test.sql new file mode 100644 index 0000000..5aa1c4a --- /dev/null +++ b/exercises/practice/flatten-array/flatten-array_test.sql @@ -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;