Puzzlers on Kotlin Academy, week 1 - challenge your brain and Kotlin programming skills
Article originally published at blog.kotlin-academy.com
In last week, we’ve started publishing puzzlers on Kotlin Academy portal. Now we already have 3 of them. This is why we can show them here for you.
We will start from absolute classic of Kotlin puzzlers. Next we will check out more basic knowledge, and finally we will have original and more advanced puzzler that might surprise you. Have fun
Scala-like functions
fun hello() = {
println("Hello, World")
}
hello()
What will it display? Some possibilities:
a. Does not compile
b. Prints “Hello, World”
c. Nothing
d. Something else
Answer is not so simple, but to see it, you need to check out this link or end of this article, since we don’t want to spoil it.
Indent trimming
val world = "multiline world"
println("""
Hello
\$world
""".trimIndent()
What does it display? Some possibilities:
a)
Hello
$world
b)
Hello
$world
c)
Hello
\multiline world
d) Doesn’t compile
Author: Dmitry Kandalov
If you know trimIndent
and triple quoted strings, then the answer should be clear. Until you don’t know it well enough If you are not sure, check out answer here of at the end of this article.
If-else chaining
fun printNumberSign(num: Int) {
if (num < 0) {
"negative"
} else if (num > 0) {
"positive"
} else {
"zero"
}.let { print(it) }
}
printNumberSign(-2)
print(",")
printNumberSign(0)
print(",")
printNumberSign(2)
What does it display? Some possibilities:
a) negative,zero,positive
b) negative,zero,
c) negative,,positive
d) ,zero,positive
Author: Kevin Most
This one is tricky, so think twice. When you are ready, check answer here or below.
Answers and explanations
For “Scala-like functions” correct answer is:
c. Nothing
Why? Here is an explanation:
hello is a function that returns function created using lambda expression. It’s just returning function. To print text, we need to invoke it: hello()() // Prints: Hello, World
For “Indent trimming” correct answer is:
c)
Hello
\multiline world
Why? Here is an explanation:
A raw string is delimited by a triple quote (
”””
), contains no escaping and can contain newlines and any other characters. Although dollar cannot be used there even with escape character . If we want to use $, then we need to use {‘’} instead
For “If-else chaining” correct answer is:
d) ,zero,positive
Why? Here is an explanation:
Remember that the
else if
construct is really just callingelse
with a “single-line”if
, which is the rest of your expression. In Kotlin, functions are resolved before if-else blocks, so the.let { print(it) }
is only applying to the finalelse if
. So in this case, the result of the firstif
statement will not be used and the function will return immediately. To avoid this situation, you can wrap the entireif … else …
in parenthesis, and then chain.let
on it
Do you want more puzzlers in the future? Track Kotlin Academy portal or subscribe to our mailing list.
To be up-to-date with great news on Kotlin Academy, subscribe to the newsletter and observe Twitter.