Sha256: cd1e1c3e08ed46f35efdddfefdca8a5035809c4fa1b509be8056eb1f3f858077

Contents?: true

Size: 818 Bytes

Versions: 126

Compression:

Stored size: 818 Bytes

Contents

object PigLatin {

    fun translate(phrase: String) = phrase.split(Regex("\\s+")).map({ translateWord(it) }).joinToString(" ")

    private fun translateWord(word: String): String {
        if (startsWithVowelSound(word)) {
            return word + "ay"
        }

        val groups = splitInitialConsonantSound(word)
        if(groups != null) {
            return groups[1] + groups[0] + "ay"
        }

        return word
    }

    private val consonant = Regex("^([^aeiou]?qu|[^aeiouy]+|[^aeiou]+)([a-z]*)", RegexOption.IGNORE_CASE)
    private fun splitInitialConsonantSound(word: String) = consonant.matchEntire(word)?.groupValues?.drop(1)

    private val vowels = Regex("^([aeiou]|y[^aeiou]|xr)[a-z]*", RegexOption.IGNORE_CASE)
    private fun startsWithVowelSound(word: String) = vowels.matches(word)

}

Version data entries

126 entries across 126 versions & 1 rubygems

Version Path
trackler-2.2.1.119 tracks/kotlin/exercises/pig-latin/.meta/src/reference/kotlin/PigLatin.kt
trackler-2.2.1.118 tracks/kotlin/exercises/pig-latin/.meta/src/reference/kotlin/PigLatin.kt
trackler-2.2.1.117 tracks/kotlin/exercises/pig-latin/.meta/src/reference/kotlin/PigLatin.kt
trackler-2.2.1.116 tracks/kotlin/exercises/pig-latin/.meta/src/reference/kotlin/PigLatin.kt
trackler-2.2.1.115 tracks/kotlin/exercises/pig-latin/.meta/src/reference/kotlin/PigLatin.kt
trackler-2.2.1.114 tracks/kotlin/exercises/pig-latin/.meta/src/reference/kotlin/PigLatin.kt
trackler-2.2.1.113 tracks/kotlin/exercises/pig-latin/.meta/src/reference/kotlin/PigLatin.kt
trackler-2.2.1.111 tracks/kotlin/exercises/pig-latin/.meta/src/reference/kotlin/PigLatin.kt
trackler-2.2.1.110 tracks/kotlin/exercises/pig-latin/.meta/src/reference/kotlin/PigLatin.kt
trackler-2.2.1.109 tracks/kotlin/exercises/pig-latin/.meta/src/reference/kotlin/PigLatin.kt
trackler-2.2.1.108 tracks/kotlin/exercises/pig-latin/.meta/src/reference/kotlin/PigLatin.kt
trackler-2.2.1.107 tracks/kotlin/exercises/pig-latin/.meta/src/reference/kotlin/PigLatin.kt
trackler-2.2.1.106 tracks/kotlin/exercises/pig-latin/.meta/src/reference/kotlin/PigLatin.kt
trackler-2.2.1.105 tracks/kotlin/exercises/pig-latin/.meta/src/reference/kotlin/PigLatin.kt
trackler-2.2.1.104 tracks/kotlin/exercises/pig-latin/.meta/src/reference/kotlin/PigLatin.kt
trackler-2.2.1.103 tracks/kotlin/exercises/pig-latin/.meta/src/reference/kotlin/PigLatin.kt
trackler-2.2.1.102 tracks/kotlin/exercises/pig-latin/.meta/src/reference/kotlin/PigLatin.kt
trackler-2.2.1.101 tracks/kotlin/exercises/pig-latin/.meta/src/reference/kotlin/PigLatin.kt
trackler-2.2.1.100 tracks/kotlin/exercises/pig-latin/.meta/src/reference/kotlin/PigLatin.kt
trackler-2.2.1.99 tracks/kotlin/exercises/pig-latin/.meta/src/reference/kotlin/PigLatin.kt