Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
28
Task/Ordered-words/Swift/ordered-words.swift
Normal file
28
Task/Ordered-words/Swift/ordered-words.swift
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
import Foundation
|
||||
|
||||
guard
|
||||
let url = NSURL(string: "http://www.puzzlers.org/pub/wordlists/unixdict.txt"),
|
||||
let input = try? NSString(contentsOfURL: url,encoding: NSUTF8StringEncoding) as String
|
||||
else { exit(EXIT_FAILURE) }
|
||||
|
||||
let words = input.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet())
|
||||
let group: ([Int: [String]], String) -> [Int: [String]] = {
|
||||
var d = $0; let g = d[$1.characters.count] ?? []
|
||||
d[$1.characters.count] = g + [$1]
|
||||
return d
|
||||
}
|
||||
let ordered: ([String], String) -> [String] = {
|
||||
guard String($1.characters.sort()) == $1 else { return $0 }
|
||||
return $0 + [$1]
|
||||
}
|
||||
|
||||
let groups = words
|
||||
.reduce([String](), combine: ordered)
|
||||
.reduce([Int: [String]](), combine: group)
|
||||
|
||||
guard
|
||||
let maxLength = groups.keys.maxElement(),
|
||||
let maxLengthGroup = groups[maxLength]
|
||||
else { exit(EXIT_FAILURE) }
|
||||
|
||||
maxLengthGroup.forEach { print($0) }
|
||||
Loading…
Add table
Add a link
Reference in a new issue