Skip to content

Update PigLatinTest to version 1.1.0. Refs #370, #331 #468

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

Merged
merged 1 commit into from
Oct 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 9 additions & 28 deletions exercises/pig-latin/example.scala
Original file line number Diff line number Diff line change
@@ -1,31 +1,12 @@
object PigLatin {
def translate(phrase: String): String = {
phrase.split("\\s+").map(translateWord).mkString(" ")
}

def translateWord(str: String): String = {
val lowercase = str.toLowerCase

if (Seq("yt", "xr").exists(pre => lowercase.startsWith(pre))) {
lowercase + "ay"
} else {
val before = lowercase.takeWhile(!_.isLetter)
val w1 = lowercase.drop(before.length)
val w2 = w1.takeWhile(_.isLetter)
val after = w1.drop(w2.length)
val (cs, w) = consonantCluster(w2)

before ++ w ++ cs ++ "ay" ++ after
private val vowelRegex = "(^|\\s+)(a|e|i|o|u|yt|xr)(\\w+)".r
private val consonantRegex = "(^|\\s+)(?<consonant>ch|qu|thr|th|sch|yt|rh|\\wqu|\\w)(?<rest>\\w+)".r
private val vowelReplacement = "$1$2$3ay"
private val consonantReplacement = "$1$3$2ay"

def translate(phrase: String): String =
vowelRegex.findFirstIn(phrase) match {
case Some(x) => vowelRegex.replaceAllIn(phrase, vowelReplacement)
case None => consonantRegex.replaceAllIn(phrase, consonantReplacement)
}
}

private def consonantCluster(str: String): (String, String) = {
val isVowel = Set('a', 'e', 'i', 'o', 'u')
val first = str.takeWhile(!isVowel(_))
val rest = str.drop(first.length)
if (rest.startsWith("u") && first.endsWith("q"))
(first ++ "u", rest.drop(1))
else
(first, rest)
}
}
22 changes: 16 additions & 6 deletions exercises/pig-latin/src/test/scala/PigLatinTest.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import org.scalatest.{Matchers, FunSuite}

/** @version 1.0.0 */
/** @version 1.1.0 */
class PigLatinTest extends FunSuite with Matchers {

test("word beginning with a") {
Expand Down Expand Up @@ -42,11 +42,6 @@ class PigLatinTest extends FunSuite with Matchers {
PigLatin.translate("koala") should be ("oalakay")
}

test("word beginning with y") {
pending
PigLatin.translate("yellow") should be ("ellowyay")
}

test("word beginning with x") {
pending
PigLatin.translate("xenon") should be ("enonxay")
Expand Down Expand Up @@ -97,6 +92,21 @@ class PigLatinTest extends FunSuite with Matchers {
PigLatin.translate("xray") should be ("xrayay")
}

test("y is treated like a consonant at the beginning of a word") {
pending
PigLatin.translate("yellow") should be ("ellowyay")
}

test("y is treated like a vowel at the end of a consonant cluster") {
pending
PigLatin.translate("rhythm") should be ("ythmrhay")
}

test("y as second letter in two letter word") {
pending
PigLatin.translate("my") should be ("ymay")
}

test("a whole phrase") {
pending
PigLatin.translate("quick fast run") should be ("ickquay astfay unray")
Expand Down