Print the Words

Given a blob of text you will have to determine the following statistics about it: The longest wor…

Click here to read the complete problem statement.


If you need help solving this problem, mention your approach and ask specific questions. Please avoid sharing your code and asking the Community to figure out “what’s wrong”.

I solved this on Python but for some reason can’t solve it in Kotlin? Can anyone help me? @hjr265 kotlin expert for example.

fun main() {
    val words = Regex("\\w+").findAll(readLine()!!).map { it.value }.toList()

    var minW = words.first()
    var maxW = words.first()
    var maxFreqW = words.first()
    var freq = mutableMapOf<String, Int>()

    for (w in words) {
        if (w.length < minW.length) minW = w
        if (w.length > maxW.length) maxW = w

        val lc = w.toLowerCase()
        val f = (freq[lc] ?: 0) + 1
        freq.put(lc, f)
        if (f > freq[maxFreqW.toLowerCase()]!!)
            maxFreqW = w
    }

    println(maxW)
    println(minW)
    println(maxFreqW)
}

Ok solved in another submission, but still not sure why this failed.