Answer the question
In order to leave comments, you need to log in
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'} }!!
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question