Sha256: 53f8b00042daf179a571d1c628acc75a7dc32e161e1a4cf4a0267c07aad4b58a

Contents?: true

Size: 968 Bytes

Versions: 59

Compression:

Stored size: 968 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

59 entries across 59 versions & 1 rubygems

Version Path
trackler-2.0.8.30 tracks/swift/exercises/word-count/Sources/WordCountExample.swift
trackler-2.0.8.29 tracks/swift/exercises/word-count/Sources/WordCountExample.swift
trackler-2.0.8.28 tracks/swift/exercises/word-count/Sources/WordCountExample.swift
trackler-2.0.8.27 tracks/swift/exercises/word-count/Sources/WordCountExample.swift
trackler-2.0.8.26 tracks/swift/exercises/word-count/Sources/WordCountExample.swift
trackler-2.0.8.24 tracks/swift/exercises/word-count/Sources/WordCountExample.swift
trackler-2.0.8.23 tracks/swift/exercises/word-count/Sources/WordCountExample.swift
trackler-2.0.8.22 tracks/swift/exercises/word-count/Sources/WordCountExample.swift
trackler-2.0.8.21 tracks/swift/exercises/word-count/Sources/WordCountExample.swift
trackler-2.0.8.20 tracks/swift/exercises/word-count/Sources/WordCountExample.swift
trackler-2.0.8.19 tracks/swift/exercises/word-count/Sources/WordCountExample.swift
trackler-2.0.8.18 tracks/swift/exercises/word-count/Sources/WordCountExample.swift
trackler-2.0.8.17 tracks/swift/exercises/word-count/Sources/WordCountExample.swift
trackler-2.0.8.16 tracks/swift/exercises/word-count/Sources/WordCountExample.swift
trackler-2.0.8.15 tracks/swift/exercises/word-count/Sources/WordCountExample.swift
trackler-2.0.8.14 tracks/swift/exercises/word-count/Sources/WordCountExample.swift
trackler-2.0.8.13 tracks/swift/exercises/word-count/Sources/WordCountExample.swift
trackler-2.0.8.12 tracks/swift/exercises/word-count/Sources/WordCountExample.swift
trackler-2.0.8.11 tracks/swift/exercises/word-count/Sources/WordCountExample.swift
trackler-2.0.8.10 tracks/swift/exercises/word-count/Sources/WordCountExample.swift