Sha256: 18fb0bd447a3abc55f5df51488c7387a1c340a10abd9f3ad50abc4ad6dae34a8

Contents?: true

Size: 970 Bytes

Versions: 78

Compression:

Stored size: 970 Bytes

Contents

struct WordCount {

    func splitStringToArray(_ inString: String) -> [String] {
        return inString.characters.split(whereSeparator: { splitAt($0) }).map { String($0) }
    }

    func splitAt(_ characterToCompare: Character, charToSplitAt: String = " !&$%^&,:") -> Bool {
        for each in charToSplitAt.characters {
            if each == characterToCompare {
                return true
            }
        }
        return false
    }

    let words: String

    init(words: String) {
        self.words = words
    }

    func count() -> [String: Int] {
        var dict = [String: Int]()
        let cleanArray = splitStringToArray(words)

        cleanArray.forEach { string in
            if !string.isEmpty {
                if let count = dict[string.lowercased()] {
                    dict[string.lowercased()] = count + 1
                } else { dict[string.lowercased()] = 1
                }
            }
        }
        return dict
    }
}

Version data entries

78 entries across 78 versions & 1 rubygems

Version Path
trackler-2.1.0.53 tracks/swift/exercises/word-count/Sources/WordCountExample.swift
trackler-2.1.0.52 tracks/swift/exercises/word-count/Sources/WordCountExample.swift
trackler-2.1.0.51 tracks/swift/exercises/word-count/Sources/WordCountExample.swift
trackler-2.1.0.50 tracks/swift/exercises/word-count/Sources/WordCountExample.swift
trackler-2.1.0.49 tracks/swift/exercises/word-count/Sources/WordCountExample.swift
trackler-2.1.0.48 tracks/swift/exercises/word-count/Sources/WordCountExample.swift
trackler-2.1.0.47 tracks/swift/exercises/word-count/Sources/WordCountExample.swift
trackler-2.1.0.46 tracks/swift/exercises/word-count/Sources/WordCountExample.swift
trackler-2.1.0.45 tracks/swift/exercises/word-count/Sources/WordCountExample.swift
trackler-2.1.0.44 tracks/swift/exercises/word-count/Sources/WordCountExample.swift
trackler-2.1.0.43 tracks/swift/exercises/word-count/Sources/WordCountExample.swift
trackler-2.1.0.42 tracks/swift/exercises/word-count/Sources/WordCountExample.swift
trackler-2.1.0.41 tracks/swift/exercises/word-count/Sources/WordCountExample.swift
trackler-2.1.0.40 tracks/swift/exercises/word-count/Sources/WordCountExample.swift
trackler-2.1.0.39 tracks/swift/exercises/word-count/Sources/WordCountExample.swift
trackler-2.1.0.38 tracks/swift/exercises/word-count/Sources/WordCountExample.swift
trackler-2.1.0.37 tracks/swift/exercises/word-count/Sources/WordCountExample.swift
trackler-2.1.0.36 tracks/swift/exercises/word-count/Sources/WordCountExample.swift
trackler-2.1.0.34 tracks/swift/exercises/word-count/Sources/WordCountExample.swift
trackler-2.1.0.33 tracks/swift/exercises/word-count/Sources/WordCountExample.swift