-
-
Notifications
You must be signed in to change notification settings - Fork 111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new practice exercise pig-latin
#691
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Instructions | ||
|
||
Your task is to translate text from English to Pig Latin using the following rules: | ||
|
||
- **Rule 1**: If a word begins with a vowel sound, add an "ay" sound to the end of the word (e.g. "apple" -> "appleay"). | ||
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 (e.g. "pig" -> "igpay"). | ||
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 them 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"). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"authors": [ | ||
"jiegillet" | ||
], | ||
"files": { | ||
"solution": [ | ||
"src/PigLatin.elm" | ||
], | ||
"test": [ | ||
"tests/Tests.elm" | ||
], | ||
"example": [ | ||
".meta/src/PigLatin.example.elm" | ||
] | ||
}, | ||
"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/" | ||
} |
56 changes: 56 additions & 0 deletions
56
exercises/practice/pig-latin/.meta/src/PigLatin.example.elm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
module PigLatin exposing (translate) | ||
|
||
|
||
translate : String -> String | ||
translate = | ||
String.words >> List.map translateWord >> String.join " " | ||
|
||
|
||
translateWord : String -> String | ||
translateWord input = | ||
if String.startsWith "xr" input || String.startsWith "yt" input then | ||
input ++ "ay" | ||
|
||
else if String.startsWith "y" input then | ||
String.dropLeft 1 input ++ "yay" | ||
|
||
else | ||
let | ||
( consonnants, rest ) = | ||
splitConsonant input | ||
in | ||
if String.endsWith "q" consonnants && String.startsWith "u" rest then | ||
String.dropLeft 1 rest ++ consonnants ++ "uay" | ||
|
||
else | ||
rest ++ consonnants ++ "ay" | ||
|
||
|
||
splitConsonant : String -> ( String, String ) | ||
splitConsonant input = | ||
let | ||
isVowelOrY char = | ||
List.member char [ 'a', 'e', 'i', 'o', 'u', 'y' ] | ||
|
||
( first, second ) = | ||
input |> String.toList |> takeWhile (isVowelOrY >> not) | ||
in | ||
( String.fromList first, String.fromList second ) | ||
|
||
|
||
takeWhile : (a -> Bool) -> List a -> ( List a, List a ) | ||
takeWhile condition list = | ||
case list of | ||
[] -> | ||
( [], [] ) | ||
|
||
head :: tail -> | ||
if condition head then | ||
let | ||
( first, second ) = | ||
takeWhile condition tail | ||
in | ||
( head :: first, second ) | ||
|
||
else | ||
( [], list ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"type": "application", | ||
"source-directories": [ | ||
"src" | ||
], | ||
"elm-version": "0.19.1", | ||
"dependencies": { | ||
"direct": { | ||
"elm/core": "1.0.5", | ||
"elm/json": "1.1.3", | ||
"elm/parser": "1.1.0", | ||
"elm/random": "1.0.0", | ||
"elm/regex": "1.0.0", | ||
"elm/time": "1.0.0" | ||
}, | ||
"indirect": {} | ||
}, | ||
"test-dependencies": { | ||
"direct": { | ||
"elm-explorations/test": "2.1.0", | ||
"rtfeldman/elm-iso8601-date-strings": "1.1.4" | ||
}, | ||
"indirect": { | ||
"elm/bytes": "1.0.8", | ||
"elm/html": "1.0.0", | ||
"elm/virtual-dom": "1.0.3" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module PigLatin exposing (translate) | ||
|
||
|
||
translate : String -> String | ||
translate input = | ||
Debug.todo "Please implement translate" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
module Tests exposing (tests) | ||
|
||
import Expect | ||
import PigLatin | ||
import Test exposing (Test, describe, skip, test) | ||
|
||
|
||
tests : Test | ||
tests = | ||
describe "PigLatin" | ||
[ describe "ay is added to words that start with vowels" | ||
[ -- skip <| | ||
test "word beginning with a" <| | ||
\() -> | ||
PigLatin.translate "apple" | ||
|> Expect.equal "appleay" | ||
, skip <| | ||
test "word beginning with e" <| | ||
\() -> | ||
PigLatin.translate "ear" | ||
|> Expect.equal "earay" | ||
, skip <| | ||
test "word beginning with i" <| | ||
\() -> | ||
PigLatin.translate "igloo" | ||
|> Expect.equal "iglooay" | ||
, skip <| | ||
test "word beginning with o" <| | ||
\() -> | ||
PigLatin.translate "object" | ||
|> Expect.equal "objectay" | ||
, skip <| | ||
test "word beginning with u" <| | ||
\() -> | ||
PigLatin.translate "under" | ||
|> Expect.equal "underay" | ||
, skip <| | ||
test "word beginning with a vowel and followed by a qu" <| | ||
\() -> | ||
PigLatin.translate "equal" | ||
|> Expect.equal "equalay" | ||
] | ||
, describe "first letter and ay are moved to the end of words that start with consonants" | ||
[ skip <| | ||
test "word beginning with p" <| | ||
\() -> | ||
PigLatin.translate "pig" | ||
|> Expect.equal "igpay" | ||
, skip <| | ||
test "word beginning with k" <| | ||
\() -> | ||
PigLatin.translate "koala" | ||
|> Expect.equal "oalakay" | ||
, skip <| | ||
test "word beginning with x" <| | ||
\() -> | ||
PigLatin.translate "xenon" | ||
|> Expect.equal "enonxay" | ||
, skip <| | ||
test "word beginning with q without a following u" <| | ||
\() -> | ||
PigLatin.translate "qat" | ||
|> Expect.equal "atqay" | ||
] | ||
, describe "some letter clusters are treated like a single consonant" | ||
[ skip <| | ||
test "word beginning with ch" <| | ||
\() -> | ||
PigLatin.translate "chair" | ||
|> Expect.equal "airchay" | ||
, skip <| | ||
test "word beginning with qu" <| | ||
\() -> | ||
PigLatin.translate "queen" | ||
|> Expect.equal "eenquay" | ||
, skip <| | ||
test "word beginning with qu and a preceding consonant" <| | ||
\() -> | ||
PigLatin.translate "square" | ||
|> Expect.equal "aresquay" | ||
, skip <| | ||
test "word beginning with th" <| | ||
\() -> | ||
PigLatin.translate "therapy" | ||
|> Expect.equal "erapythay" | ||
, skip <| | ||
test "word beginning with thr" <| | ||
\() -> | ||
PigLatin.translate "thrush" | ||
|> Expect.equal "ushthray" | ||
, skip <| | ||
test "word beginning with sch" <| | ||
\() -> | ||
PigLatin.translate "school" | ||
|> Expect.equal "oolschay" | ||
] | ||
, describe "some letter clusters are treated like a single vowel" | ||
[ skip <| | ||
test "word beginning with yt" <| | ||
\() -> | ||
PigLatin.translate "yttria" | ||
|> Expect.equal "yttriaay" | ||
, skip <| | ||
test "word beginning with xr" <| | ||
\() -> | ||
PigLatin.translate "xray" | ||
|> Expect.equal "xrayay" | ||
] | ||
, describe "position of y in a word determines if it is a consonant or a vowel" | ||
[ skip <| | ||
test "y is treated like a consonant at the beginning of a word" <| | ||
\() -> | ||
PigLatin.translate "yellow" | ||
|> Expect.equal "ellowyay" | ||
, skip <| | ||
test "y is treated like a vowel at the end of a consonant cluster" <| | ||
\() -> | ||
PigLatin.translate "rhythm" | ||
|> Expect.equal "ythmrhay" | ||
, skip <| | ||
test "y as second letter in two letter word" <| | ||
\() -> | ||
PigLatin.translate "my" | ||
|> Expect.equal "ymay" | ||
] | ||
, describe "phrases are translated" | ||
[ skip <| | ||
test "a whole phrase" <| | ||
\() -> | ||
PigLatin.translate "quick fast run" | ||
|> Expect.equal "ickquay astfay unray" | ||
] | ||
] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is picky, but I would probably rename this function. takeWhile doesn't seem to be what it is doing really. Maybe
split
orslice
or something like that?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It takes the first part of a list while a condition is true. The only difference with the usual
takeWhile
is that it returns both parts of the list, and that's clear with the type signature. I think the name makes sense.