Home > Software design >  Need explanation of the short Kotlin solution for strings in Codewars
Need explanation of the short Kotlin solution for strings in Codewars

Time:07-28

I got a task on code wars.

The task is

In this simple Kata your task is to create a function that turns a string into a Mexican Wave. You will be passed a string and you must return that string in an array where an uppercase letter is a person standing up.

Rules are

  1. The input string will always be lower case but maybe empty.

  2. If the character in the string is whitespace then pass over it as if it was an empty seat

Example

wave("hello") => []string{"Hello", "hEllo", "heLlo", "helLo", "hellO"}

So I have found the solution but I want to understand the logic of it. Since its so minimalistic and looks cool but I don't understand what happens there. So the solution is

fun wave(str: String) = str.indices.map { str.take(it)   str.drop(it).capitalize() }.filter { it != str }

Could you please explain?

CodePudding user response:

str.indices just returns the valid indices of the string. This means the numbers from 0 to and including str.length - 1 - a total of str.length numbers.

Then, these numbers are mapped (in other words, transformed) into strings. We will now refer to each of these numbers as "it", as that is what it refers to in the map lambda.

Here's how we do the transformation: we first take the first it characters of str, then combine that with the last str.length - it characters of str, but with the first of those characters capitalized. How do we get the last str.length - it characters? We drop the first it characters.

Here's an example for when str is "hello", illustrated in a table:

it str.take(it) str.drop(it) str.drop(it).capitalize() Combined
0 hello Hello Hello
1 h ello Ello hEllo
2 he llo Llo heLLo
3 hel lo Lo helLo
4 hell o O hellO

Lastly, the solution also filters out transformed strings that are the same as str. This is to handle Rule #2. Transformed strings can only be the same as str if the capitalised character is a whitespace (because capitalising a whitespace character doesn't change it).

Side note: capitalize is deprecated. For other ways to capitalise the first character, see Is there a shorter replacement for Kotlin's deprecated String.capitalize() function?

  • Related