Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,14 @@
"prerequisites": [],
"difficulty": 7
},
{
"slug": "acronym",
"name": "Acronym",
"uuid": "880388be-1e77-4092-bcf3-f237572c40e1",
"practices": [],
"prerequisites": [],
"difficulty": 8
},
{
"slug": "armstrong-numbers",
"name": "Armstrong Numbers",
Expand Down
17 changes: 17 additions & 0 deletions exercises/practice/acronym/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Instructions

Convert a phrase to its acronym.

Techies love their TLA (Three Letter Acronyms)!

Help generate some jargon by writing a program that converts a long name like Portable Network Graphics to its acronym (PNG).

Punctuation is handled as follows: hyphens are word separators (like whitespace); all other punctuation can be removed from the input.

For example:

| Input | Output |
| ------------------------- | ------ |
| As Soon As Possible | ASAP |
| Liquid-crystal display | LCD |
| Thank George It's Friday! | TGIF |
19 changes: 19 additions & 0 deletions exercises/practice/acronym/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"jimmytty"
],
"files": {
"solution": [
"acronym.sql"
],
"test": [
"acronym_test.sql"
],
"example": [
".meta/example.sql"
]
},
"blurb": "Convert a long phrase to its acronym.",
"source": "Julien Vanier",
"source_url": "https://github.com/monkbroc"
}
20 changes: 20 additions & 0 deletions exercises/practice/acronym/.meta/example.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
UPDATE acronym
SET result = (
WITH RECURSIVE
rcte(string, abbr) AS (
VALUES(' ' || phrase, '')
UNION ALL
SELECT
SUBSTR(string,2),
abbr ||
CASE
WHEN GLOB('[ _-]', SUBSTR(string, 1, 1))
AND GLOB('[A-Za-z]', SUBSTR(string, 2, 1))
THEN SUBSTR(string, 2, 1)
ELSE ''
END
FROM rcte
WHERE string GLOB '*[ _-]*'
)
SELECT UPPER(abbr) FROM rcte WHERE string NOT GLOB '*[ _-]*'
);
37 changes: 37 additions & 0 deletions exercises/practice/acronym/.meta/tests.toml
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.

[1e22cceb-c5e4-4562-9afe-aef07ad1eaf4]
description = "basic"

[79ae3889-a5c0-4b01-baf0-232d31180c08]
description = "lowercase words"

[ec7000a7-3931-4a17-890e-33ca2073a548]
description = "punctuation"

[32dd261c-0c92-469a-9c5c-b192e94a63b0]
description = "all caps word"

[ae2ac9fa-a606-4d05-8244-3bcc4659c1d4]
description = "punctuation without whitespace"

[0e4b1e7c-1a6d-48fb-81a7-bf65eb9e69f9]
description = "very long abbreviation"

[6a078f49-c68d-4b7b-89af-33a1a98c28cc]
description = "consecutive delimiters"

[5118b4b1-4572-434c-8d57-5b762e57973e]
description = "apostrophes"

[adc12eab-ec2d-414f-b48c-66a4fc06cdef]
description = "underscore emphasis"
2 changes: 2 additions & 0 deletions exercises/practice/acronym/acronym.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- Schema: CREATE TABLE acronym ( phrase TEXT PRIMARY KEY, result TEXT );
-- Task: update the acronym table and set the result based on the phrase.
40 changes: 40 additions & 0 deletions exercises/practice/acronym/acronym_test.sql
Original file line number Diff line number Diff line change
@@ -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 ./acronym.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 phrase, result FROM acronym) AS actual
WHERE (actual.phrase, actual.result) = (tests.phrase, tests.expected);

-- Update message for failed tests to give helpful information:
UPDATE tests
SET message = (
'Result for "'
|| tests.phrase
|| '"'
|| ' is <' || COALESCE(actual.result, 'NULL')
|| '> but should be <' || tests.expected || '>'
)
FROM (SELECT phrase, result FROM acronym) AS actual
WHERE actual.phrase = tests.phrase 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;
8 changes: 8 additions & 0 deletions exercises/practice/acronym/create_fixture.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
DROP TABLE IF EXISTS acronym;
CREATE TABLE acronym (
phrase TEXT PRIMARY KEY,
result TEXT
);

.mode csv
.import ./data.csv acronym
27 changes: 27 additions & 0 deletions exercises/practice/acronym/create_test_table.sql
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
phrase TEXT NOT NULL,
expected TEXT NOT NULL
);

INSERT INTO tests (uuid, description, phrase, expected)
VALUES
('1e22cceb-c5e4-4562-9afe-aef07ad1eaf4','basic','Portable Network Graphics','PNG'),
('79ae3889-a5c0-4b01-baf0-232d31180c08','lowercase words','Ruby on Rails','ROR'),
('ec7000a7-3931-4a17-890e-33ca2073a548','punctuation','First In, First Out','FIFO'),
('32dd261c-0c92-469a-9c5c-b192e94a63b0','all caps word','GNU Image Manipulation Program','GIMP'),
('ae2ac9fa-a606-4d05-8244-3bcc4659c1d4','punctuation without whitespace','Complementary metal-oxide semiconductor','CMOS'),
('0e4b1e7c-1a6d-48fb-81a7-bf65eb9e69f9','very long abbreviation','Rolling On The Floor Laughing So Hard That My Dogs Came Over And Licked Me','ROTFLSHTMDCOALM'),
('6a078f49-c68d-4b7b-89af-33a1a98c28cc','consecutive delimiters','Something - I made up from thin air','SIMUFTA'),
('5118b4b1-4572-434c-8d57-5b762e57973e','apostrophes','Halley''s Comet','HC'),
('adc12eab-ec2d-414f-b48c-66a4fc06cdef','underscore emphasis','The Road _Not_ Taken','TRNT');
9 changes: 9 additions & 0 deletions exercises/practice/acronym/data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"Portable Network Graphics",""
"Ruby on Rails",""
"First In, First Out",""
"GNU Image Manipulation Program",""
"Complementary metal-oxide semiconductor",""
"Rolling On The Floor Laughing So Hard That My Dogs Came Over And Licked Me",""
"Something - I made up from thin air",""
"Halley's Comet",""
"The Road _Not_ Taken",""