Skip to content

Commit 066f081

Browse files
authored
Auto-format .sql files using github.com/sql-formatter-org/sql-formatter - anagram (#141)
1 parent 7c059b7 commit 066f081

File tree

5 files changed

+280
-96
lines changed

5 files changed

+280
-96
lines changed
Lines changed: 86 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,92 @@
11
DROP TABLE IF EXISTS tmp;
2+
23
CREATE TEMPORARY TABLE tmp AS
3-
SELECT CAST(word AS TEXT) word FROM (
4-
SELECT LOWER(subject) word FROM anagram
5-
UNION
6-
SELECT LOWER(j.VALUE)
7-
FROM anagram, JSON_EACH(candidates) j
4+
SELECT
5+
CAST(word AS TEXT) word
6+
FROM
7+
(
8+
SELECT
9+
LOWER(subject) word
10+
FROM
11+
anagram
12+
UNION
13+
SELECT
14+
LOWER(j.VALUE)
15+
FROM
16+
anagram,
17+
JSON_EACH(candidates) j
818
);
9-
ALTER TABLE tmp ADD s TEXT;
19+
20+
ALTER TABLE tmp
21+
ADD s TEXT;
22+
1023
UPDATE tmp
11-
SET s = (
12-
WITH RECURSIVE rcte(word,c) AS (
13-
VALUES(word, '')
14-
UNION ALL
15-
SELECT SUBSTR(word, 2), SUBSTR(word, 1, 1)
16-
FROM rcte
17-
WHERE LENGTH(word) > 0
18-
)
19-
SELECT GROUP_CONCAT(c, '')
20-
FROM (
21-
SELECT c
22-
FROM rcte
23-
WHERE UNICODE(c) BETWEEN UNICODE('a') AND UNICODE('z')
24-
ORDER BY c
25-
)
26-
);
24+
SET
25+
s = (
26+
WITH RECURSIVE
27+
rcte (word, c) AS (
28+
VALUES
29+
(word, '')
30+
UNION ALL
31+
SELECT
32+
SUBSTR(word, 2),
33+
SUBSTR(word, 1, 1)
34+
FROM
35+
rcte
36+
WHERE
37+
LENGTH(word) > 0
38+
)
39+
SELECT
40+
GROUP_CONCAT(c, '')
41+
FROM
42+
(
43+
SELECT
44+
c
45+
FROM
46+
rcte
47+
WHERE
48+
UNICODE(c) BETWEEN UNICODE('a') AND UNICODE('z')
49+
ORDER BY
50+
c
51+
)
52+
);
2753

2854
UPDATE anagram
29-
SET result = (
30-
SELECT JSON_GROUP_ARRAY(candidate)
31-
FROM (
32-
SELECT subject, candidate,
33-
(SELECT s FROM tmp WHERE LOWER(subject) = word) AS a,
34-
(SELECT s FROM tmp WHERE LOWER(candidate) = word) AS b
35-
FROM (
36-
SELECT subject,
37-
j.VALUE AS candidate
38-
FROM JSON_EACH(candidates) AS j
39-
)
40-
)
41-
WHERE a = b
42-
AND LOWER(subject) <> LOWER(candidate)
43-
);
55+
SET
56+
result = (
57+
SELECT
58+
JSON_GROUP_ARRAY(candidate)
59+
FROM
60+
(
61+
SELECT
62+
subject,
63+
candidate,
64+
(
65+
SELECT
66+
s
67+
FROM
68+
tmp
69+
WHERE
70+
LOWER(subject) = word
71+
) AS a,
72+
(
73+
SELECT
74+
s
75+
FROM
76+
tmp
77+
WHERE
78+
LOWER(candidate) = word
79+
) AS b
80+
FROM
81+
(
82+
SELECT
83+
subject,
84+
j.VALUE AS candidate
85+
FROM
86+
JSON_EACH(candidates) AS j
87+
)
88+
)
89+
WHERE
90+
a = b
91+
AND LOWER(subject) <> LOWER(candidate)
92+
);

exercises/practice/anagram/anagram.sql

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
-- candidates TEXT NOT NULL, -- json array of strings
44
-- result TEXT -- json array of strings
55
-- );
6-
76
-- Task: update the anagram table and set the result based on valid
87
-- candidates for the subject field
98
-- * the candidates column contains a JSON-encoded list of strings.
Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,78 @@
11
-- Create database:
22
.read ./create_fixture.sql
3-
43
-- Read user student solution and save any output as markdown in user_output.md:
54
.mode markdown
65
.output user_output.md
76
.read ./anagram.sql
87
.output
9-
108
-- Create a clean testing environment:
119
.read ./create_test_table.sql
12-
1310
-- Comparison of user input and the tests updates the status for each test:
1411
UPDATE tests
15-
SET status = 'pass'
16-
FROM (SELECT subject, candidates, result FROM anagram) AS actual
17-
WHERE (actual.subject, actual.candidates, JSON(actual.result)) = (tests.subject, tests.candidates, JSON(tests.expected));
12+
SET
13+
status = 'pass'
14+
FROM
15+
(
16+
SELECT
17+
subject,
18+
candidates,
19+
result
20+
FROM
21+
anagram
22+
) AS actual
23+
WHERE
24+
(
25+
actual.subject,
26+
actual.candidates,
27+
JSON(actual.result)
28+
) = (
29+
tests.subject,
30+
tests.candidates,
31+
JSON(tests.expected)
32+
);
1833

1934
-- Update message for failed tests to give helpful information:
2035
UPDATE tests
21-
SET message = (
22-
'Result for "'
23-
|| JSON_OBJECT(
24-
'subject', tests.subject,
25-
'candidates', JSON(tests.candidates)
26-
)
27-
|| '"'
28-
|| ' is <' || COALESCE(actual.result, 'NULL')
29-
|| '> but should be <' || tests.expected || '>'
30-
)
31-
FROM (SELECT subject, candidates, result FROM anagram) AS actual
32-
WHERE (actual.subject, actual.candidates) = (tests.subject, tests.candidates) AND tests.status = 'fail';
36+
SET
37+
message = (
38+
'Result for "' || JSON_OBJECT(
39+
'subject',
40+
tests.subject,
41+
'candidates',
42+
JSON(tests.candidates)
43+
) || '"' || ' is <' || COALESCE(actual.result, 'NULL') || '> but should be <' || tests.expected || '>'
44+
)
45+
FROM
46+
(
47+
SELECT
48+
subject,
49+
candidates,
50+
result
51+
FROM
52+
anagram
53+
) AS actual
54+
WHERE
55+
(actual.subject, actual.candidates) = (tests.subject, tests.candidates)
56+
AND tests.status = 'fail';
3357

3458
-- Save results to ./output.json (needed by the online test-runner)
3559
.mode json
3660
.once './output.json'
37-
SELECT description, status, message, output, test_code, task_id
38-
FROM tests;
61+
SELECT
62+
description,
63+
status,
64+
message,
65+
output,
66+
test_code,
67+
task_id
68+
FROM
69+
tests;
3970

4071
-- Display test results in readable form for the student:
4172
.mode table
42-
SELECT description, status, message
43-
FROM tests;
73+
SELECT
74+
description,
75+
status,
76+
message
77+
FROM
78+
tests;
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
DROP TABLE IF EXISTS anagram;
2+
23
CREATE TABLE anagram (
3-
subject TEXT NOT NULL,
4-
candidates TEXT NOT NULL, -- json array of strings
5-
result TEXT -- json array of strings
4+
subject TEXT NOT NULL,
5+
candidates TEXT NOT NULL, -- json array of strings
6+
result TEXT -- json array of strings
67
);
8+
79
.mode csv
810
.import ./data.csv anagram
9-
10-
UPDATE anagram SET result = NULL;
11+
UPDATE anagram
12+
SET
13+
result = NULL;

0 commit comments

Comments
 (0)