forked from igorwojda/kotlin-coding-challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.kt
34 lines (28 loc) · 848 Bytes
/
solution.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.igorwojda.integer.printnumber.steps
private object Solution1 {
private fun printNumber(n: Int, step: Int = 1): List<Int> =
(n downTo 1 step step).toList()
}
private object Solution2 {
private fun printNumber(n: Int, step: Int = 1): List<Int> {
fun printNumberRec(n: Int): List<Int> =
when {
n <= 0 -> emptyList()
else -> listOf(n) + printNumberRec(n - step)
}
return printNumberRec(n)
}
}
private object Solution3 {
private fun printNumber(n: Int, step: Int = 1): List<Int> {
val list = mutableListOf<Int>()
if (n <= 0) {
return emptyList()
} else {
list.add(n)
}
list.addAll(printNumber(n - step, step))
return list
}
}
private object KtLintWillNotComplain