From 31bca48debd6f2ebb2b7f9ca9fc119f771d88f44 Mon Sep 17 00:00:00 2001 From: Ric Emery Date: Mon, 2 Oct 2017 10:05:45 -0700 Subject: [PATCH] Update PigLatinTest to version 1.1.0. Refs #370, #331 --- exercises/pig-latin/example.scala | 37 +++++-------------- .../src/test/scala/PigLatinTest.scala | 22 ++++++++--- 2 files changed, 25 insertions(+), 34 deletions(-) diff --git a/exercises/pig-latin/example.scala b/exercises/pig-latin/example.scala index c938ceae..aa2ef458 100644 --- a/exercises/pig-latin/example.scala +++ b/exercises/pig-latin/example.scala @@ -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+)(?ch|qu|thr|th|sch|yt|rh|\\wqu|\\w)(?\\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) - } } diff --git a/exercises/pig-latin/src/test/scala/PigLatinTest.scala b/exercises/pig-latin/src/test/scala/PigLatinTest.scala index cc687412..edb4e72e 100644 --- a/exercises/pig-latin/src/test/scala/PigLatinTest.scala +++ b/exercises/pig-latin/src/test/scala/PigLatinTest.scala @@ -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") { @@ -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") @@ -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")