Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
19
Task/Letter-frequency/Swift/letter-frequency.swift
Normal file
19
Task/Letter-frequency/Swift/letter-frequency.swift
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import Foundation
|
||||
|
||||
let dictPath: String
|
||||
|
||||
switch CommandLine.arguments.count {
|
||||
case 2:
|
||||
dictPath = CommandLine.arguments[1]
|
||||
case _:
|
||||
dictPath = "/usr/share/dict/words"
|
||||
}
|
||||
|
||||
let wordsData = FileManager.default.contents(atPath: dictPath)!
|
||||
let allWords = String(data: wordsData, encoding: .utf8)!
|
||||
let words = allWords.components(separatedBy: "\n")
|
||||
let counts = words.flatMap({ $0.map({ ($0, 1) }) }).reduce(into: [:], { $0[$1.0, default: 0] += $1.1 })
|
||||
|
||||
for (char, count) in counts {
|
||||
print("\(char): \(count)")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue