Skip to content

Commit f6398f5

Browse files
authored
Acronym: add new practice exercise (#102)
1 parent ad4c9ff commit f6398f5

File tree

10 files changed

+187
-0
lines changed

10 files changed

+187
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,14 @@
194194
"prerequisites": [],
195195
"difficulty": 7
196196
},
197+
{
198+
"slug": "acronym",
199+
"name": "Acronym",
200+
"uuid": "880388be-1e77-4092-bcf3-f237572c40e1",
201+
"practices": [],
202+
"prerequisites": [],
203+
"difficulty": 8
204+
},
197205
{
198206
"slug": "armstrong-numbers",
199207
"name": "Armstrong Numbers",
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Instructions
2+
3+
Convert a phrase to its acronym.
4+
5+
Techies love their TLA (Three Letter Acronyms)!
6+
7+
Help generate some jargon by writing a program that converts a long name like Portable Network Graphics to its acronym (PNG).
8+
9+
Punctuation is handled as follows: hyphens are word separators (like whitespace); all other punctuation can be removed from the input.
10+
11+
For example:
12+
13+
| Input | Output |
14+
| ------------------------- | ------ |
15+
| As Soon As Possible | ASAP |
16+
| Liquid-crystal display | LCD |
17+
| Thank George It's Friday! | TGIF |
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"jimmytty"
4+
],
5+
"files": {
6+
"solution": [
7+
"acronym.sql"
8+
],
9+
"test": [
10+
"acronym_test.sql"
11+
],
12+
"example": [
13+
".meta/example.sql"
14+
]
15+
},
16+
"blurb": "Convert a long phrase to its acronym.",
17+
"source": "Julien Vanier",
18+
"source_url": "https://github.com/monkbroc"
19+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
UPDATE acronym
2+
SET result = (
3+
WITH RECURSIVE
4+
rcte(string, abbr) AS (
5+
VALUES(' ' || phrase, '')
6+
UNION ALL
7+
SELECT
8+
SUBSTR(string,2),
9+
abbr ||
10+
CASE
11+
WHEN GLOB('[ _-]', SUBSTR(string, 1, 1))
12+
AND GLOB('[A-Za-z]', SUBSTR(string, 2, 1))
13+
THEN SUBSTR(string, 2, 1)
14+
ELSE ''
15+
END
16+
FROM rcte
17+
WHERE string GLOB '*[ _-]*'
18+
)
19+
SELECT UPPER(abbr) FROM rcte WHERE string NOT GLOB '*[ _-]*'
20+
);
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[1e22cceb-c5e4-4562-9afe-aef07ad1eaf4]
13+
description = "basic"
14+
15+
[79ae3889-a5c0-4b01-baf0-232d31180c08]
16+
description = "lowercase words"
17+
18+
[ec7000a7-3931-4a17-890e-33ca2073a548]
19+
description = "punctuation"
20+
21+
[32dd261c-0c92-469a-9c5c-b192e94a63b0]
22+
description = "all caps word"
23+
24+
[ae2ac9fa-a606-4d05-8244-3bcc4659c1d4]
25+
description = "punctuation without whitespace"
26+
27+
[0e4b1e7c-1a6d-48fb-81a7-bf65eb9e69f9]
28+
description = "very long abbreviation"
29+
30+
[6a078f49-c68d-4b7b-89af-33a1a98c28cc]
31+
description = "consecutive delimiters"
32+
33+
[5118b4b1-4572-434c-8d57-5b762e57973e]
34+
description = "apostrophes"
35+
36+
[adc12eab-ec2d-414f-b48c-66a4fc06cdef]
37+
description = "underscore emphasis"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- Schema: CREATE TABLE acronym ( phrase TEXT PRIMARY KEY, result TEXT );
2+
-- Task: update the acronym table and set the result based on the phrase.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
-- Create database:
2+
.read ./create_fixture.sql
3+
4+
-- Read user student solution and save any output as markdown in user_output.md:
5+
.mode markdown
6+
.output user_output.md
7+
.read ./acronym.sql
8+
.output
9+
10+
-- Create a clean testing environment:
11+
.read ./create_test_table.sql
12+
13+
-- Comparison of user input and the tests updates the status for each test:
14+
UPDATE tests
15+
SET status = 'pass'
16+
FROM (SELECT phrase, result FROM acronym) AS actual
17+
WHERE (actual.phrase, actual.result) = (tests.phrase, tests.expected);
18+
19+
-- Update message for failed tests to give helpful information:
20+
UPDATE tests
21+
SET message = (
22+
'Result for "'
23+
|| tests.phrase
24+
|| '"'
25+
|| ' is <' || COALESCE(actual.result, 'NULL')
26+
|| '> but should be <' || tests.expected || '>'
27+
)
28+
FROM (SELECT phrase, result FROM acronym) AS actual
29+
WHERE actual.phrase = tests.phrase AND tests.status = 'fail';
30+
31+
-- Save results to ./output.json (needed by the online test-runner)
32+
.mode json
33+
.once './output.json'
34+
SELECT description, status, message, output, test_code, task_id
35+
FROM tests;
36+
37+
-- Display test results in readable form for the student:
38+
.mode table
39+
SELECT description, status, message
40+
FROM tests;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
DROP TABLE IF EXISTS acronym;
2+
CREATE TABLE acronym (
3+
phrase TEXT PRIMARY KEY,
4+
result TEXT
5+
);
6+
7+
.mode csv
8+
.import ./data.csv acronym
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
DROP TABLE IF EXISTS tests;
2+
CREATE TABLE IF NOT EXISTS tests (
3+
-- uuid and description are taken from the test.toml file
4+
uuid TEXT PRIMARY KEY,
5+
description TEXT NOT NULL,
6+
-- The following section is needed by the online test-runner
7+
status TEXT DEFAULT 'fail',
8+
message TEXT,
9+
output TEXT,
10+
test_code TEXT,
11+
task_id INTEGER DEFAULT NULL,
12+
-- Here are columns for the actual tests
13+
phrase TEXT NOT NULL,
14+
expected TEXT NOT NULL
15+
);
16+
17+
INSERT INTO tests (uuid, description, phrase, expected)
18+
VALUES
19+
('1e22cceb-c5e4-4562-9afe-aef07ad1eaf4','basic','Portable Network Graphics','PNG'),
20+
('79ae3889-a5c0-4b01-baf0-232d31180c08','lowercase words','Ruby on Rails','ROR'),
21+
('ec7000a7-3931-4a17-890e-33ca2073a548','punctuation','First In, First Out','FIFO'),
22+
('32dd261c-0c92-469a-9c5c-b192e94a63b0','all caps word','GNU Image Manipulation Program','GIMP'),
23+
('ae2ac9fa-a606-4d05-8244-3bcc4659c1d4','punctuation without whitespace','Complementary metal-oxide semiconductor','CMOS'),
24+
('0e4b1e7c-1a6d-48fb-81a7-bf65eb9e69f9','very long abbreviation','Rolling On The Floor Laughing So Hard That My Dogs Came Over And Licked Me','ROTFLSHTMDCOALM'),
25+
('6a078f49-c68d-4b7b-89af-33a1a98c28cc','consecutive delimiters','Something - I made up from thin air','SIMUFTA'),
26+
('5118b4b1-4572-434c-8d57-5b762e57973e','apostrophes','Halley''s Comet','HC'),
27+
('adc12eab-ec2d-414f-b48c-66a4fc06cdef','underscore emphasis','The Road _Not_ Taken','TRNT');

exercises/practice/acronym/data.csv

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"Portable Network Graphics",""
2+
"Ruby on Rails",""
3+
"First In, First Out",""
4+
"GNU Image Manipulation Program",""
5+
"Complementary metal-oxide semiconductor",""
6+
"Rolling On The Floor Laughing So Hard That My Dogs Came Over And Licked Me",""
7+
"Something - I made up from thin air",""
8+
"Halley's Comet",""
9+
"The Road _Not_ Taken",""

0 commit comments

Comments
 (0)