diff --git a/config.json b/config.json index 6cc5eac..6057fd6 100644 --- a/config.json +++ b/config.json @@ -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", diff --git a/exercises/practice/acronym/.docs/instructions.md b/exercises/practice/acronym/.docs/instructions.md new file mode 100644 index 0000000..133bd2c --- /dev/null +++ b/exercises/practice/acronym/.docs/instructions.md @@ -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 | diff --git a/exercises/practice/acronym/.meta/config.json b/exercises/practice/acronym/.meta/config.json new file mode 100644 index 0000000..458dfab --- /dev/null +++ b/exercises/practice/acronym/.meta/config.json @@ -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" +} diff --git a/exercises/practice/acronym/.meta/example.sql b/exercises/practice/acronym/.meta/example.sql new file mode 100644 index 0000000..a26eb24 --- /dev/null +++ b/exercises/practice/acronym/.meta/example.sql @@ -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 '*[ _-]*' + ); diff --git a/exercises/practice/acronym/.meta/tests.toml b/exercises/practice/acronym/.meta/tests.toml new file mode 100644 index 0000000..6e3277c --- /dev/null +++ b/exercises/practice/acronym/.meta/tests.toml @@ -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" diff --git a/exercises/practice/acronym/acronym.sql b/exercises/practice/acronym/acronym.sql new file mode 100644 index 0000000..9dfe741 --- /dev/null +++ b/exercises/practice/acronym/acronym.sql @@ -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. diff --git a/exercises/practice/acronym/acronym_test.sql b/exercises/practice/acronym/acronym_test.sql new file mode 100644 index 0000000..caa841f --- /dev/null +++ b/exercises/practice/acronym/acronym_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 ./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; diff --git a/exercises/practice/acronym/create_fixture.sql b/exercises/practice/acronym/create_fixture.sql new file mode 100644 index 0000000..7c706ee --- /dev/null +++ b/exercises/practice/acronym/create_fixture.sql @@ -0,0 +1,8 @@ +DROP TABLE IF EXISTS acronym; +CREATE TABLE acronym ( + phrase TEXT PRIMARY KEY, + result TEXT +); + +.mode csv +.import ./data.csv acronym diff --git a/exercises/practice/acronym/create_test_table.sql b/exercises/practice/acronym/create_test_table.sql new file mode 100644 index 0000000..e717ea8 --- /dev/null +++ b/exercises/practice/acronym/create_test_table.sql @@ -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'); diff --git a/exercises/practice/acronym/data.csv b/exercises/practice/acronym/data.csv new file mode 100644 index 0000000..c7d935a --- /dev/null +++ b/exercises/practice/acronym/data.csv @@ -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",""