Sha256: 810edd21ca6db7f25dec178030baa9e16acda278daadba7e68c6448798741bee

Contents?: true

Size: 773 Bytes

Versions: 233

Compression:

Stored size: 773 Bytes

Contents

object PigLatin {
  def translate(phrase: String): String = {
    phrase.split("\\s+").map(translateWord).mkString(" ")
  }

  def translateWord(str: String): String = {
    val lowercase = str.toLowerCase
    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

233 entries across 233 versions & 1 rubygems

Version Path
trackler-2.2.1.15 tracks/scala/exercises/pig-latin/example.scala
trackler-2.2.1.14 tracks/scala/exercises/pig-latin/example.scala
trackler-2.2.1.13 tracks/scala/exercises/pig-latin/example.scala
trackler-2.2.1.12 tracks/scala/exercises/pig-latin/example.scala
trackler-2.2.1.11 tracks/scala/exercises/pig-latin/example.scala
trackler-2.2.1.10 tracks/scala/exercises/pig-latin/example.scala
trackler-2.2.1.9 tracks/scala/exercises/pig-latin/example.scala
trackler-2.2.1.8 tracks/scala/exercises/pig-latin/example.scala
trackler-2.2.1.7 tracks/scala/exercises/pig-latin/example.scala
trackler-2.2.1.6 tracks/scala/exercises/pig-latin/example.scala
trackler-2.2.1.5 tracks/scala/exercises/pig-latin/example.scala
trackler-2.2.1.4 tracks/scala/exercises/pig-latin/example.scala
trackler-2.2.1.3 tracks/scala/exercises/pig-latin/example.scala
trackler-2.2.1.2 tracks/scala/exercises/pig-latin/example.scala
trackler-2.2.1.1 tracks/scala/exercises/pig-latin/example.scala
trackler-2.2.1.0 tracks/scala/exercises/pig-latin/example.scala
trackler-2.2.0.6 tracks/scala/exercises/pig-latin/example.scala
trackler-2.2.0.5 tracks/scala/exercises/pig-latin/example.scala
trackler-2.2.0.4 tracks/scala/exercises/pig-latin/example.scala
trackler-2.2.0.3 tracks/scala/exercises/pig-latin/example.scala