Skip to content

Instantly share code, notes, and snippets.

@davenportw15
Created December 30, 2016 03:28
Show Gist options
  • Save davenportw15/374e1c3b653fa06801a8913ad935432d to your computer and use it in GitHub Desktop.
Save davenportw15/374e1c3b653fa06801a8913ad935432d to your computer and use it in GitHub Desktop.
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