Skip to content

Commit

Permalink
Add pig-latin exercise (exercism#217)
Browse files Browse the repository at this point in the history
* Add pig-latin exercise

* rule 1 with single letter words
  • Loading branch information
keiravillekode authored Jun 5, 2024
1 parent 08b3596 commit 7d281be
Show file tree
Hide file tree
Showing 8 changed files with 310 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,14 @@
"prerequisites": [],
"difficulty": 4
},
{
"slug": "pig-latin",
"name": "Pig Latin",
"uuid": "351ee590-6869-460e-815e-ae8644c8ed9e",
"practices": [],
"prerequisites": [],
"difficulty": 4
},
{
"slug": "gigasecond",
"name": "Gigasecond",
Expand Down
46 changes: 46 additions & 0 deletions exercises/practice/pig-latin/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Instructions

Your task is to translate text from English to Pig Latin.
The translation is defined using four rules, which look at the pattern of vowels and consonants at the beginning of a word.
These rules look at each word's use of vowels and consonants:

- vowels: the letters `a`, `e`, `i`, `o`, and `u`
- consonants: the other 21 letters of the English alphabet

## Rule 1

If a word begins with a vowel, or starts with `"xr"` or `"yt"`, add an `"ay"` sound to the end of the word.

For example:

- `"apple"` -> `"appleay"` (starts with vowel)
- `"xray"` -> `"xrayay"` (starts with `"xr"`)
- `"yttria"` -> `"yttriaay"` (starts with `"yt"`)

## Rule 2

If a word begins with a 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.

For example:

- `"pig"` -> `"igp"` -> `"igpay"` (starts with single consonant)
- `"chair"` -> `"airch"` -> `"airchay"` (starts with multiple consonants)
- `"thrush"` -> `"ushthr"` -> `"ushthray"` (starts with multiple consonants)

## Rule 3

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.

For example:

- `"quick"` -> `"ickqu"` -> `"ay"` (starts with `"qu"`, no preceding consonants)
- `"square"` -> `"aresqu"` -> `"aresquay"` (starts with one consonant followed by `"qu`")

## Rule 4

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.

Some examples:

- `"my"` -> `"ym"` -> `"ymay"` (starts with single consonant followed by `"y"`)
- `"rhythm"` -> `"ythmrh"` -> `"ythmrhay"` (starts with multiple consonants followed by `"y"`)
8 changes: 8 additions & 0 deletions exercises/practice/pig-latin/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Introduction

Your parents have challenged you and your sibling to a game of two-on-two basketball.
Confident they'll win, they let you score the first couple of points, but then start taking over the game.
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.
This will give you the edge to prevail over your parents!

[pig-latin]: https://en.wikipedia.org/wiki/Pig_latin
19 changes: 19 additions & 0 deletions exercises/practice/pig-latin/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"pig-latin.v"
],
"test": [
"run_test.v"
],
"example": [
".meta/example.v"
]
},
"blurb": "Implement a program that translates from English to Pig Latin.",
"source": "The Pig Latin exercise at Test First Teaching by Ultrasaurus",
"source_url": "https://github.com/ultrasaurus/test-first-teaching/blob/master/learn_ruby/pig_latin/"
}
67 changes: 67 additions & 0 deletions exercises/practice/pig-latin/.meta/example.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
module main

import strings

fn translate(phrase string) string {
vowel := [
true, // a
false,
false,
false,
true, // e
false,
false,
false,
true, // i
false,
false,
false,
false,
false,
true, // o
false,
false,
false,
false,
false,
true, // u
false,
false,
false,
true, // y
false,
]

words := phrase.split(' ')
mut builder := strings.new_builder(phrase.len + 2 * words.len)
for word in words {
mut index := 0
rule1 := if vowel[word[0] - `a`] {
word[0] != `y` || (word.len > 1 && word[0..2] == 'yt')
} else {
word.len > 1 && word[0..2] == 'xr'
}

if !rule1 {
index++

// Rules 2 and 4: `y` is treated like a vowel.
for index < word.len && !vowel[word[index] - `a`] {
index++
}

// Rule 3
if index < word.len && word[(index - 1)..(index + 1)] == 'qu' {
index++
}
}
if builder.len != 0 {
builder.write_string(' ')
}
builder.write_string(word[index..])
builder.write_string(word[..index])
builder.write_string('ay')
}

return builder.str()
}
69 changes: 69 additions & 0 deletions exercises/practice/pig-latin/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# This is an auto-generated file. Regular comments will be removed when this
# file is regenerated. Regenerating will not touch any manually added keys,
# so comments can be added in a "comment" key.

[11567f84-e8c6-4918-aedb-435f0b73db57]
description = "ay is added to words that start with vowels -> word beginning with a"

[f623f581-bc59-4f45-9032-90c3ca9d2d90]
description = "ay is added to words that start with vowels -> word beginning with e"

[7dcb08b3-23a6-4e8a-b9aa-d4e859450d58]
description = "ay is added to words that start with vowels -> word beginning with i"

[0e5c3bff-266d-41c8-909f-364e4d16e09c]
description = "ay is added to words that start with vowels -> word beginning with o"

[614ba363-ca3c-4e96-ab09-c7320799723c]
description = "ay is added to words that start with vowels -> word beginning with u"

[bf2538c6-69eb-4fa7-a494-5a3fec911326]
description = "ay is added to words that start with vowels -> word beginning with a vowel and followed by a qu"

[e5be8a01-2d8a-45eb-abb4-3fcc9582a303]
description = "first letter and ay are moved to the end of words that start with consonants -> word beginning with p"

