I have talked to many Android developers, and most of them are excited about Kotlin. So am I. When I just started learning Kotlin, I was solving Kotlin Koans, and along with other great features, I was impressed with the power of functions for performing operations on collections. Since then, I spent three years writing Kotlin code but rarely utilised all the potential of the language.
During this year, I did more than a hundred coding problems on Leetcode in Java. I didn’t switch to Kotlin because I know the syntax of Java 6 so well, that I could effortlessly write code without autocompletion and syntax highlighting. But I didn’t keep track of new Java features, as Android support of Java SDK lagged many versions behind. I didn’t switch to Kotlin for solving problems right away.
Although I was writing Kotlin code for several years, I felt that I need to make an extra cognitive effort to get the syntax and the language constructions right. Solving algorithmic problems, especially under time pressure, is very different from Android app development. Still, the more I learned about Kotlin, the more I realised how many powerful features I’m missing, and how much boilerplate code I need to write.
One day, I have decided that I need to move on, so I started a new session in Leetcode and switched the compiler to Kotlin. I solved just a few easy problems, but I already feel that I have something to share.
Let’s start with loops. Let’s say, you have an
IntArray
of 10 elements
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
and you want to print
123456789
val array = intArrayOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
for(index in (1 until array.size)) {
print(array[index])
}
(1 until array.size)
is an
IntRange
a class that represents a range of values of type
Int
The first element in this range is
1
and the last one is
9
as we used
until
to exclude the last value. We don’t want to get
ArrayIndexOutOfBoundsException
right?
But what if we want to print all the elements of the array, except the element at index 5? Like this
012346789
Let’s get a bit more Kotliney then writing an
if
statement in the loop.
val array = intArrayOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
for(index in array.indices - 5) {
print(array[index])
}
array.indices
returns the range of valid indices for the array. In this case
array.indices
represent
IntRange
of
(0..9)
Making
(0..9) - 5
will result in
[0, 1, 2, 3, 4, 6, 7, 8, 9]
This is exactly what we need.
Kotlin also provides an ability to iterate from the greater number down to the smaller number using
downTo
The iteration step size can also be changed using
step
val array = intArrayOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
for(index in array.size - 1 downTo 1 step 2) {
print(array[index])
}
The code above with result in
97531
It’s problem number 1119 on Leetcode.
Given a string S, remove the vowels ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’ from it, and return the new string.
Even in Java there is a 1 line regex solution, but my intuition was the following:
1. Create a StringBuilder
2. Iterate over characters, and if the current character is not a vowel, append it to the StringBuilder
3. Return String from the StringBuilder
public String removeVowels(String S) {
StringBuilder sb = new StringBuilder();
for(char s: S.toCharArray()) {
if(s != 'a' && s != 'e' && s != 'i' && s !='o' && s != 'u') {
sb.append(s);
}
}
return sb.toString();
}
What about Kotlin? More idiomatic way is to use
filter()
or
filterNot()
fun removeVowels(S: String): String {
val vowels = setOf('a', 'e', 'i', 'o', 'u')
return S.filter { it !in vowels }
}
filter {predicate: (Char) -> Boolean}
returns a string containing only those characters from the original string that match the given predicate.
But instead of inverting
!in
let’s use
filterNot()
fun removeVowels(S: String): String {
val vowels = setOf('a', 'e', 'i', 'o', 'u')
return S.filterNot { it in vowels }
}
That was simple even for a beginner. Let’s move on to something a bit more sophisticated.
It’s another easy problem from Leetcode. Number 1480.
Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]). Return the running sum of nums.
Input: nums = [1,2,3,4]
Output: [1,3,6,10]
Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].
So we need to iterate over the array, adding the value at the current index to the running sum, and put it to the same index in the result array.
Is there something in Kotlin to help us with the running sum? Well, there’s different variations of
fold()
and
reduce()
operations.
Here’s a good explanation of those functions. But since Kotlin 1.4 there’s even more:
runningFold()
and
runningReduce()
As we want to start with the first element and return an array, it looks like
runningReduce()
is what we need. Let’s check its signature.
/**
* Returns a list containing successive accumulation values generated by applying [operation] from left to right
* to each element and current accumulator value that starts with the first element of this array.
*
* @param [operation] function that takes current accumulator value and an element, and calculates the next accumulator value.
*
* @sample samples.collections.Collections.Aggregates.runningReduce
*/
@SinceKotlin("1.4")
@kotlin.internal.InlineOnly
public inline fun IntArray.runningReduce(operation: (acc: Int, Int) -> Int): List<Int>
Sounds a bit too complex, but it will make sense when you’ll see an example.
fun runningSum(nums: IntArray): IntArray {
return nums.runningReduce { sum, element -> sum + element }.toIntArray()
}
This is the whole solution to the running sum problem using Kotlin
runningReduce()
function.
sum
starts with the first
element
in the array, element represens the current element. In lambda, we calculate the value of the next
sum
Oh… I guess my explanation isn’t making it more clear that a doc. Let’s just print out the values of the
sum
and the
element
at each step:
sum: 1; element: 2; sum + element: 3
sum: 3; element: 3; sum + element: 6
sum: 6; element: 4; sum + element: 10
sum: 10; element: 5; sum + element: 15
And the array we return is
[1, 3, 6, 10, 15]
There is no
sum + element: 1
I didn’t miss the line. The thing is that
runningReduce
as we see in the doc, takes the first value as the initial accumulator.
Unfortunately, Leetcode doesn’t support Kotlin 1.4 yet, so the code above might not compile.
#kotlin #programming #android #java #mobile-apps