Last active
August 29, 2015 14:14
-
-
Save nathanborror/29d2a6d7a634593b9edb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
let words = ["Cat", "Chicken", "Fish", "Dog", "Mouse", "Pig", "Monkey"] | |
typealias Entry = (Character, [String]) | |
func distinct<T: Equatable>(source: [T]) -> [T] { | |
var unique = [T]() | |
for item in source { | |
if !contains(unique, item) { | |
unique.append(item) | |
} | |
} | |
return unique | |
} | |
func buildIndex(words: [String]) -> [Entry] { | |
let letters = words.map { (word) -> Character in | |
Character(word.substringToIndex(advance(word.startIndex, 1)).uppercaseString) | |
} | |
let distinctLetters = distinct(letters) | |
return distinctLetters.map { (letter) -> Entry in | |
return (letter, words.filter { (word) -> Bool in | |
Character(word.substringToIndex(advance(word.startIndex, 1)).uppercaseString) == letter | |
}) | |
} | |
} | |
println(buildIndex(words)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
let words = ["Cat", "Chicken", "Fish", "Dog", "Mouse", "Pig", "Monkey"] | |
typealias Entry = (Character, [String]) | |
func distinct<T: Equatable>(source: [T]) -> [T] { | |
var unique = [T]() | |
for item in source { | |
if !contains(unique, item) { | |
unique.append(item) | |
} | |
} | |
return unique | |
} | |
func buildIndex(words: [String]) -> [Entry] { | |
func firstLetter(str: String) -> Character { | |
return Character(str.substringToIndex(advance(str.startIndex, 1)).uppercaseString) | |
} | |
return distinct(words.map(firstLetter)).map { (letter) -> Entry in | |
return (letter, words.filter { (word) -> Bool in | |
firstLetter(word) == letter | |
}) | |
} | |
} | |
println(buildIndex(words)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
let words = ["Cat", "Chicken", "Fish", "Dog", "Mouse", "Pig", "Monkey"] | |
typealias Entry = (Character, [String]) | |
func buildIndex(words: [String]) -> [Entry] { | |
var result = [Entry]() | |
var letters = [Character]() | |
for word in words { | |
let firstLetter = Character(word.substringToIndex(advance(word.startIndex, 1)).uppercaseString) | |
if !contains(letters, firstLetter) { | |
letters.append(firstLetter) | |
} | |
} | |
for letter in letters { | |
var wordsForLetter = [String]() | |
for word in words { | |
let firstLetter = Character(word.substringToIndex(advance(word.startIndex, 1)).uppercaseString) | |
if firstLetter == letter { | |
wordsForLetter.append(word) | |
} | |
} | |
result.append((letter, wordsForLetter)) | |
} | |
return result | |
} | |
println(buildIndex(words)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment