J
J
JhonnyF2021-08-30 00:44:08
Kotlin
JhonnyF, 2021-08-30 00:44:08

How does the method work?

Good day!
Explain, please, on the fingers, how this method works?
The fact is that I solved a problem on CodeWars, according to the condition of which a string is given, from which it is necessary to isolate the word, the weight of which is the largest.
The weight is calculated based on the sum of the letters in its composition; for letters, it depends on their number in the alphabet, i.e. a - 1, b - 2, etc., then if we have aa - this is 2 points, ab - 3, etc.
I ended up with a sheet of 40 lines, but in the solutions there is such a compact code example, and I do not understand how it works:

fun high(str: String): String {
        return str.split(' ').maxByOrNull{ it.sumBy{ it - 'a'} }!!
    }


Can you please explain in words how { it.sumBy{ it - 'a'} returns the most significant word?
I climbed into the Kotlin docks, the method itself is simply described there, but not this wonderful lambda.
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2021-08-30
@vabka

Subtracts from each character in the word 'a' and sums it.
Thus, the letters will have such "weights":
a- 0
b - 1
c - 2
and so on

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question