[d36d1e13-a7ed-464d-a282-8820cb2261ce]
description = "first letter and ay are moved to the end of words that start with consonants -> word beginning with k"

[d838b56f-0a89-4c90-b326-f16ff4e1dddc]
description = "first letter and ay are moved to the end of words that start with consonants -> word beginning with x"

[bce94a7a-a94e-4e2b-80f4-b2bb02e40f71]
description = "first letter and ay are moved to the end of words that start with consonants -> word beginning with q without a following u"

[c01e049a-e3e2-451c-bf8e-e2abb7e438b8]
description = "some letter clusters are treated like a single consonant -> word beginning with ch"

[9ba1669e-c43f-4b93-837a-cfc731fd1425]
description = "some letter clusters are treated like a single consonant -> word beginning with qu"

[92e82277-d5e4-43d7-8dd3-3a3b316c41f7]
description = "some letter clusters are treated like a single consonant -> word beginning with qu and a preceding consonant"

[79ae4248-3499-4d5b-af46-5cb05fa073ac]
description = "some letter clusters are treated like a single consonant -> word beginning with th"

[e0b3ae65-f508-4de3-8999-19c2f8e243e1]
description = "some letter clusters are treated like a single consonant -> word beginning with thr"

[20bc19f9-5a35-4341-9d69-1627d6ee6b43]
description = "some letter clusters are treated like a single consonant -> word beginning with sch"

[54b796cb-613d-4509-8c82-8fbf8fc0af9e]
description = "some letter clusters are treated like a single vowel -> word beginning with yt"

[8c37c5e1-872e-4630-ba6e-d20a959b67f6]
description = "some letter clusters are treated like a single vowel -> word beginning with xr"

[a4a36d33-96f3-422c-a233-d4021460ff00]
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"

[adc90017-1a12-4100-b595-e346105042c7]
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"

[29b4ca3d-efe5-4a95-9a54-8467f2e5e59a]
description = "position of y in a word determines if it is a consonant or a vowel -> y as second letter in two letter word"

[44616581-5ce3-4a81-82d0-40c7ab13d2cf]
description = "phrases are translated -> a whole phrase"
4 changes: 4 additions & 0 deletions exercises/practice/pig-latin/pig-latin.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module main

fn translate(phrase string) string {
}
89 changes: 89 additions & 0 deletions exercises/practice/pig-latin/run_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
module main

fn test_ay_is_added_to_words_that_start_with_vowels__word_beginning_with_a() {
assert translate('apple') == 'appleay'
}

fn test_ay_is_added_to_words_that_start_with_vowels__word_beginning_with_e() {
assert translate('ear') == 'earay'
}

fn test_ay_is_added_to_words_that_start_with_vowels__word_beginning_with_i() {
assert translate('igloo') == 'iglooay'
}

fn test_ay_is_added_to_words_that_start_with_vowels__word_beginning_with_o() {
assert translate('object') == 'objectay'
}

fn test_ay_is_added_to_words_that_start_with_vowels__word_beginning_with_u() {
assert translate('under') == 'underay'
}

fn test_ay_is_added_to_words_that_start_with_vowels__word_beginning_with_a_vowel_and_followed_by_a_qu() {
assert translate('equal') == 'equalay'
}

fn test_first_letter_and_ay_are_moved_to_the_end_of_words_that_start_with_consonants__word_beginning_with_p() {
assert translate('pig') == 'igpay'
}

fn test_first_letter_and_ay_are_moved_to_the_end_of_words_that_start_with_consonants__word_beginning_with_k() {
assert translate('koala') == 'oalakay'
}

fn test_first_letter_and_ay_are_moved_to_the_end_of_words_that_start_with_consonants__word_beginning_with_x() {
assert translate('xenon') == 'enonxay'
}

fn test_first_letter_and_ay_are_moved_to_the_end_of_words_that_start_with_consonants__word_beginning_with_q_without_a_following_u() {
assert translate('qat') == 'atqay'
}

fn test_some_letter_clusters_are_treated_like_a_single_consonant__word_beginning_with_ch() {
assert translate('chair') == 'airchay'
}

fn test_some_letter_clusters_are_treated_like_a_single_consonant__word_beginning_with_qu() {
assert translate('queen') == 'eenquay'
}

fn test_some_letter_clusters_are_treated_like_a_single_consonant__word_beginning_with_qu_and_a_preceding_consonant() {
assert translate('square') == 'aresquay'
}

fn test_some_letter_clusters_are_treated_like_a_single_consonant__word_beginning_with_th() {
assert translate('therapy') == 'erapythay'
}

fn test_some_letter_clusters_are_treated_like_a_single_consonant__word_beginning_with_thr() {
assert translate('thrush') == 'ushthray'
}

fn test_some_letter_clusters_are_treated_like_a_single_consonant__word_beginning_with_sch() {
assert translate('school') == 'oolschay'
}

fn test_some_letter_clusters_are_treated_like_a_single_vowel__word_beginning_with_yt() {
assert translate('yttria') == 'yttriaay'
}

fn test_some_letter_clusters_are_treated_like_a_single_vowel__word_beginning_with_xr() {
assert translate('xray') == 'xrayay'
}

fn test_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() {
assert translate('yellow') == 'ellowyay'
}

fn test_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() {
assert translate('rhythm') == 'ythmrhay'
}

fn test_position_of_y_in_a_word_determines_if_it_is_a_consonant_or_a_vowel__y_as_second_letter_in_two_letter_word() {
assert translate('my') == 'ymay'
}

fn test_phrases_are_translated__a_whole_phrase() {
assert translate('quick fast run') == 'ickquay astfay unray'
}

0 comments on commit 7d281be

Please sign in to comment.