Created
December 30, 2016 03:28
-
-
Save davenportw15/374e1c3b653fa06801a8913ad935432d 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 java.io.File | |
val dictionary = "/usr/share/dict/words" | |
val minimumLength = 5 | |
fun main(args: Array<String>) { | |
val words = File(dictionary).readLines() | |
val output = File("/Users/william/Desktop/words.txt").bufferedWriter() | |
words.forEach { word -> | |
val wordsThatContainWord = wordsContaingWord(words, word) | |
if (wordsThatContainWord.isNotEmpty()) { | |
val formattedWords = wordsThatContainWord.joinToString(", ") | |
output.write("${word}: ${formattedWords}\n") | |
} | |
} | |
} | |
fun wordsContaingWord(words: List<String>, word: String): List<String> { | |
val wordLowerCase = word.toLowerCase() | |
return words.filter { wordInDictionary -> | |
val wordInDictionaryLowerCase = wordInDictionary.toLowerCase() | |
wordInDictionaryLowerCase.contains(wordLowerCase) | |
&& wordInDictionaryLowerCase != wordLowerCase | |
&& wordLowerCase.count() >= minimumLength | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment