Skip to content

Commit 97bd70c

Browse files
authored
Add practice exercise: pig-latin (#126)
1 parent bdfac6a commit 97bd70c

File tree

11 files changed

+327
-0
lines changed

11 files changed

+327
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,14 @@
330330
"prerequisites": [],
331331
"difficulty": 8
332332
},
333+
{
334+
"slug": "pig-latin",
335+
"name": "Pig Latin",
336+
"uuid": "2c45ef99-047e-4d83-b11d-256acc20a175",
337+
"practices": [],
338+
"prerequisites": [],
339+
"difficulty": 8
340+
},
333341
{
334342
"slug": "rest-api",
335343
"name": "REST API",
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Instructions
2+
3+
Your task is to translate text from English to Pig Latin.
4+
The translation is defined using four rules, which look at the pattern of vowels and consonants at the beginning of a word.
5+
These rules look at each word's use of vowels and consonants:
6+
7+
- vowels: the letters `a`, `e`, `i`, `o`, and `u`
8+
- consonants: the other 21 letters of the English alphabet
9+
10+
## Rule 1
11+
12+
If a word begins with a vowel, or starts with `"xr"` or `"yt"`, add an `"ay"` sound to the end of the word.
13+
14+
For example:
15+
16+
- `"apple"` -> `"appleay"` (starts with vowel)
17+
- `"xray"` -> `"xrayay"` (starts with `"xr"`)
18+
- `"yttria"` -> `"yttriaay"` (starts with `"yt"`)
19+
20+
## Rule 2
21+
22+
If a word begins with one or more consonants, first move those consonants to the end of the word and then add an `"ay"` sound to the end of the word.
23+
24+
For example:
25+
26+
- `"pig"` -> `"igp"` -> `"igpay"` (starts with single consonant)
27+
- `"chair"` -> `"airch"` -> `"airchay"` (starts with multiple consonants)
28+
- `"thrush"` -> `"ushthr"` -> `"ushthray"` (starts with multiple consonants)
29+
30+
## Rule 3
31+
32+
If a word starts with zero or more consonants followed by `"qu"`, first move those consonants (if any) and the `"qu"` part to the end of the word, and then add an `"ay"` sound to the end of the word.
33+
34+
For example:
35+
36+
- `"quick"` -> `"ickqu"` -> `"ickquay"` (starts with `"qu"`, no preceding consonants)
37+
- `"square"` -> `"aresqu"` -> `"aresquay"` (starts with one consonant followed by `"qu`")
38+
39+
## Rule 4
40+
41+
If a word starts with one or more consonants followed by `"y"`, first move the consonants preceding the `"y"`to the end of the word, and then add an `"ay"` sound to the end of the word.
42+
43+
Some examples:
44+
45+
- `"my"` -> `"ym"` -> `"ymay"` (starts with single consonant followed by `"y"`)
46+
- `"rhythm"` -> `"ythmrh"` -> `"ythmrhay"` (starts with multiple consonants followed by `"y"`)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Introduction
2+
3+
Your parents have challenged you and your sibling to a game of two-on-two basketball.
4+
Confident they'll win, they let you score the first couple of points, but then start taking over the game.
5+
Needing a little boost, you start speaking in [Pig Latin][pig-latin], which is a made-up children's language that's difficult for non-children to understand.
6+
This will give you the edge to prevail over your parents!
7+
8+
[pig-latin]: https://en.wikipedia.org/wiki/Pig_latin
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+
"pig-latin.sql"
8+
],
9+
"test": [
10+
"pig-latin_test.sql"
11+
],
12+
"example": [
13+
".meta/example.sql"
14+
]
15+
},
16+
"blurb": "Implement a program that translates from English to Pig Latin.",
17+
"source": "The Pig Latin exercise at Test First Teaching by Ultrasaurus",
18+
"source_url": "https://github.com/ultrasaurus/test-first-teaching/blob/master/learn_ruby/pig_latin/"
19+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
UPDATE "pig-latin"
2+
SET result = (
3+
WITH words (word) AS (
4+
SELECT j.value
5+
FROM JSON_EACH(PRINTF('["%s"]', REPLACE(phrase, ' ', '","'))) j
6+
)
7+
SELECT GROUP_CONCAT(word, ' ')
8+
FROM (
9+
SELECT (
10+
WITH remove_vowels (word, no_vowel) AS (
11+
SELECT
12+
word,
13+
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
14+
word,
15+
'a', CHAR(10)),
16+
'e', CHAR(10)),
17+
'i', CHAR(10)),
18+
'o', CHAR(10)),
19+
'u', CHAR(10)
20+
)
21+
FROM "pig-latin"
22+
)
23+
SELECT CASE
24+
WHEN REGEXP('^([aeiou]|xr|yt)', word) THEN word || 'ay'
25+
WHEN REGEXP('^[bcdfghjklmnpqrstvwxyz]*qu', word)
26+
THEN PRINTF(
27+
'%s%say',
28+
SUBSTR(word, INSTR(word, 'qu') + 2),
29+
SUBSTR(word, 1, INSTR(word, 'qu') + 1)
30+
)
31+
WHEN REGEXP('^[bcdfghjklmnpqrstvwxz]+y', word)
32+
THEN
33+
PRINTF(
34+
'%s%say',
35+
SUBSTR(word, INSTR(word, 'y')),
36+
SUBSTR(word, 1 , INSTR(word, 'y') - 1)
37+
)
38+
WHEN GLOB('[bcdfghjklmnpqrstvwxyz]*', word)
39+
THEN
40+
PRINTF(
41+
'%s%say',
42+
SUBSTR(word, INSTR(no_vowel, CHAR(10))),
43+
SUBSTR(word, 1, INSTR(no_vowel, CHAR(10)) - 1)
44+
)
45+
END
46+
FROM remove_vowels
47+
) AS word
48+
FROM words
49+
)
50+
)
51+
;
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
[11567f84-e8c6-4918-aedb-435f0b73db57]
13+
description = "ay is added to words that start with vowels -> word beginning with a"
14+
15+
[f623f581-bc59-4f45-9032-90c3ca9d2d90]
16+
description = "ay is added to words that start with vowels -> word beginning with e"
17+
18+
[7dcb08b3-23a6-4e8a-b9aa-d4e859450d58]
19+
description = "ay is added to words that start with vowels -> word beginning with i"
20+
21+
[0e5c3bff-266d-41c8-909f-364e4d16e09c]
22+
description = "ay is added to words that start with vowels -> word beginning with o"
23+
24+
[614ba363-ca3c-4e96-ab09-c7320799723c]
25+
description = "ay is added to words that start with vowels -> word beginning with u"
26+
27+
[bf2538c6-69eb-4fa7-a494-5a3fec911326]
28+
description = "ay is added to words that start with vowels -> word beginning with a vowel and followed by a qu"
29+
30+
[e5be8a01-2d8a-45eb-abb4-3fcc9582a303]
31+
description = "first letter and ay are moved to the end of words that start with consonants -> word beginning with p"
32+
33+
[d36d1e13-a7ed-464d-a282-8820cb2261ce]
34+
description = "first letter and ay are moved to the end of words that start with consonants -> word beginning with k"
35+
36+
[d838b56f-0a89-4c90-b326-f16ff4e1dddc]
37+
description = "first letter and ay are moved to the end of words that start with consonants -> word beginning with x"
38+
39+
[bce94a7a-a94e-4e2b-80f4-b2bb02e40f71]
40+
description = "first letter and ay are moved to the end of words that start with consonants -> word beginning with q without a following u"
41+
42+
[e59dbbe8-ccee-4619-a8e9-ce017489bfc0]
43+
description = "first letter and ay are moved to the end of words that start with consonants -> word beginning with consonant and vowel containing qu"
44+
45+
[c01e049a-e3e2-451c-bf8e-e2abb7e438b8]
46+
description = "some letter clusters are treated like a single consonant -> word beginning with ch"
47+
48+
[9ba1669e-c43f-4b93-837a-cfc731fd1425]
49+
description = "some letter clusters are treated like a single consonant -> word beginning with qu"
50+
51+
[92e82277-d5e4-43d7-8dd3-3a3b316c41f7]
52+
description = "some letter clusters are treated like a single consonant -> word beginning with qu and a preceding consonant"
53+
54+
[79ae4248-3499-4d5b-af46-5cb05fa073ac]
55+
description = "some letter clusters are treated like a single consonant -> word beginning with th"
56+
57+
[e0b3ae65-f508-4de3-8999-19c2f8e243e1]
58+
description = "some letter clusters are treated like a single consonant -> word beginning with thr"
59+
60+
[20bc19f9-5a35-4341-9d69-1627d6ee6b43]
61+
description = "some letter clusters are treated like a single consonant -> word beginning with sch"
62+
63+
[54b796cb-613d-4509-8c82-8fbf8fc0af9e]
64+
description = "some letter clusters are treated like a single vowel -> word beginning with yt"
65+
66+
[8c37c5e1-872e-4630-ba6e-d20a959b67f6]
67+
description = "some letter clusters are treated like a single vowel -> word beginning with xr"
68+
69+
[a4a36d33-96f3-422c-a233-d4021460ff00]
70+
description = "position of y in a word determines if it is a consonant or a vowel -> y is treated like a consonant at the beginning of a word"
71+
72+
[adc90017-1a12-4100-b595-e346105042c7]
73+
description = "position of y in a word determines if it is a consonant or a vowel -> y is treated like a vowel at the end of a consonant cluster"
74+
75+
[29b4ca3d-efe5-4a95-9a54-8467f2e5e59a]
76+
description = "position of y in a word determines if it is a consonant or a vowel -> y as second letter in two letter word"
77+
78+
[44616581-5ce3-4a81-82d0-40c7ab13d2cf]
79+
description = "phrases are translated -> a whole phrase"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
DROP TABLE IF EXISTS "pig-latin";
2+
CREATE TABLE "pig-latin" (
3+
phrase TEXT NOT NULL,
4+
result TEXT
5+
);
6+
7+
.mode csv
8+
.import ./data.csv "pig-latin"
9+
10+
UPDATE "pig-latin" SET result = NULL;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
('11567f84-e8c6-4918-aedb-435f0b73db57', 'word beginning with a', 'apple', 'appleay'),
20+
('f623f581-bc59-4f45-9032-90c3ca9d2d90', 'word beginning with e', 'ear', 'earay'),
21+
('7dcb08b3-23a6-4e8a-b9aa-d4e859450d58', 'word beginning with i', 'igloo', 'iglooay'),
22+
('0e5c3bff-266d-41c8-909f-364e4d16e09c', 'word beginning with o', 'object', 'objectay'),
23+
('614ba363-ca3c-4e96-ab09-c7320799723c', 'word beginning with u', 'under', 'underay'),
24+
('bf2538c6-69eb-4fa7-a494-5a3fec911326', 'word beginning with a vowel and followed by a qu', 'equal', 'equalay'),
25+
('e5be8a01-2d8a-45eb-abb4-3fcc9582a303', 'word beginning with p', 'pig', 'igpay'),
26+
('d36d1e13-a7ed-464d-a282-8820cb2261ce', 'word beginning with k', 'koala', 'oalakay'),
27+
('d838b56f-0a89-4c90-b326-f16ff4e1dddc', 'word beginning with x', 'xenon', 'enonxay'),
28+
('bce94a7a-a94e-4e2b-80f4-b2bb02e40f71', 'word beginning with q without a following u', 'qat', 'atqay'),
29+
('e59dbbe8-ccee-4619-a8e9-ce017489bfc0', 'word beginning with consonant and vowel containing qu', 'liquid', 'iquidlay'),
30+
('c01e049a-e3e2-451c-bf8e-e2abb7e438b8', 'word beginning with ch', 'chair', 'airchay'),
31+
('9ba1669e-c43f-4b93-837a-cfc731fd1425', 'word beginning with qu', 'queen', 'eenquay'),
32+
('92e82277-d5e4-43d7-8dd3-3a3b316c41f7', 'word beginning with qu and a preceding consonant', 'square', 'aresquay'),
33+
('79ae4248-3499-4d5b-af46-5cb05fa073ac', 'word beginning with th', 'therapy', 'erapythay'),
34+
('e0b3ae65-f508-4de3-8999-19c2f8e243e1', 'word beginning with thr', 'thrush', 'ushthray'),
35+
('20bc19f9-5a35-4341-9d69-1627d6ee6b43', 'word beginning with sch', 'school', 'oolschay'),
36+
('54b796cb-613d-4509-8c82-8fbf8fc0af9e', 'word beginning with yt', 'yttria', 'yttriaay'),
37+
('8c37c5e1-872e-4630-ba6e-d20a959b67f6', 'word beginning with xr', 'xray', 'xrayay'),
38+
('a4a36d33-96f3-422c-a233-d4021460ff00', 'y is treated like a consonant at the beginning of a word', 'yellow', 'ellowyay'),
39+
('adc90017-1a12-4100-b595-e346105042c7', 'y is treated like a vowel at the end of a consonant cluster', 'rhythm', 'ythmrhay'),
40+
('29b4ca3d-efe5-4a95-9a54-8467f2e5e59a', 'y as second letter in two letter word', 'my', 'ymay'),
41+
('44616581-5ce3-4a81-82d0-40c7ab13d2cf', 'a whole phrase', 'quick fast run', 'ickquay astfay unray');

exercises/practice/pig-latin/data.csv

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"apple",
2+
"ear",
3+
"igloo",
4+
"object",
5+
"under",
6+
"equal",
7+
"pig",
8+
"koala",
9+
"xenon",
10+
"qat",
11+
"liquid",
12+
"chair",
13+
"queen",
14+
"square",
15+
"therapy",
16+
"thrush",
17+
"school",
18+
"yttria",
19+
"xray",
20+
"yellow",
21+
"rhythm",
22+
"my",
23+
"quick fast run",
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- Schema: CREATE TABLE "pig-latin" ( phrase TEXT NOT NULL, result TEXT );
2+
-- Task: update the pig-latin table and set the result based on the phrase.

0 commit comments

Comments
 (0)