Sha256: a92153362bb7f58af6352d5e64876d38dc4ed85a03d29943b9b2130a6c39d43e

Contents?: true

Size: 1.96 KB

Versions: 266

Compression:

Stored size: 1.96 KB

Contents

import Foundation

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

266 entries across 266 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/swift/exercises/pig-latin/Sources/PigLatinExample.swift
trackler-2.2.1.179 tracks/swift/exercises/pig-latin/Sources/PigLatinExample.swift
trackler-2.2.1.178 tracks/swift/exercises/pig-latin/Sources/PigLatinExample.swift
trackler-2.2.1.177 tracks/swift/exercises/pig-latin/Sources/PigLatinExample.swift
trackler-2.2.1.176 tracks/swift/exercises/pig-latin/Sources/PigLatinExample.swift
trackler-2.2.1.175 tracks/swift/exercises/pig-latin/Sources/PigLatinExample.swift
trackler-2.2.1.174 tracks/swift/exercises/pig-latin/Sources/PigLatinExample.swift
trackler-2.2.1.173 tracks/swift/exercises/pig-latin/Sources/PigLatinExample.swift
trackler-2.2.1.172 tracks/swift/exercises/pig-latin/Sources/PigLatinExample.swift
trackler-2.2.1.171 tracks/swift/exercises/pig-latin/Sources/PigLatinExample.swift
trackler-2.2.1.170 tracks/swift/exercises/pig-latin/Sources/PigLatinExample.swift
trackler-2.2.1.169 tracks/swift/exercises/pig-latin/Sources/PigLatinExample.swift
trackler-2.2.1.167 tracks/swift/exercises/pig-latin/Sources/PigLatinExample.swift
trackler-2.2.1.166 tracks/swift/exercises/pig-latin/Sources/PigLatinExample.swift
trackler-2.2.1.165 tracks/swift/exercises/pig-latin/Sources/PigLatinExample.swift
trackler-2.2.1.164 tracks/swift/exercises/pig-latin/Sources/PigLatinExample.swift
trackler-2.2.1.163 tracks/swift/exercises/pig-latin/Sources/PigLatinExample.swift
trackler-2.2.1.162 tracks/swift/exercises/pig-latin/Sources/PigLatinExample.swift
trackler-2.2.1.161 tracks/swift/exercises/pig-latin/Sources/PigLatinExample.swift
trackler-2.2.1.160 tracks/swift/exercises/pig-latin/Sources/PigLatinExample.swift