From 4232ca7163d34b2d29c81edb7c0ca3bcdcd28ef2 Mon Sep 17 00:00:00 2001 From: Ronaldo Ferreira de Lima Date: Tue, 19 Aug 2025 23:42:02 -0300 Subject: [PATCH 1/3] * add practice exercise: matrix --- config.json | 8 ++++ .../matrix/.docs/instructions.append.md | 25 ++++++++++++ .../practice/matrix/.docs/instructions.md | 38 ++++++++++++++++++ exercises/practice/matrix/.meta/config.json | 19 +++++++++ exercises/practice/matrix/.meta/example.sql | 22 ++++++++++ exercises/practice/matrix/.meta/tests.toml | 34 ++++++++++++++++ exercises/practice/matrix/create_fixture.sql | 13 ++++++ .../practice/matrix/create_test_table.sql | 30 ++++++++++++++ exercises/practice/matrix/data.csv | 8 ++++ exercises/practice/matrix/matrix.sql | 9 +++++ exercises/practice/matrix/matrix_test.sql | 40 +++++++++++++++++++ 11 files changed, 246 insertions(+) create mode 100644 exercises/practice/matrix/.docs/instructions.append.md create mode 100644 exercises/practice/matrix/.docs/instructions.md create mode 100644 exercises/practice/matrix/.meta/config.json create mode 100644 exercises/practice/matrix/.meta/example.sql create mode 100644 exercises/practice/matrix/.meta/tests.toml create mode 100644 exercises/practice/matrix/create_fixture.sql create mode 100644 exercises/practice/matrix/create_test_table.sql create mode 100644 exercises/practice/matrix/data.csv create mode 100644 exercises/practice/matrix/matrix.sql create mode 100644 exercises/practice/matrix/matrix_test.sql diff --git a/config.json b/config.json index ea86b75..dcc41f8 100644 --- a/config.json +++ b/config.json @@ -202,6 +202,14 @@ "prerequisites": [], "difficulty": 5 }, + { + "slug": "matrix", + "name": "Matrix", + "uuid": "6884edbe-4a2b-4415-8aa0-57c959b200c8", + "practices": [], + "prerequisites": [], + "difficulty": 5 + }, { "slug": "meetup", "name": "Meetup", diff --git a/exercises/practice/matrix/.docs/instructions.append.md b/exercises/practice/matrix/.docs/instructions.append.md new file mode 100644 index 0000000..489daa0 --- /dev/null +++ b/exercises/practice/matrix/.docs/instructions.append.md @@ -0,0 +1,25 @@ +# SQLite-specific instructions + +The **result** column contains a JSON-encoded list of integers. + +Example: +```json +[9,8,7] +``` + +## Table Schema + +```sql +CREATE TABLE matrix ( + property TEXT NOT NULL, + string TEXT NOT NULL, + "index" INTEGER NOT NULL, + result TEXT -- json array of integers +); +``` + +## JSON documentation + +[JSON Functions And Operators][json-docs] + +[json-docs]: https://www.sqlite.org/json1.html diff --git a/exercises/practice/matrix/.docs/instructions.md b/exercises/practice/matrix/.docs/instructions.md new file mode 100644 index 0000000..dadea8a --- /dev/null +++ b/exercises/practice/matrix/.docs/instructions.md @@ -0,0 +1,38 @@ +# Instructions + +Given a string representing a matrix of numbers, return the rows and columns of that matrix. + +So given a string with embedded newlines like: + +```text +9 8 7 +5 3 2 +6 6 7 +``` + +representing this matrix: + +```text + 1 2 3 + |--------- +1 | 9 8 7 +2 | 5 3 2 +3 | 6 6 7 +``` + +your code should be able to spit out: + +- A list of the rows, reading each row left-to-right while moving top-to-bottom across the rows, +- A list of the columns, reading each column top-to-bottom while moving from left-to-right. + +The rows for our example matrix: + +- 9, 8, 7 +- 5, 3, 2 +- 6, 6, 7 + +And its columns: + +- 9, 5, 6 +- 8, 3, 6 +- 7, 2, 7 diff --git a/exercises/practice/matrix/.meta/config.json b/exercises/practice/matrix/.meta/config.json new file mode 100644 index 0000000..852870e --- /dev/null +++ b/exercises/practice/matrix/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "jimmytty" + ], + "files": { + "solution": [ + "matrix.sql" + ], + "test": [ + "matrix_test.sql" + ], + "example": [ + ".meta/example.sql" + ] + }, + "blurb": "Given a string representing a matrix of numbers, return the rows and columns of that matrix.", + "source": "Exercise by the JumpstartLab team for students at The Turing School of Software and Design.", + "source_url": "https://turing.edu" +} diff --git a/exercises/practice/matrix/.meta/example.sql b/exercises/practice/matrix/.meta/example.sql new file mode 100644 index 0000000..f0e84bc --- /dev/null +++ b/exercises/practice/matrix/.meta/example.sql @@ -0,0 +1,22 @@ +UPDATE matrix + SET result = ( + SELECT j.value + FROM JSON_EACH( + PRINTF('[[%s]]', REPLACE(REPLACE(string, ' ', ','), CHAR(10), '],[')) + ) j + WHERE j.key = "index" - 1 + ) + WHERE property = 'row' +; + +UPDATE matrix + SET result = ( + SELECT JSON_GROUP_ARRAY( + JSON_EXTRACT(j.value, PRINTF('$[%d]', "index" - 1)) + ) + FROM JSON_EACH( + PRINTF('[[%s]]', REPLACE(REPLACE(string, ' ', ','), CHAR(10), '],[')) + ) j + ) + WHERE property = 'column' +; diff --git a/exercises/practice/matrix/.meta/tests.toml b/exercises/practice/matrix/.meta/tests.toml new file mode 100644 index 0000000..90b509c --- /dev/null +++ b/exercises/practice/matrix/.meta/tests.toml @@ -0,0 +1,34 @@ +# 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. + +[ca733dab-9d85-4065-9ef6-a880a951dafd] +description = "extract row from one number matrix" + +[5c93ec93-80e1-4268-9fc2-63bc7d23385c] +description = "can extract row" + +[2f1aad89-ad0f-4bd2-9919-99a8bff0305a] +description = "extract row where numbers have different widths" + +[68f7f6ba-57e2-4e87-82d0-ad09889b5204] +description = "can extract row from non-square matrix with no corresponding column" + +[e8c74391-c93b-4aed-8bfe-f3c9beb89ebb] +description = "extract column from one number matrix" + +[7136bdbd-b3dc-48c4-a10c-8230976d3727] +description = "can extract column" + +[ad64f8d7-bba6-4182-8adf-0c14de3d0eca] +description = "can extract column from non-square matrix with no corresponding row" + +[9eddfa5c-8474-440e-ae0a-f018c2a0dd89] +description = "extract column where numbers have different widths" diff --git a/exercises/practice/matrix/create_fixture.sql b/exercises/practice/matrix/create_fixture.sql new file mode 100644 index 0000000..1850489 --- /dev/null +++ b/exercises/practice/matrix/create_fixture.sql @@ -0,0 +1,13 @@ +DROP TABLE IF EXISTS matrix; +CREATE TABLE matrix ( + property TEXT NOT NULL, + string TEXT NOT NULL, + "index" INTEGER NOT NULL, + result TEXT -- json array of integers +); + +.mode csv +.import ./data.csv matrix + +UPDATE matrix SET result = NULL; +UPDATE matrix SET string = REPLACE(string, '\n', CHAR(10)); diff --git a/exercises/practice/matrix/create_test_table.sql b/exercises/practice/matrix/create_test_table.sql new file mode 100644 index 0000000..bc031c7 --- /dev/null +++ b/exercises/practice/matrix/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 + property TEXT NOT NULL, + string TEXT NOT NULL, + "index" INTEGER NOT NULL, + expected TEXT NOT NULL -- json array of integers +); + +INSERT INTO tests (uuid, description, property, string, "index", expected) +VALUES + ('ca733dab-9d85-4065-9ef6-a880a951dafd', 'extract row from one number matrix', 'row', '1', 1, '[1]'), + ('5c93ec93-80e1-4268-9fc2-63bc7d23385c', 'can extract row', 'row', '1 2\n3 4', 2, '[3,4]'), + ('2f1aad89-ad0f-4bd2-9919-99a8bff0305a', 'extract row where numbers have different widths', 'row', '1 2\n10 20', 2, '[10,20]'), + ('68f7f6ba-57e2-4e87-82d0-ad09889b5204', 'can extract row from non-square matrix with no corresponding column', 'row', '1 2 3\n4 5 6\n7 8 9\n8 7 6', 4, '[8,7,6]'), + ('e8c74391-c93b-4aed-8bfe-f3c9beb89ebb', 'extract column from one number matrix', 'column', '1', 1, '[1]'), + ('7136bdbd-b3dc-48c4-a10c-8230976d3727', 'can extract column', 'column', '1 2 3\n4 5 6\n7 8 9', 3, '[3,6,9]'), + ('ad64f8d7-bba6-4182-8adf-0c14de3d0eca', 'can extract column from non-square matrix with no corresponding row', 'column', '1 2 3 4\n5 6 7 8\n9 8 7 6', 4, '[4,8,6]'), + ('9eddfa5c-8474-440e-ae0a-f018c2a0dd89', 'extract column where numbers have different widths', 'column', '89 1903 3\n18 3 1\n9 4 800', 2, '[1903,3,4]'); + +UPDATE tests SET string = REPLACE(string, '\n', CHAR(10)); diff --git a/exercises/practice/matrix/data.csv b/exercises/practice/matrix/data.csv new file mode 100644 index 0000000..4e22e78 --- /dev/null +++ b/exercises/practice/matrix/data.csv @@ -0,0 +1,8 @@ +"row","1",1, +"row","1 2\n3 4",2, +"row","1 2\n10 20",2, +"row","1 2 3\n4 5 6\n7 8 9\n8 7 6",4, +"column","1",1, +"column","1 2 3\n4 5 6\n7 8 9",3, +"column","1 2 3 4\n5 6 7 8\n9 8 7 6",4, +"column","89 1903 3\n18 3 1\n9 4 800",2, diff --git a/exercises/practice/matrix/matrix.sql b/exercises/practice/matrix/matrix.sql new file mode 100644 index 0000000..874d1e7 --- /dev/null +++ b/exercises/practice/matrix/matrix.sql @@ -0,0 +1,9 @@ +-- Schema: +-- CREATE TABLE matrix ( +-- property TEXT NOT NULL, +-- string TEXT NOT NULL, +-- "index" INTEGER NOT NULL, +-- result TEXT -- json array of integers +-- ); +-- +-- Task: update the matrix table and set the result based on the property, string and index. diff --git a/exercises/practice/matrix/matrix_test.sql b/exercises/practice/matrix/matrix_test.sql new file mode 100644 index 0000000..304c969 --- /dev/null +++ b/exercises/practice/matrix/matrix_test.sql @@ -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 ./matrix.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 property, string, "index", result FROM matrix) AS actual +WHERE (actual.property, actual.string, actual."index", JSON(actual.result)) = (tests.property, tests.string, tests."index", JSON(tests.expected)); + +-- Update message for failed tests to give helpful information: +UPDATE tests +SET message = ( + 'Result for "' + || PRINTF('property=%s,string=%s,index=%d', tests.property, tests.string, tests."index") + || '"' + || ' is <' || COALESCE(actual.result, 'NULL') + || '> but should be <' || tests.expected || '>' +) +FROM (SELECT property, string, "index", result FROM matrix) AS actual +WHERE (actual.property, actual.string, actual."index") = (tests.property, tests.string, tests."index") 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; From e46c30b960adde777103cc7e46fb388e4431fea4 Mon Sep 17 00:00:00 2001 From: Ronaldo Ferreira de Lima Date: Wed, 20 Aug 2025 22:38:21 -0300 Subject: [PATCH 2/3] Update exercises/practice/matrix/matrix.sql Co-authored-by: Isaac Good --- exercises/practice/matrix/matrix.sql | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/exercises/practice/matrix/matrix.sql b/exercises/practice/matrix/matrix.sql index 874d1e7..2616e9c 100644 --- a/exercises/practice/matrix/matrix.sql +++ b/exercises/practice/matrix/matrix.sql @@ -1,9 +1,9 @@ -- Schema: -- CREATE TABLE matrix ( --- property TEXT NOT NULL, --- string TEXT NOT NULL, --- "index" INTEGER NOT NULL, --- result TEXT -- json array of integers +-- string TEXT NOT NULL, -- the matrix as a plain string +-- property TEXT NOT NULL, -- either "row" or "column" which we want to extract +-- "index" INTEGER NOT NULL, -- the row or column index to extract +-- result TEXT -- json array of integers containing the row or column at the specified index -- ); -- -- Task: update the matrix table and set the result based on the property, string and index. From 23c22f523c2904af31277615f1c05c6e5705e023 Mon Sep 17 00:00:00 2001 From: Ronaldo Ferreira de Lima Date: Wed, 20 Aug 2025 22:51:35 -0300 Subject: [PATCH 3/3] * removing duplicate info from instructions.append.md --- .../matrix/.docs/instructions.append.md | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/exercises/practice/matrix/.docs/instructions.append.md b/exercises/practice/matrix/.docs/instructions.append.md index 489daa0..27d1e44 100644 --- a/exercises/practice/matrix/.docs/instructions.append.md +++ b/exercises/practice/matrix/.docs/instructions.append.md @@ -1,23 +1,5 @@ # SQLite-specific instructions -The **result** column contains a JSON-encoded list of integers. - -Example: -```json -[9,8,7] -``` - -## Table Schema - -```sql -CREATE TABLE matrix ( - property TEXT NOT NULL, - string TEXT NOT NULL, - "index" INTEGER NOT NULL, - result TEXT -- json array of integers -); -``` - ## JSON documentation [JSON Functions And Operators][json-docs]