Created
April 5, 2020 03:04
-
-
Save theevo/e06a82acfd5e75920de15fd2ec52f3b0 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
/*: | |
# Tuesday Stretch Problem 5.2 | |
## Longest Word In String | |
### Instructions | |
- Write a function that takes a string and returns the biggest word in that string. | |
- Make sure to remove punctuation and whitespace. | |
``` | |
longestWord("This string, has a gigantic! word in it...") // returns "gigantic" | |
longestWord("one, two, three") // returns "three" | |
``` | |
- Note: Look up `NSCharacterSet` methods to remove whitespace and punctuation. Create the charSets as `NSMutableCharset` objects so you can combine the two CharSets. Call the `componentsSeperatedByCharacterInSet` method on the parameter string to get an array of strings after separating them by the charSets. Loop through the array to check against your return string length. | |
### Black Diamond 💎💎💎 | |
- Do this in an Objective-C project. | |
*/ | |
import Foundation | |
extension Dictionary { | |
public init(keys: [Key], values: [Value]) { | |
precondition(keys.count == values.count) | |
self.init() | |
for (index, key) in keys.enumerated() { | |
self[key] = values[index] | |
} | |
} | |
} | |
extension String { | |
var lettersOnly: String { | |
return self.components(separatedBy: CharacterSet.letters.inverted).joined() | |
} | |
} | |
func longestWord(in string: String) -> String { | |
// Break the string into words | |
let words = string.components(separatedBy: .whitespacesAndNewlines) | |
print(words) | |
// Sanitize the words of punctuation | |
let cleanWords = words.map { $0.lettersOnly } | |
// Map those words into letter counts | |
let letterCounts = cleanWords.compactMap{ $0.count } //{ $0.starts(with: "#") | |
// insert those things into Dictionary to have key-value pairs | |
let wordDictionary: [String: Int] = Dictionary(keys: cleanWords, values: letterCounts) | |
print(wordDictionary) | |
let longestWord = wordDictionary.max { a, b in a.value < b.value } | |
// Return the word with the largest count | |
return longestWord?.key ?? "sorry, couldn't find longest word. blame the programmer." | |
} | |
print(longestWord(in: "one, two, three")) // "three" | |
print(longestWord(in: "This string, has a gigantic! word in it...")) // "gigantic" | |
print(longestWord(in: "Don't trip and fall")) // could be any of the 4 letter words | |
print(longestWord(in: "Hello there! Come here my little friend. Don't be afraid. Don't worry, he'll be all right. What happened? Rest easy, son, you've had a busy day. You're fortunate you're still in one piece. Ben? Ben Kenobi! Boy, am I glad to see you! The Jundland wastes are not to be traveled lightly.")) // fortunate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment