Kt. Academy Logo
WorkshopsBooksArticlesStore

Learn by doing

Train new Kotlin skills by solving practical challenges

What are the challenges?

Small programming tasks that require you to implement a function or a class in Kotlin. Thanks to them, you can check in practice if you understand the material correctly and train it to be able to use it immediately after the workshop. All challenges are verified by unit tests.

Exemplary challenges

// Fibonacci number that starts from 1 and 1 (fib(0) == 1, fib(1) == 1, fib(2) == 2, fib(3) == 3, fib(4) == 5, fib(5) == 8) // https://en.wikipedia.org/wiki/Fibonacci_number fun fib(n: Int): Int = TODO() fun main() { fib(0) shouldEquals 1 fib(1) shouldEquals 1 fib(2) shouldEquals 2 fib(3) shouldEquals 3 fib(4) shouldEquals 5 fib(5) shouldEquals 8 fib(6) shouldEquals 13 fib(7) shouldEquals 21 fib(8) shouldEquals 34 fib(9) shouldEquals 55 fib(10) shouldEquals 89 } infix fun <T> T.shouldEquals(other: T) { if (this != other) throw AssertionError("Value $this not equal to $other") }
interface Person { val name: String val age: Int val canBuyAlcohol: Boolean fun helloText(): String fun cheerText(person: Person): String } // TODO: Implement classes Businessman and Student // * They both should implement Person // * They both can buy alcohol only if over 21 // * Businessman says hello by “Good morning”, Student by “Hi”. // * Businessman cheers by “Hello, my name is {his name}, nice to see you {cheered person name}”, Student by “Hey {cheered person name}, I am {his name}”. fun main() { val businessman: Person = TODO("Use Businessman constructor here once it is implemented") val student: Person = TODO("Use Businessman constructor here once it is implemented") println(businessman.helloText()) println(student.helloText()) println(businessman.cheerText(student)) println(student.cheerText(businessman)) fun sayIfCanBuyAlcohol(person: Person) { val modal = if(person.canBuyAlcohol) "can" else "can't" println("${person.name} $modal buy alcohol") } sayIfCanBuyAlcohol(businessman) sayIfCanBuyAlcohol(student) }