Sha256: a499cf0a10cf3c8928f7e800bd745237fc5415bca1e0026e29c194c97dad3c6f

Contents?: true

Size: 1.93 KB

Versions: 71

Compression:

Stored size: 1.93 KB

Contents

private extension String {
    func substringFromIndexInt(_ indx: Int) -> String {
        let index = self.characters.index(self.startIndex, offsetBy: indx)
        return self.substring(from: index)
    }

    func substringWithRangeInt(_ intRange: Range<Int>) -> String {
        let start = self.characters.index(self.startIndex, offsetBy: intRange.lowerBound)
        let end = self.characters.index(self.startIndex, offsetBy: intRange.upperBound)
        return self.substring(with: start..<end)
    }
}

struct PigLatin {

    static func translate (_ word: String) -> String {
        return word.components(separatedBy: " ").map {self.translateWord($0)}.joined(separator: " ")
    }

    static func translateWord(_ word: String) -> String {

        func wordStartsWithPrefixes(_ word: String, prefixes: [String]) -> Bool {
            return 0 < prefixes.filter {word.hasPrefix($0)}.count
        }

        func wordStartsWithVowelLike(_ word: String) -> Bool {
            return wordStartsWithPrefixes(word, prefixes: ["xr", "yt", "a", "e", "i", "o", "u" ])
        }

        func wordStartsWithConsonantAndQu(_ word: String) -> Bool {
            let index = word.characters.index(word.startIndex, offsetBy: 1)
            return word.substring(from: index).hasPrefix("qu")
        }

        if wordStartsWithVowelLike(word) {return word + "ay"}
        if wordStartsWithPrefixes(word, prefixes: ["thr", "sch"]) {
            return (word.substringFromIndexInt(3) + word.substringWithRangeInt(0..<3) + "ay") }

        if wordStartsWithPrefixes(word, prefixes: ["ch", "qu", "th"]) {
            return word.substringFromIndexInt(2) +
                word.substringWithRangeInt(0..<2) + "ay" }
        if wordStartsWithConsonantAndQu(word) {
            return word.substringFromIndexInt(3) +
                word.substringWithRangeInt(0..<1) + "quay"}

        return word.substringFromIndexInt(1) +
            word.substringWithRangeInt(0..<1) + "ay"

    }

}

Version data entries

71 entries across 71 versions & 1 rubygems

Version Path
trackler-2.0.6.15 tracks/swift/exercises/pig-latin/PigLatinExample.swift
trackler-2.0.6.14 tracks/swift/exercises/pig-latin/PigLatinExample.swift
trackler-2.0.6.13 tracks/swift/exercises/pig-latin/PigLatinExample.swift
trackler-2.0.6.12 tracks/swift/exercises/pig-latin/PigLatinExample.swift
trackler-2.0.6.11 tracks/swift/exercises/pig-latin/PigLatinExample.swift
trackler-2.0.6.10 tracks/swift/exercises/pig-latin/PigLatinExample.swift
trackler-2.0.6.9 tracks/swift/exercises/pig-latin/PigLatinExample.swift
trackler-2.0.6.8 tracks/swift/exercises/pig-latin/PigLatinExample.swift
trackler-2.0.6.7 tracks/swift/exercises/pig-latin/PigLatinExample.swift
trackler-2.0.6.6 tracks/swift/exercises/pig-latin/PigLatinExample.swift
trackler-2.0.6.5 tracks/swift/exercises/pig-latin/PigLatinExample.swift
trackler-2.0.6.4 tracks/swift/exercises/pig-latin/PigLatinExample.swift
trackler-2.0.6.3 tracks/swift/exercises/pig-latin/PigLatinExample.swift
trackler-2.0.6.2 tracks/swift/exercises/pig-latin/PigLatinExample.swift
trackler-2.0.6.1 tracks/swift/exercises/pig-latin/PigLatinExample.swift
trackler-2.0.6.0 tracks/swift/exercises/pig-latin/PigLatinExample.swift
trackler-2.0.5.18 tracks/swift/exercises/pig-latin/PigLatinExample.swift
trackler-2.0.5.17 tracks/swift/exercises/pig-latin/PigLatinExample.swift
trackler-2.0.5.16 tracks/swift/exercises/pig-latin/PigLatinExample.swift
trackler-2.0.5.15 tracks/swift/exercises/pig-latin/PigLatinExample.swift