How to Reverse Letters in Kotlin

The challenge Given a string str, reverse it omitting all non-alphabetic characters. Examples: For str = "krishan", the output should be "nahsirk". For str = "ultr53o?n", the output should be "nortlu". Input/Output: [input] string str A string consists of lowercase Latin letters, digits, and symbols. [output] a string The solution in Kotlin Option 1: fun reverseLetter(str: String): String { return str.filter(Char::isLetter).reversed() } Option 2: fun reverseLetter(str: String) = str.reversed().filter{ it.isLetter() } Option 3:...

May 13, 2022 · 1 min · 113 words · Andrew

How to Get the ASCII Value of Character in Kotlin

The challenge Get the ASCII value of a character. The solution in Kotlin Option 1: val getAscii = Char::toInt Option 2: fun getAscii(c: Char) = c.code Option 3: fun getAscii(c: Char): Int { return c.code.toByte().toInt() } Test cases to validate our solution import kotlin.test.assertEquals import org.junit.Test class TestExample { @Test fun `Basic tests`() { assertEquals(65, getAscii('A')) assertEquals(32, getAscii(' ')) assertEquals(33, getAscii('!')) } }

May 12, 2022 · 1 min · 63 words · Andrew

How to use Coroutines in Kotlin

Coroutines are a way to handle multithreading in Kotlin, read more about it from the official docs: An example of a Coroutine in Kotlin val runBlocking = runBlocking { sleep(5) delay(1_000L) launch { delay(1_100L) note("inside launch") } note("outside launch") } Introducing the suspend keyword coroutineScopes need to be in wrapping suspend: suspend fun coroutineFunction(): String { return coroutineScope { launch { println("test") } "we return this string" } } runBlocking { println(coroutineFunction()) } Making Asynchronous API calls using Coroutines suspend fun call2Apis() { val call1 = async { delay(1_000L) println("got data from api1 @ ${Instant....

June 17, 2021 · 2 min · 220 words · Andrew

The Pair Type in Kotlin

val pair: Pair<String, Int> = "myKey" to 2 What is a Pair Type? A utility class when you want to return 2 values that are not related to one another. Some examples val (a, b) = Pair(1, "x") println(a) // 1 println(b) // x Alterantively, you could use: val p = Pair("x", "y") println(p) // (x, y) You can refer to it as first and second val p = Pair("x", "y") println(p....

June 16, 2021 · 2 min · 221 words · Andrew

Deconstruction in Kotlin

There are times when you need to extract an object into a number of variables. Let’s take the example below: val (id, name, position, department) = employee In Kotlin, this is known as a destructuring declaration. These new variables can now be used independently. The compilation thereof would look something like this, but is not required: val id = employee.component1() val name = employee.component2() val position = employee.component3() val department = employee....

June 11, 2021 · 1 min · 211 words · Andrew

What is a Unit in Kotlin?

Unit corresponds to the void keyword in Java. What is a Unit in Kotlin? If you have a function that does not return a value, you can return a Unit object. Generally, you would return a Unit in Kotlin where you would traditionally return a void in Java. fun doesntReturnAnythign(): Unit { val one = 1 }

June 9, 2021 · 1 min · 57 words · Andrew

How to declare a Function in Kotlin?

Functions are reusable pieces of code, often called Blocks, that as the name suggests, act as building blocks to piece together a program. Each language has its own keyword for defining a function block. What is fun? The fun keyword in Kotlin denotes a function, or method to wrap a reusable code block. fun prepends the name of a function as illustrated below. fun functionName(): String { return "A String" } The structure of a Kotlin function The function can take any number of parameters, and a return type....

June 8, 2021 · 1 min · 137 words · Andrew

The differences between Val and Var in Kotlin

Among all the many keywords that Kotlin comes with, there exists val and var. What are they used for? What are the differences, and why would you use one over another? They are both used to declare variables in Kotlin. But how are they different? What is val? val is used to declare a variable that is read-only, and who’s value does not change. Note that this only applies to the parent variable, and not it’s properties....

June 7, 2021 · 1 min · 177 words · Andrew

Build a pile of Cubes in Kotlin

The challenge Your task is to construct a building which will be a pile of n cubes. The cube at the bottom will have a volume of n^3, the cube above will have volume of (n-1)^3 and so on until the top which will have a volume of 1^3. You are given the total volume m of the building. Being given m can you find the number n of cubes you will have to build?...

March 30, 2021 · 2 min · 307 words · Andrew

Which x for that sum in Kotlin

The challenge Consider the sequence U(n, x) = x + 2x**2 + 3x**3 + .. + nx**n where x is a real number and n a positive integer. When n goes to infinity and x has a correct value (ie x is in its domain of convergence D), U(n, x) goes to a finite limit m depending on x. Usually given x we try to find m. Here we will try to find x (x real, 0 < x < 1) when m is given (m real, m > 0)....

March 29, 2021 · 2 min · 312 words · Andrew