Sha256: da05a06fc674a1153b403ed5e54c61ba5189fac64bab9d272b82f321cd4b19d5

Contents?: true

Size: 896 Bytes

Versions: 24

Compression:

Stored size: 896 Bytes

Contents

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 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)
  }
}

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
trackler-2.2.1.39 tracks/scala/exercises/pig-latin/example.scala
trackler-2.2.1.38 tracks/scala/exercises/pig-latin/example.scala
trackler-2.2.1.37 tracks/scala/exercises/pig-latin/example.scala
trackler-2.2.1.36 tracks/scala/exercises/pig-latin/example.scala
trackler-2.2.1.35 tracks/scala/exercises/pig-latin/example.scala
trackler-2.2.1.34 tracks/scala/exercises/pig-latin/example.scala
trackler-2.2.1.33 tracks/scala/exercises/pig-latin/example.scala
trackler-2.2.1.32 tracks/scala/exercises/pig-latin/example.scala
trackler-2.2.1.31 tracks/scala/exercises/pig-latin/example.scala
trackler-2.2.1.30 tracks/scala/exercises/pig-latin/example.scala
trackler-2.2.1.29 tracks/scala/exercises/pig-latin/example.scala
trackler-2.2.1.28 tracks/scala/exercises/pig-latin/example.scala
trackler-2.2.1.27 tracks/scala/exercises/pig-latin/example.scala
trackler-2.2.1.26 tracks/scala/exercises/pig-latin/example.scala
trackler-2.2.1.25 tracks/scala/exercises/pig-latin/example.scala
trackler-2.2.1.24 tracks/scala/exercises/pig-latin/example.scala
trackler-2.2.1.23 tracks/scala/exercises/pig-latin/example.scala
trackler-2.2.1.22 tracks/scala/exercises/pig-latin/example.scala
trackler-2.2.1.21 tracks/scala/exercises/pig-latin/example.scala
trackler-2.2.1.20 tracks/scala/exercises/pig-latin/example.scala