Anonymous user
Index finite lists of positive integers: Difference between revisions
Index finite lists of positive integers (view source)
Revision as of 22:53, 10 May 2017
, 7 years ago→{{header|Kotlin}}: Updated example see https://github.com/dkandalov/rosettacode-kotlin for details
(Added Kotlin) |
(→{{header|Kotlin}}: Updated example see https://github.com/dkandalov/rosettacode-kotlin for details) |
||
Line 356:
=={{header|Kotlin}}==
<lang scala>// version 1.1.
import java.math.BigInteger
Line 367:
fun unrank(r: BigInteger) = when (r) {
-BigInteger.ONE -> emptyList<Int>()
else -> r.toString(11).split('a').map { if (it != "") it.toInt() else 0 }
}
Line 382:
fun unrank2(r: BigInteger) = when (r) {
BigInteger.ZERO -> emptyList<Int>()
else -> r.toString(2).drop(1).split('1').map { it.length }
}
fun main(args: Array<String>) {
Line 395:
println("After unranking : $li")
println("\nAlternative approach (not suitable for large numbers)...\n")
li = li.dropLast(1)
println("Before ranking : $li")
Line 402:
li = unrank2(r)
println("After unranking : $li")
println()
for (i in 0..10) {
val bi = BigInteger.valueOf(i.toLong())
li = unrank2(bi)
println("${"%2d".format(i)} -> ${li.toString().padEnd(9)} -> ${rank2(li)}")
}
}</lang>
|