From 1b6335b848418278b48c7f96b973f21b7b455a0f Mon Sep 17 00:00:00 2001 From: Blake Lewis Date: Tue, 30 Jan 2024 15:14:49 -0800 Subject: [PATCH] Pig Latin in Racket (#344) --- config.json | 8 ++ .../practice/pig-latin/.docs/instructions.md | 20 +++++ .../practice/pig-latin/.meta/config.json | 17 ++++ .../practice/pig-latin/.meta/example.rkt | 23 ++++++ exercises/practice/pig-latin/.meta/tests.toml | 76 ++++++++++++++++++ .../practice/pig-latin/pig-latin-test.rkt | 79 +++++++++++++++++++ exercises/practice/pig-latin/pig-latin.rkt | 6 ++ 7 files changed, 229 insertions(+) create mode 100644 exercises/practice/pig-latin/.docs/instructions.md create mode 100644 exercises/practice/pig-latin/.meta/config.json create mode 100644 exercises/practice/pig-latin/.meta/example.rkt create mode 100644 exercises/practice/pig-latin/.meta/tests.toml create mode 100644 exercises/practice/pig-latin/pig-latin-test.rkt create mode 100644 exercises/practice/pig-latin/pig-latin.rkt diff --git a/config.json b/config.json index dd64397d..68643d0d 100644 --- a/config.json +++ b/config.json @@ -606,6 +606,14 @@ "practices": [], "prerequisites": [], "difficulty": 3 + }, + { + "slug": "pig-latin", + "name": "Pig Latin", + "uuid": "952531aa-1975-48c0-9324-e8d4ebabe07e", + "practices": [], + "prerequisites": [], + "difficulty": 2 } ] }, diff --git a/exercises/practice/pig-latin/.docs/instructions.md b/exercises/practice/pig-latin/.docs/instructions.md new file mode 100644 index 00000000..032905aa --- /dev/null +++ b/exercises/practice/pig-latin/.docs/instructions.md @@ -0,0 +1,20 @@ +# Instructions + +Implement a program that translates from English to Pig Latin. + +Pig Latin is a made-up children's language that's intended to be confusing. +It obeys a few simple rules (below), but when it's spoken quickly it's really difficult for non-children (and non-native speakers) to understand. + +- **Rule 1**: If a word begins with a vowel sound, add an "ay" sound to the end of the word. + Please note that "xr" and "yt" at the beginning of a word make vowel sounds (e.g. "xray" -> "xrayay", "yttria" -> "yttriaay"). +- **Rule 2**: If a word begins with a consonant sound, move it to the end of the word and then add an "ay" sound to the end of the word. + Consonant sounds can be made up of multiple consonants, such as the "ch" in "chair" or "st" in "stand" (e.g. "chair" -> "airchay"). +- **Rule 3**: If a word starts with a consonant sound followed by "qu", move it to the end of the word, and then add an "ay" sound to the end of the word (e.g. "square" -> "aresquay"). +- **Rule 4**: If a word contains a "y" after a consonant cluster or as the second letter in a two letter word it makes a vowel sound (e.g. "rhythm" -> "ythmrhay", "my" -> "ymay"). + +There are a few more rules for edge cases, and there are regional variants too. +Check the tests for all the details. + +Read more about [Pig Latin on Wikipedia][pig-latin]. + +[pig-latin]: https://en.wikipedia.org/wiki/Pig_latin diff --git a/exercises/practice/pig-latin/.meta/config.json b/exercises/practice/pig-latin/.meta/config.json new file mode 100644 index 00000000..944adf89 --- /dev/null +++ b/exercises/practice/pig-latin/.meta/config.json @@ -0,0 +1,17 @@ +{ + "authors": ["blakelewis"], + "files": { + "solution": [ + "pig-latin.rkt" + ], + "test": [ + "pig-latin-test.rkt" + ], + "example": [ + ".meta/example.rkt" + ] + }, + "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/" +} diff --git a/exercises/practice/pig-latin/.meta/example.rkt b/exercises/practice/pig-latin/.meta/example.rkt new file mode 100644 index 00000000..66de8aa5 --- /dev/null +++ b/exercises/practice/pig-latin/.meta/example.rkt @@ -0,0 +1,23 @@ +#lang racket + +(provide translate) + +(define (leading-vowel? word) + (regexp-match #px"^[aeiou]|^[xy][^aeiou]" word)) + +(define (leading-consonants word) + (let ([head (if (string-prefix? word "y") + "y" + (car (string-split word #px"[aeiouy]")))]) + (if (and (string-suffix? head "q") + (string-prefix? (substring word (string-length head)) "u")) + (string-append head "u") + head))) + +(define (translate-word word) + (let* ([head (if (leading-vowel? word) "" (leading-consonants word))] + [tail (substring word (string-length head))]) + (string-append tail head "ay"))) + +(define (translate text) + (string-join (map translate-word (string-split text)))) diff --git a/exercises/practice/pig-latin/.meta/tests.toml b/exercises/practice/pig-latin/.meta/tests.toml new file mode 100644 index 00000000..c29168c5 --- /dev/null +++ b/exercises/practice/pig-latin/.meta/tests.toml @@ -0,0 +1,76 @@ +# 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. + +[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" diff --git a/exercises/practice/pig-latin/pig-latin-test.rkt b/exercises/practice/pig-latin/pig-latin-test.rkt new file mode 100644 index 00000000..22b523b9 --- /dev/null +++ b/exercises/practice/pig-latin/pig-latin-test.rkt @@ -0,0 +1,79 @@ +#lang racket/base + +(require "pig-latin.rkt") + +(module+ test + (require rackunit rackunit/text-ui)) + +(module+ test + (define suite + (test-suite + "Pig Latin tests" + + (test-equal? "word beginning with a" + (translate "apple") "appleay") + + (test-equal? "word beginning with e" + (translate "ear") "earay") + + (test-equal? "word beginning with i" + (translate "igloo") "iglooay") + + (test-equal? "word beginning with o" + (translate "object") "objectay") + + (test-equal? "word beginning with u" + (translate "under") "underay") + + (test-equal? "word beginning with a vowel and followed by a qu" + (translate "equal") "equalay") + + (test-equal? "word beginning with p" + (translate "pig") "igpay") + + (test-equal? "word beginning with k" + (translate "koala") "oalakay") + + (test-equal? "word beginning with x" + (translate "xenon") "enonxay") + + (test-equal? "word beginning with q without a following u" + (translate "qat") "atqay") + + (test-equal? "word beginning with ch" + (translate "chair") "airchay") + + (test-equal? "word beginning with qu" + (translate "queen") "eenquay") + + (test-equal? "word beginning with qu and a preceding consonant" + (translate "square") "aresquay") + + (test-equal? "word beginning with th" + (translate "therapy") "erapythay") + + (test-equal? "word beginning with thr" + (translate "thrush") "ushthray") + + (test-equal? "word beginning with sch" + (translate "school") "oolschay") + + (test-equal? "word beginning with yt" + (translate "yttria") "yttriaay") + + (test-equal? "word beginning with xr" + (translate "xray") "xrayay") + + (test-equal? "y is treated like a consonant at the beginning of a word" + (translate "yellow") "ellowyay") + + (test-equal? "y is treated like a vowel at the end of a consonant cluster" + (translate "rhythm") "ythmrhay") + + (test-equal? "y as second letter in two letter word" + (translate "my") "ymay") + + (test-equal? "a whole phrase" + (translate "quick fast run") "ickquay astfay unray"))) + + (run-tests suite)) diff --git a/exercises/practice/pig-latin/pig-latin.rkt b/exercises/practice/pig-latin/pig-latin.rkt new file mode 100644 index 00000000..45117b71 --- /dev/null +++ b/exercises/practice/pig-latin/pig-latin.rkt @@ -0,0 +1,6 @@ +#lang racket + +(provide translate) + +(define (translate text) + (error "Not implemented yet"))