Skip to content

Instantly share code, notes, and snippets.

@jonbullock
Created March 11, 2017 11:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonbullock/97e275220359afe3d8ebf67dcbecb192 to your computer and use it in GitHub Desktop.
Save jonbullock/97e275220359afe3d8ebf67dcbecb192 to your computer and use it in GitHub Desktop.
URL to fetch = https://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=json&titles=Java_virtual_machine
Largest word=Is_there_any_protection_against_Java_exploits
Java=132
import java.util.*
import kotlin.comparisons.compareBy
import kotlin.comparisons.thenBy
fun main(args: Array<String>) {
if (args.size == 0) {
println("Provide a Wikipedia page to fetch")
return
}
val apiEndpoint = "https://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=json&titles=" + args[0]
println("URL to fetch = ${apiEndpoint}")
val pageContents = java.net.URL(apiEndpoint).readText()
val words = Regex("(\\w+)").findAll(pageContents)
val largestWord = (words.maxBy{ it.value.length })?.value
println("Largest word=${largestWord}")
val occurrences = HashMap<String, Int>()
words.forEach {
if (occurrences.containsKey(it.value)) {
var count = occurrences.get(it.value)
occurrences.put(it.value, count!!.inc())
} else {
occurrences.put(it.value, 1)
}
}
// sort map based on number of occurances
val sortedOccurrences = occurrences.toSortedMap(compareBy<String> { it.length }.thenBy { it })
// filter out words of 1 char (i.e. n or e) and 'the'
val filtedOccurrences = sortedOccurrences.filterKeys { it.length > 1 }.filterKeys { !it.equals("the") }
println(filtedOccurrences.maxBy { it.value })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment