🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of SergiiVlasiuk

SergiiVlasiuk's solution

to Pig Latin in the Scala Track

Published at Aug 24 2019 · 0 comments
Instructions
Test suite
Solution

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.
  • 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.

There are a few more rules for edge cases, and there are regional variants too.

See http://en.wikipedia.org/wiki/Pig_latin for more details.

The Scala exercises assume an SBT project scheme. The exercise solution source should be placed within the exercise directory/src/main/scala. The exercise unit tests can be found within the exercise directory/src/test/scala.

To run the tests simply run the command sbt test in the exercise directory.

For more detailed info about the Scala track see the help page.

Source

The Pig Latin exercise at Test First Teaching by Ultrasaurus https://github.com/ultrasaurus/test-first-teaching/blob/master/learn_ruby/pig_latin/

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

PigLatinTest.scala

import org.scalatest.{Matchers, FunSuite}

/** @version 1.2.0 */
class PigLatinTest extends FunSuite with Matchers {

  test("word beginning with a") {
    PigLatin.translate("apple") should be("appleay")
  }

  test("word beginning with e") {
    pending
    PigLatin.translate("ear") should be("earay")
  }

  test("word beginning with i") {
    pending
    PigLatin.translate("igloo") should be("iglooay")
  }

  test("word beginning with o") {
    pending
    PigLatin.translate("object") should be("objectay")
  }

  test("word beginning with u") {
    pending
    PigLatin.translate("under") should be("underay")
  }

  test("word beginning with a vowel and followed by a qu") {
    pending
    PigLatin.translate("equal") should be("equalay")
  }

  test("word beginning with p") {
    pending
    PigLatin.translate("pig") should be("igpay")
  }

  test("word beginning with k") {
    pending
    PigLatin.translate("koala") should be("oalakay")
  }

  test("word beginning with x") {
    pending
    PigLatin.translate("xenon") should be("enonxay")
  }

  test("word beginning with q without a following u") {
    pending
    PigLatin.translate("qat") should be("atqay")
  }

  test("word beginning with ch") {
    pending
    PigLatin.translate("chair") should be("airchay")
  }

  test("word beginning with qu") {
    pending
    PigLatin.translate("queen") should be("eenquay")
  }

  test("word beginning with qu and a preceding consonant") {
    pending
    PigLatin.translate("square") should be("aresquay")
  }

  test("word beginning with th") {
    pending
    PigLatin.translate("therapy") should be("erapythay")
  }

  test("word beginning with thr") {
    pending
    PigLatin.translate("thrush") should be("ushthray")
  }

  test("word beginning with sch") {
    pending
    PigLatin.translate("school") should be("oolschay")
  }

  test("word beginning with yt") {
    pending
    PigLatin.translate("yttria") should be("yttriaay")
  }

  test("word beginning with xr") {
    pending
    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")
  }
}
object PigLatin{
  def translate(phrase: String): String = {
    phrase.split("\\s").map(translateWord).mkString(" ")
  }

  def translateWord(phrase: String): String = {
    val specialVowel = """^(yt|xr)(.*)""".r
    val consonant = """^(qu|y|[^aeiouy]qu|[^aeiouy]+)(.*)""".r
    val prefix = phrase match {
      case specialVowel(first, last) => first + last
      case consonant(first, last) => last + first
      case _ => phrase
    }
    prefix ++ "ay"
  }
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?