-
-
Notifications
You must be signed in to change notification settings - Fork 13
Add practice exercise: saddle-points #135
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d81f66d
* add practice exercise: saddle-points
jimmytty df01f40
* moving info from stub file to instructions.append.md
jimmytty 3f9be44
* indentation
jimmytty 7bed9d9
Update exercises/practice/saddle-points/.docs/instructions.append.md
jimmytty 07319d8
Update exercises/practice/saddle-points/.docs/instructions.append.md
jimmytty 38f13c6
* indentation
jimmytty 6c1192a
Merge branch 'main' into saddle-points
jimmytty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
exercises/practice/saddle-points/.docs/instructions.append.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# SQLite-specific instructions | ||
|
||
- The **matrix** column contains the input, a JSON-encoded array of arrays of integers. | ||
- The **result** columns should contain the output, the list of saddle points. | ||
The output should be a JSON-encoded array of objects, each object has two keys: row and column. | ||
For example, `[{"row": 2, "column": 1}]`. | ||
|
||
## JSON documentation | ||
|
||
See [JSON Functions And Operators][json-docs] for SQLite JSON functions. | ||
|
||
[json-docs]: https://www.sqlite.org/json1.html |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Instructions | ||
|
||
Your task is to find the potential trees where you could build your tree house. | ||
|
||
The data company provides the data as grids that show the heights of the trees. | ||
The rows of the grid represent the east-west direction, and the columns represent the north-south direction. | ||
|
||
An acceptable tree will be the largest in its row, while being the smallest in its column. | ||
|
||
A grid might not have any good trees at all. | ||
Or it might have one, or even several. | ||
|
||
Here is a grid that has exactly one candidate tree. | ||
|
||
```text | ||
↓ | ||
1 2 3 4 | ||
|----------- | ||
1 | 9 8 7 8 | ||
→ 2 |[5] 3 2 4 | ||
3 | 6 6 7 1 | ||
``` | ||
|
||
- Row 2 has values 5, 3, 2, and 4. The largest value is 5. | ||
- Column 1 has values 9, 5, and 6. The smallest value is 5. | ||
|
||
So the point at `[2, 1]` (row: 2, column: 1) is a great spot for a tree house. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Introduction | ||
|
||
You plan to build a tree house in the woods near your house so that you can watch the sun rise and set. | ||
|
||
You've obtained data from a local survey company that show the height of every tree in each rectangular section of the map. | ||
You need to analyze each grid on the map to find good trees for your tree house. | ||
|
||
A good tree is both: | ||
|
||
- taller than every tree to the east and west, so that you have the best possible view of the sunrises and sunsets. | ||
- shorter than every tree to the north and south, to minimize the amount of tree climbing. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"authors": [ | ||
"jimmytty" | ||
], | ||
"files": { | ||
"solution": [ | ||
"saddle-points.sql" | ||
], | ||
"test": [ | ||
"saddle-points_test.sql" | ||
], | ||
"example": [ | ||
".meta/example.sql" | ||
] | ||
}, | ||
"blurb": "Detect saddle points in a matrix.", | ||
"source": "J Dalbey's Programming Practice problems", | ||
"source_url": "https://users.csc.calpoly.edu/~jdalbey/103/Projects/ProgrammingPractice.html" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
UPDATE "saddle-points" | ||
SET result = ( | ||
WITH | ||
jtree AS ( | ||
SELECT *, | ||
SUBSTR(fullkey, 1, INSTR(fullkey, ']')) rowkey, | ||
SUBSTR(fullkey, INSTR(fullkey, '][')) colkey | ||
FROM JSON_TREE(matrix) j | ||
), | ||
max_single_in_row AS ( | ||
SELECT MAX(value) value, | ||
fullkey, | ||
rowkey | ||
FROM jtree | ||
WHERE type = 'integer' | ||
GROUP BY rowkey | ||
), | ||
max_mult_in_row AS ( | ||
SELECT jtree.VALUE, jtree.fullkey, ms.rowkey | ||
FROM max_single_in_row ms, jtree | ||
WHERE ms.rowkey = jtree.rowkey | ||
AND ms.value = jtree.value | ||
), | ||
min_single_in_col AS ( | ||
SELECT MIN(value) value, fullkey, colkey | ||
FROM jtree | ||
WHERE type = 'integer' | ||
GROUP BY colkey | ||
), | ||
min_mult_in_col AS ( | ||
SELECT jtree.VALUE, jtree.fullkey, ms.colkey | ||
FROM min_single_in_col ms, jtree | ||
WHERE ms.colkey = jtree.colkey | ||
AND ms.value = jtree.VALUE | ||
), | ||
get_coords AS ( | ||
SELECT JSON(REPLACE(LTRIM(mc.fullkey, '$'), '][', ',')) coords | ||
FROM max_mult_in_row mr, | ||
min_mult_in_col mc | ||
WHERE mr.fullkey = mc.fullkey | ||
), | ||
to_objects AS ( | ||
SELECT JSON_OBJECT( | ||
'row', JSON_EXTRACT(coords, '$[0]') + 1, | ||
'column', JSON_EXTRACT(coords, '$[1]') + 1 | ||
) AS jobj | ||
FROM get_coords | ||
) | ||
SELECT JSON_GROUP_ARRAY(JSON(jobj)) | ||
FROM to_objects | ||
) | ||
; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# 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. | ||
|
||
[3e374e63-a2e0-4530-a39a-d53c560382bd] | ||
description = "Can identify single saddle point" | ||
|
||
[6b501e2b-6c1f-491f-b1bb-7f278f760534] | ||
description = "Can identify that empty matrix has no saddle points" | ||
|
||
[8c27cc64-e573-4fcb-a099-f0ae863fb02f] | ||
description = "Can identify lack of saddle points when there are none" | ||
|
||
[6d1399bd-e105-40fd-a2c9-c6609507d7a3] | ||
description = "Can identify multiple saddle points in a column" | ||
|
||
[3e81dce9-53b3-44e6-bf26-e328885fd5d1] | ||
description = "Can identify multiple saddle points in a row" | ||
|
||
[88868621-b6f4-4837-bb8b-3fad8b25d46b] | ||
description = "Can identify saddle point in bottom right corner" | ||
|
||
[5b9499ca-fcea-4195-830a-9c4584a0ee79] | ||
description = "Can identify saddle points in a non square matrix" | ||
|
||
[ee99ccd2-a1f1-4283-ad39-f8c70f0cf594] | ||
description = "Can identify that saddle points in a single column matrix are those with the minimum value" | ||
|
||
[63abf709-a84b-407f-a1b3-456638689713] | ||
description = "Can identify that saddle points in a single row matrix are those with the maximum value" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
DROP TABLE IF EXISTS "saddle-points"; | ||
CREATE TABLE "saddle-points" ( | ||
matrix TEXT NOT NULL, -- json array of arrays | ||
result TEXT -- json array of objects | ||
); | ||
|
||
.mode csv | ||
.import ./data.csv "saddle-points" | ||
|
||
UPDATE "saddle-points" SET result = NULL; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
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 | ||
matrix TEXT NOT NULL, -- json array of arrays | ||
expected TEXT NOT NULL -- json array of objects | ||
); | ||
|
||
INSERT INTO tests (uuid, description, matrix, expected) | ||
VALUES | ||
('3e374e63-a2e0-4530-a39a-d53c560382bd', 'Can identify single saddle point', '[[9,8,7],[5,3,2],[6,6,7]]', '[{"row":2,"column":1}]'), | ||
('6b501e2b-6c1f-491f-b1bb-7f278f760534', 'Can identify that empty matrix has no saddle points', '[[]]', '[]'), | ||
('8c27cc64-e573-4fcb-a099-f0ae863fb02f', 'Can identify lack of saddle points when there are none', '[[1,2,3],[3,1,2],[2,3,1]]', '[]'), | ||
('6d1399bd-e105-40fd-a2c9-c6609507d7a3', 'Can identify multiple saddle points in a column', '[[4,5,4],[3,5,5],[1,5,4]]', '[{"row":1,"column":2},{"row":2,"column":2},{"row":3,"column":2}]'), | ||
('3e81dce9-53b3-44e6-bf26-e328885fd5d1', 'Can identify multiple saddle points in a row', '[[6,7,8],[5,5,5],[7,5,6]]', '[{"row":2,"column":1},{"row":2,"column":2},{"row":2,"column":3}]'), | ||
('88868621-b6f4-4837-bb8b-3fad8b25d46b', 'Can identify saddle point in bottom right corner', '[[8,7,9],[6,7,6],[3,2,5]]', '[{"row":3,"column":3}]'), | ||
('5b9499ca-fcea-4195-830a-9c4584a0ee79', 'Can identify saddle points in a non square matrix', '[[3,1,3],[3,2,4]]', '[{"row":1,"column":3},{"row":1,"column":1}]'), | ||
('ee99ccd2-a1f1-4283-ad39-f8c70f0cf594', 'Can identify that saddle points in a single column matrix are those with the minimum value', '[[2],[1],[4],[1]]', '[{"row":2,"column":1},{"row":4,"column":1}]'), | ||
('63abf709-a84b-407f-a1b3-456638689713', 'Can identify that saddle points in a single row matrix are those with the maximum value', '[[2,5,3,5]]', '[{"row":1,"column":2},{"row":1,"column":4}]'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
"[[9,8,7],[5,3,2],[6,6,7]]", | ||
"[[]]", | ||
"[[1,2,3],[3,1,2],[2,3,1]]", | ||
"[[4,5,4],[3,5,5],[1,5,4]]", | ||
"[[6,7,8],[5,5,5],[7,5,6]]", | ||
"[[8,7,9],[6,7,6],[3,2,5]]", | ||
"[[3,1,3],[3,2,4]]", | ||
"[[2],[1],[4],[1]]", | ||
"[[2,5,3,5]]", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
-- Schema: | ||
-- CREATE TABLE "saddle-points" ( | ||
-- matrix TEXT NOT NULL, -- json array of arrays | ||
-- result TEXT -- json array of object | ||
-- ); | ||
-- Task: update the saddle-points table and set the result based on the matrix. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
-- 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 ./saddle-points.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 matrix, result FROM "saddle-points") AS actual | ||
WHERE actual.matrix = tests.matrix | ||
AND IIF( | ||
actual.matrix ISNULL, ( | ||
SELECT JSON_GROUP_ARRAY(JSON(value)) | ||
FROM ( | ||
SELECT j.value | ||
FROM JSON_EACH(actual.result) j | ||
ORDER BY j.VALUE | ||
) | ||
), | ||
actual.result | ||
) = ( | ||
SELECT JSON_GROUP_ARRAY(JSON(value)) | ||
FROM ( | ||
SELECT j.VALUE | ||
FROM JSON_EACH(tests.expected) j | ||
ORDER BY j.VALUE | ||
) | ||
) | ||
; | ||
|
||
-- Update message for failed tests to give helpful information: | ||
UPDATE tests | ||
SET message = ( | ||
'Result for "' || tests.matrix || '"' | ||
|| ' is <' || COALESCE(actual.result, 'NULL') | ||
|| '> but should be <' || tests.expected || '>' | ||
) | ||
FROM (SELECT matrix, result FROM "saddle-points") AS actual | ||
WHERE actual.matrix = tests.matrix 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; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.