From 21f8c8aed38c23bf3d7f658a217e7f6f4f425513 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20B=20Nagy?= <20251272+BNAndras@users.noreply.github.com> Date: Fri, 21 Jun 2024 20:56:30 -0700 Subject: [PATCH 1/4] Add pig-latin --- config.json | 8 ++ .../practice/pig-latin/.docs/instructions.md | 46 +++++++++ .../practice/pig-latin/.docs/introduction.md | 8 ++ .../practice/pig-latin/.meta/config.json | 19 ++++ exercises/practice/pig-latin/.meta/example.jl | 18 ++++ exercises/practice/pig-latin/.meta/tests.toml | 76 +++++++++++++++ exercises/practice/pig-latin/pig-latin.jl | 4 + exercises/practice/pig-latin/runtests.jl | 94 +++++++++++++++++++ 8 files changed, 273 insertions(+) create mode 100644 exercises/practice/pig-latin/.docs/instructions.md create mode 100644 exercises/practice/pig-latin/.docs/introduction.md create mode 100644 exercises/practice/pig-latin/.meta/config.json create mode 100644 exercises/practice/pig-latin/.meta/example.jl create mode 100644 exercises/practice/pig-latin/.meta/tests.toml create mode 100644 exercises/practice/pig-latin/pig-latin.jl create mode 100644 exercises/practice/pig-latin/runtests.jl diff --git a/config.json b/config.json index 0dc63f04..5a2ec7d5 100644 --- a/config.json +++ b/config.json @@ -85,6 +85,14 @@ "integers" ] }, + { + "slug": "pig-latin", + "name": "Pig Latin", + "uuid": "3c53b207-ffcb-4111-ba3e-3aece095e268", + "practices": [], + "prerequisites": [], + "difficulty": 2 + }, { "slug": "nucleotide-count", "name": "Nucleotide Count", diff --git a/exercises/practice/pig-latin/.docs/instructions.md b/exercises/practice/pig-latin/.docs/instructions.md new file mode 100644 index 00000000..a9645ac2 --- /dev/null +++ b/exercises/practice/pig-latin/.docs/instructions.md @@ -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 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"` -> `"ickquay"` (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"`) diff --git a/exercises/practice/pig-latin/.docs/introduction.md b/exercises/practice/pig-latin/.docs/introduction.md new file mode 100644 index 00000000..04baa475 --- /dev/null +++ b/exercises/practice/pig-latin/.docs/introduction.md @@ -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 diff --git a/exercises/practice/pig-latin/.meta/config.json b/exercises/practice/pig-latin/.meta/config.json new file mode 100644 index 00000000..e11bc847 --- /dev/null +++ b/exercises/practice/pig-latin/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "BNAndras" + ], + "files": { + "solution": [ + "pig-latin.jl" + ], + "test": [ + "runtests.jl" + ], + "example": [ + ".meta/example.jl" + ] + }, + "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.jl b/exercises/practice/pig-latin/.meta/example.jl new file mode 100644 index 00000000..62a40854 --- /dev/null +++ b/exercises/practice/pig-latin/.meta/example.jl @@ -0,0 +1,18 @@ +function translate(phrase) + words = eachsplit(phrase, " ") + fragments = map(translateFragment, words) + + join(fragments, " ") +end + +vowelSound = r"^([aeiou]|xr|yt)" +consonantSound = r"^([^aeiou]+(?=y)|[^aeiou]?qu|[^aeiou]+)([a-z]+)" + +function translateFragment(fragment) + translated = fragment + if !occursin(vowelSound, fragment) + translated = replace(fragment, consonantSound => s"\g<2>\g<1>") + end + + translated * "ay" +end 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.jl b/exercises/practice/pig-latin/pig-latin.jl new file mode 100644 index 00000000..d1c8e838 --- /dev/null +++ b/exercises/practice/pig-latin/pig-latin.jl @@ -0,0 +1,4 @@ +function translate(phrase) + +end + diff --git a/exercises/practice/pig-latin/runtests.jl b/exercises/practice/pig-latin/runtests.jl new file mode 100644 index 00000000..a893c6d3 --- /dev/null +++ b/exercises/practice/pig-latin/runtests.jl @@ -0,0 +1,94 @@ +using Test + +include("pig-latin.jl") + +@testset verbose = true "tests" begin + @testset "word beginning with a" begin + @test translate("apple") == "appleay" + end + + @testset "word beginning with e" begin + @test translate("ear") == "earay" + end + + @testset "word beginning with i" begin + @test translate("igloo") == "iglooay" + end + + @testset "word beginning with o" begin + @test translate("object") == "objectay" + end + + @testset "word beginning with u" begin + @test translate("under") == "underay" + end + + @testset "word beginning with a vowel and followed by a qu" begin + @test translate("equal") == "equalay" + end + + @testset "word beginning with p" begin + @test translate("pig") == "igpay" + end + + @testset "word beginning with k" begin + @test translate("koala") == "oalakay" + end + + @testset "word beginning with x" begin + @test translate("xenon") == "enonxay" + end + + @testset "word beginning with q without a following u" begin + @test translate("qat") == "atqay" + end + + @testset "word beginning with ch" begin + @test translate("chair") == "airchay" + end + + @testset "word beginning with qu" begin + @test translate("queen") == "eenquay" + end + + @testset "word beginning with qu and a preceding consonant" begin + @test translate("square") == "aresquay" + end + + @testset "word beginning with th" begin + @test translate("therapy") == "erapythay" + end + + @testset "word beginning with thr" begin + @test translate("thrush") == "ushthray" + end + + @testset "word beginning with sch" begin + @test translate("school") == "oolschay" + end + + @testset "word beginning with yt" begin + @test translate("yttria") == "yttriaay" + end + + @testset "word beginning with xr" begin + @test translate("xray") == "xrayay" + end + + @testset "y is treated like a consonant at the beginning of a word" begin + @test translate("yellow") == "ellowyay" + end + + @testset "y is treated like a vowel at the end of a consonant cluster" begin + @test translate("rhythm") == "ythmrhay" + end + + @testset "y as second letter in two letter word" begin + @test translate("my") == "ymay" + end + + @testset "a whole phrase" begin + @test translate("quick fast run") == "ickquay astfay unray" + end +end + From 41bfbe5dea190482b314f502d0e3252ddfc08544 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20B=20Nagy?= <20251272+BNAndras@users.noreply.github.com> Date: Fri, 21 Jun 2024 21:00:42 -0700 Subject: [PATCH 2/4] eachsplit to split --- exercises/practice/pig-latin/.meta/example.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/pig-latin/.meta/example.jl b/exercises/practice/pig-latin/.meta/example.jl index 62a40854..8126f20c 100644 --- a/exercises/practice/pig-latin/.meta/example.jl +++ b/exercises/practice/pig-latin/.meta/example.jl @@ -1,5 +1,5 @@ function translate(phrase) - words = eachsplit(phrase, " ") + words = split(phrase, " ") fragments = map(translateFragment, words) join(fragments, " ") From 8b3d33efb8de22324599e853bc0c024ee34063d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20B=20Nagy?= <20251272+BNAndras@users.noreply.github.com> Date: Tue, 2 Jul 2024 17:32:13 -0700 Subject: [PATCH 3/4] Apply code review --- exercises/practice/pig-latin/.meta/example.jl | 12 +- exercises/practice/pig-latin/runtests.jl | 181 +++++++++--------- 2 files changed, 100 insertions(+), 93 deletions(-) diff --git a/exercises/practice/pig-latin/.meta/example.jl b/exercises/practice/pig-latin/.meta/example.jl index 8126f20c..71637c2b 100644 --- a/exercises/practice/pig-latin/.meta/example.jl +++ b/exercises/practice/pig-latin/.meta/example.jl @@ -1,17 +1,17 @@ function translate(phrase) words = split(phrase, " ") - fragments = map(translateFragment, words) + fragments = map(translatefragment, words) join(fragments, " ") end -vowelSound = r"^([aeiou]|xr|yt)" -consonantSound = r"^([^aeiou]+(?=y)|[^aeiou]?qu|[^aeiou]+)([a-z]+)" +vowelsound = r"^([aeiou]|xr|yt)" +consonantsound = r"^([^aeiou]+(?=y)|[^aeiou]?qu|[^aeiou]+)([a-z]+)" -function translateFragment(fragment) +function translatefragment(fragment) translated = fragment - if !occursin(vowelSound, fragment) - translated = replace(fragment, consonantSound => s"\g<2>\g<1>") + if !occursin(vowelsound, fragment) + translated = replace(fragment, consonantsound => s"\g<2>\g<1>") end translated * "ay" diff --git a/exercises/practice/pig-latin/runtests.jl b/exercises/practice/pig-latin/runtests.jl index a893c6d3..ce6b97ca 100644 --- a/exercises/practice/pig-latin/runtests.jl +++ b/exercises/practice/pig-latin/runtests.jl @@ -3,92 +3,99 @@ using Test include("pig-latin.jl") @testset verbose = true "tests" begin - @testset "word beginning with a" begin - @test translate("apple") == "appleay" - end - - @testset "word beginning with e" begin - @test translate("ear") == "earay" - end - - @testset "word beginning with i" begin - @test translate("igloo") == "iglooay" - end - - @testset "word beginning with o" begin - @test translate("object") == "objectay" - end - - @testset "word beginning with u" begin - @test translate("under") == "underay" - end - - @testset "word beginning with a vowel and followed by a qu" begin - @test translate("equal") == "equalay" - end - - @testset "word beginning with p" begin - @test translate("pig") == "igpay" - end - - @testset "word beginning with k" begin - @test translate("koala") == "oalakay" - end - - @testset "word beginning with x" begin - @test translate("xenon") == "enonxay" - end - - @testset "word beginning with q without a following u" begin - @test translate("qat") == "atqay" - end - - @testset "word beginning with ch" begin - @test translate("chair") == "airchay" - end - - @testset "word beginning with qu" begin - @test translate("queen") == "eenquay" - end - - @testset "word beginning with qu and a preceding consonant" begin - @test translate("square") == "aresquay" - end - - @testset "word beginning with th" begin - @test translate("therapy") == "erapythay" - end - - @testset "word beginning with thr" begin - @test translate("thrush") == "ushthray" - end - - @testset "word beginning with sch" begin - @test translate("school") == "oolschay" - end - - @testset "word beginning with yt" begin - @test translate("yttria") == "yttriaay" - end - - @testset "word beginning with xr" begin - @test translate("xray") == "xrayay" - end - - @testset "y is treated like a consonant at the beginning of a word" begin - @test translate("yellow") == "ellowyay" - end - - @testset "y is treated like a vowel at the end of a consonant cluster" begin - @test translate("rhythm") == "ythmrhay" - end - - @testset "y as second letter in two letter word" begin - @test translate("my") == "ymay" - end - - @testset "a whole phrase" begin - @test translate("quick fast run") == "ickquay astfay unray" + @testset "ay is added to words that begin with vowels" begin + @testset "word beginning with a" begin + @test translate("apple") == "appleay" + end + + @testset "word beginning with e" begin + @test translate("ear") == "earay" + end + + @testset "word beginning with i" begin + @test translate("igloo") == "iglooay" + end + + @testset "word beginning with o" begin + @test translate("object") == "objectay" + end + + @testset "word beginning with u" begin + @test translate("under") == "underay" + end + + @testset "word beginning with a vowel and followed by a qu" begin + @test translate("equal") == "equalay" + end + end + + @testset "some letter clusters are treated like a single consonant" begin + @testset "word beginning with p" begin + @test translate("pig") == "igpay" + end + + @testset "word beginning with k" begin + @test translate("koala") == "oalakay" + end + + @testset "word beginning with x" begin + @test translate("xenon") == "enonxay" + end + + @testset "word beginning with q without a following u" begin + @test translate("qat") == "atqay" + end + + @testset "word beginning with ch" begin + @test translate("chair") == "airchay" + end + + @testset "word beginning with qu" begin + @test translate("queen") == "eenquay" + end + + @testset "word beginning with qu and a preceding consonant" begin + @test translate("square") == "aresquay" + end + + @testset "word beginning with th" begin + @test translate("therapy") == "erapythay" + end + + @testset "word beginning with thr" begin + @test translate("thrush") == "ushthray" + end + + @testset "word beginning with sch" begin + @test translate("school") == "oolschay" + end + end + + @testset "some letter clusters are treated like a single vowel" begin + @testset "word beginning with yt" begin + @test translate("yttria") == "yttriaay" + end + + @testset "word beginning with xr" begin + @test translate("xray") == "xrayay" + end + + @testset "y is treated like a consonant at the beginning of a word" begin + @test translate("yellow") == "ellowyay" + end + + @testset "y is treated like a vowel at the end of a consonant cluster" begin + @test translate("rhythm") == "ythmrhay" + end + + @testset "y as second letter in two letter word" begin + @test translate("my") == "ymay" + end + end + + @testset "phrases are translated" begin + @testset "a whole phrase" begin + @test translate("quick fast run") == "ickquay astfay unray" + end end end - From b87306c584b188e68bb3744842f59972ece170f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20B=20Nagy?= <20251272+BNAndras@users.noreply.github.com> Date: Fri, 12 Jul 2024 10:03:32 -0700 Subject: [PATCH 4/4] Fix testset grouping --- exercises/practice/pig-latin/runtests.jl | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/exercises/practice/pig-latin/runtests.jl b/exercises/practice/pig-latin/runtests.jl index ce6b97ca..cdf47fb4 100644 --- a/exercises/practice/pig-latin/runtests.jl +++ b/exercises/practice/pig-latin/runtests.jl @@ -29,7 +29,7 @@ include("pig-latin.jl") end end - @testset "some letter clusters are treated like a single consonant" begin + @testset "first letter and ay are moved to the end of words that start with consonants" begin @testset "word beginning with p" begin @test translate("pig") == "igpay" end @@ -45,7 +45,9 @@ include("pig-latin.jl") @testset "word beginning with q without a following u" begin @test translate("qat") == "atqay" end - + end + + @testset "some letter clusters are treated like a single consonant" begin @testset "word beginning with ch" begin @test translate("chair") == "airchay" end @@ -79,7 +81,9 @@ include("pig-latin.jl") @testset "word beginning with xr" begin @test translate("xray") == "xrayay" end - + end + + @testset "position of y in a word determines if it is a consonant or a vowel" begin @testset "y is treated like a consonant at the beginning of a word" begin @test translate("yellow") == "ellowyay" end