Coding Task
Задача: Дан текст. Посчитать сколько каждое слово встречается раз.
func wordFrequency(text: String) -> [String: Int] {
var words = text.lowercased().components(separatedBy: .whitespacesAndNewlines)
var wordAndCount: [String: Int] = [:]
for word in words {
wordAndCount[word] = (wordAndCount[word] ?? 0) + 1
}
return wordAndCount
}
func wordFrequency(text: String) -> [String: Int] {
var words = text.lowercased().components(separatedBy: .alphanumerics.inverted).filter({ !$0.isEmpty })
var wordAndCount: [String: Int] = [:]
for word in words {
wordAndCount[word] = (wordAndCount[word] ?? 0) + 1
}
return wordAndCount
}
Вложения

