Skip to content

Commit c574fe4

Browse files
authored
pig-latin-exercise: Added new exercise. (#131)
1 parent 68cf9f7 commit c574fe4

10 files changed

+402
-0
lines changed

config.json

+8
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,14 @@
422422
"practices": [],
423423
"prerequisites": [],
424424
"difficulty": 7
425+
},
426+
{
427+
"slug": "pig-latin",
428+
"name": "Pig-Latin",
429+
"uuid": "5f93f3f5-c7d0-4597-86a4-4e9ff66fcc90",
430+
"practices": [],
431+
"prerequisites": [],
432+
"difficulty": 6
425433
}
426434
]
427435
},
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"`)
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"SimaDovakin"
4+
],
5+
"files": {
6+
"solution": [
7+
"pigLatin.u"
8+
],
9+
"test": [
10+
"pigLatin.test.u"
11+
],
12+
"example": [
13+
".meta/examples/pigLatin.example.u"
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
pigLatin.translate : Text -> Text
2+
pigLatin.translate = words >> map toPigLatin >> join " "
3+
4+
toPigLatin : Text -> Text
5+
toPigLatin text =
6+
use Text join
7+
match startsWithVowelSound text with
8+
true -> text ++ "ay"
9+
false ->
10+
(consonantPrefix, wordsRest) = splitByConsonantPrefix text
11+
isPrefixEmpty = consonantPrefix == ""
12+
isRestStartsWithQu = startsWith "qu" wordsRest
13+
isRestStartsWithQ = startsWith "q" wordsRest
14+
isRestStartsWithY = startsWith "y" wordsRest
15+
match (isPrefixEmpty, isRestStartsWithQu, isRestStartsWithQ, isRestStartsWithY) with
16+
(_, true, _, _) -> join "" [drop 2 wordsRest, consonantPrefix, "quay"]
17+
(_, false, true, _) -> join "" [drop 1 wordsRest, consonantPrefix, "qay"]
18+
(true, _, _, true) -> join "" [drop 1 wordsRest, consonantPrefix, "yay"]
19+
_ -> join "" [wordsRest, consonantPrefix, "ay"]
20+
21+
pigLatin.isVowel : Char -> Boolean
22+
pigLatin.isVowel = Class.is (Class.in "aeiou")
23+
24+
pigLatin.startsWithVowelSound : Text -> Boolean
25+
pigLatin.startsWithVowelSound text =
26+
match (Optional.map isVowel (head text), any (p -> startsWith p text) ["xr", "yt"]) with
27+
(Some true, _) -> true
28+
(_, true) -> true
29+
_ -> false
30+
31+
pigLatin.splitByConsonantPrefix : Text -> (Text, Text)
32+
pigLatin.splitByConsonantPrefix = break (c -> or (isVowel c) (is (in "qy") c))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
[
2+
{
3+
"name": "pigLatin.translate.tests.ex1",
4+
"test_code": "expect (\"appleay\" == pigLatin.translate \"apple\")\n |> Test.label \"ay is added to words that start with vowels - word beginning with a\""
5+
},
6+
{
7+
"name": "pigLatin.translate.tests.ex2",
8+
"test_code": "expect (\"earay\" == pigLatin.translate \"ear\")\n |> Test.label \"ay is added to words that start with vowels - word beginning with e\""
9+
},
10+
{
11+
"name": "pigLatin.translate.tests.ex3",
12+
"test_code": "expect (\"iglooay\" == pigLatin.translate \"igloo\")\n |> Test.label \"ay is added to words that start with vowels - word beginning with i\""
13+
},
14+
{
15+
"name": "pigLatin.translate.tests.ex4",
16+
"test_code": "expect (\"objectay\" == pigLatin.translate \"object\")\n |> Test.label \"ay is added to words that start with vowels - word beginning with o\""
17+
},
18+
{
19+
"name": "pigLatin.translate.tests.ex5",
20+
"test_code": "expect (\"underay\" == pigLatin.translate \"under\")\n |> Test.label \"ay is added to words that start with vowels - word beginning with u\""
21+
},
22+
{
23+
"name": "pigLatin.translate.tests.ex6",
24+
"test_code": "expect (\"equalay\" == pigLatin.translate \"equal\")\n |> Test.label \"ay is added to words that start with vowels - word beginning with a vowel and followed by a qu\""
25+
},
26+
{
27+
"name": "pigLatin.translate.tests.ex7",
28+
"test_code": "expect (\"igpay\" == pigLatin.translate \"pig\")\n |> Test.label \"first letter and ay are moved to the end of words that start with consonants - word beginning with p\""
29+
},
30+
{
31+
"name": "pigLatin.translate.tests.ex8",
32+
"test_code": "expect (\"oalakay\" == pigLatin.translate \"koala\")\n |> Test.label \"first letter and ay are moved to the end of words that start with consonants - word beginning with k\""
33+
},
34+
{
35+
"name": "pigLatin.translate.tests.ex9",
36+
"test_code": "expect (\"enonxay\" == pigLatin.translate \"xenon\")\n |> Test.label \"first letter and ay are moved to the end of words that start with consonants - word beginning with x\""
37+
},
38+
{
39+
"name": "pigLatin.translate.tests.ex10",
40+
"test_code": "expect (\"atqay\" == pigLatin.translate \"qat\")\n |> Test.label \"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+
{
43+
"name": "pigLatin.translate.tests.ex11",
44+
"test_code": "expect (\"airchay\" == pigLatin.translate \"chair\")\n |> Test.label \"some letter clusters are treated like a single consonant - word beginning with ch\""
45+
},
46+
{
47+
"name": "pigLatin.translate.tests.ex12",
48+
"test_code": "expect (\"eenquay\" == pigLatin.translate \"queen\")\n |> Test.label \"some letter clusters are treated like a single consonant - word beginning with qu\""
49+
},
50+
{
51+
"name": "pigLatin.translate.tests.ex13",
52+
"test_code": "expect (\"aresquay\" == pigLatin.translate \"square\")\n |> Test.label \"some letter clusters are treated like a single consonant - word beginning with qu and a preceding consonant\""
53+
},
54+
{
55+
"name": "pigLatin.translate.tests.ex14",
56+
"test_code": "expect (\"erapythay\" == pigLatin.translate \"therapy\")\n |> Test.label \"some letter clusters are treated like a single consonant - word beginning with th\""
57+
},
58+
{
59+
"name": "pigLatin.translate.tests.ex15",
60+
"test_code": "expect (\"ushthray\" == pigLatin.translate \"thrush\")\n |> Test.label \"some letter clusters are treated like a single consonant - word beginning with thr\""
61+
},
62+
{
63+
"name": "pigLatin.translate.tests.ex16",
64+
"test_code": "expect (\"oolschay\" == pigLatin.translate \"school\")\n |> Test.label \"some letter clusters are treated like a single consonant - word beginning with sch\""
65+
},
66+
{
67+
"name": "pigLatin.translate.tests.ex17",
68+
"test_code": "expect (\"yttriaay\" == pigLatin.translate \"yttria\")\n |> Test.label \"some letter clusters are treated like a single vowel - word beginning with yt\""
69+
},
70+
{
71+
"name": "pigLatin.translate.tests.ex18",
72+
"test_code": "expect (\"xrayay\" == pigLatin.translate \"xray\")\n |> Test.label \"some letter clusters are treated like a single vowel - word beginning with xr\""
73+
},
74+
{
75+
"name": "pigLatin.translate.tests.ex19",
76+
"test_code": "expect (\"ellowyay\" == pigLatin.translate \"yellow\")\n |> Test.label \"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\""
77+
},
78+
{
79+
"name": "pigLatin.translate.tests.ex20",
80+
"test_code": "expect (\"ythmrhay\" == pigLatin.translate \"rhythm\")\n |> Test.label \"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\""
81+
},
82+
{
83+
"name": "pigLatin.translate.tests.ex21",
84+
"test_code": "expect (\"ymay\" == pigLatin.translate \"my\")\n |> Test.label \"position of y in a word determines if it is a consonant or a vowel - y as second letter in two letter word\""
85+
},
86+
{
87+
"name": "pigLatin.translate.tests.ex22",
88+
"test_code": "expect (\"ickquay astfay unray\" == pigLatin.translate \"quick fast run\")\n |> Test.label \"phrases are translated - a whole phrase\""
89+
}
90+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Testing transcript for pig-latin exercise
2+
3+
```ucm
4+
.> load ./pigLatin.u
5+
.> add
6+
.> load ./pigLatin.test.u
7+
.> add
8+
.> move.term pigLatin.tests tests
9+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
[c01e049a-e3e2-451c-bf8e-e2abb7e438b8]
43+
description = "some letter clusters are treated like a single consonant -> word beginning with ch"
44+
45+
[9ba1669e-c43f-4b93-837a-cfc731fd1425]
46+
description = "some letter clusters are treated like a single consonant -> word beginning with qu"
47+
48+
[92e82277-d5e4-43d7-8dd3-3a3b316c41f7]
49+
description = "some letter clusters are treated like a single consonant -> word beginning with qu and a preceding consonant"
50+
51+
[79ae4248-3499-4d5b-af46-5cb05fa073ac]
52+
description = "some letter clusters are treated like a single consonant -> word beginning with th"
53+
54+
[e0b3ae65-f508-4de3-8999-19c2f8e243e1]
55+
description = "some letter clusters are treated like a single consonant -> word beginning with thr"
56+
57+
[20bc19f9-5a35-4341-9d69-1627d6ee6b43]
58+
description = "some letter clusters are treated like a single consonant -> word beginning with sch"
59+
60+
[54b796cb-613d-4509-8c82-8fbf8fc0af9e]
61+
description = "some letter clusters are treated like a single vowel -> word beginning with yt"
62+
63+
[8c37c5e1-872e-4630-ba6e-d20a959b67f6]
64+
description = "some letter clusters are treated like a single vowel -> word beginning with xr"
65+
66+
[a4a36d33-96f3-422c-a233-d4021460ff00]
67+
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"
68+
69+
[adc90017-1a12-4100-b595-e346105042c7]
70+
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"
71+
72+
[29b4ca3d-efe5-4a95-9a54-8467f2e5e59a]
73+
description = "position of y in a word determines if it is a consonant or a vowel -> y as second letter in two letter word"
74+
75+
[44616581-5ce3-4a81-82d0-40c7ab13d2cf]
76+
description = "phrases are translated -> a whole phrase"

0 commit comments

Comments
 (0)