How to resolve the algorithm Index finite lists of positive integers step by step in the Kotlin programming language
How to resolve the algorithm Index finite lists of positive integers step by step in the Kotlin programming language
Table of Contents
Problem Statement
It is known that the set of finite lists of positive integers is countable.
This means that there exists a subset of natural integers which can be mapped to the set of finite lists of positive integers.
Implement such a mapping:
Demonstrate your solution by:
There are many ways to do this. Feel free to choose any one you like.
Make the rank function as a bijection and show unrank(n) for n varying from 0 to 10.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Index finite lists of positive integers step by step in the Kotlin programming language
The code provided defines two methods to encode and decode integers. These methods, rank
and unrank
, and rank2
and unrank2
, encode an integer list to a big integer and decode a big integer to a list of integers using different strategies.
The rank
function takes a list of integers as input and returns a big integer that is the encoded version of the list. The encoding is done by joining the integers in the list with an 'a' and then encoding the resulting string in base 11. If the list is empty, the big integer '-1' is returned.
The unrank
function takes a big integer as input and returns a list of integers that is the decoded version of the big integer. The decoding is done by first converting the big integer to a string in base 11 and then splitting the string by the 'a' character. The resulting list of strings is then converted to integers. If the big integer is '-1', an empty list is returned.
The rank2
function takes a list of integers as input and returns a big integer that is the encoded version of the list. The encoding is done by mapping each integer in the list to a string of '1' followed by n '0's.
The unrank2
function takes a big integer as input and returns a list of integers that is the decoded version of the big integer. The decoding is done by first converting the big integer to a string in base 2 and then dropping the first character. The resulting string is then split by the '1' character. The lengths of the resulting list of strings are then converted to integers.
The main function of the program tests the rank
, unrank
, rank2
and unrank2
functions. It first creates a list of integers, ranks it using the rank
function, unranks the resulting big integer using the unrank
function, and prints the results. It then does the same thing using the rank2
and unrank2
functions.
Finally, the main function prints a table of the values of the rank2
function for the integers from 0 to 10.
Source code in the kotlin programming language
// version 1.1.2
import java.math.BigInteger
/* Separates each integer in the list with an 'a' then encodes in base 11. Empty list mapped to '-1' */
fun rank(li: List<Int>) = when (li.size) {
0 -> -BigInteger.ONE
else -> BigInteger(li.joinToString("a"), 11)
}
fun unrank(r: BigInteger) = when (r) {
-BigInteger.ONE -> emptyList<Int>()
else -> r.toString(11).split('a').map { if (it != "") it.toInt() else 0 }
}
/* Each integer n in the list mapped to '1' plus n '0's. Empty list mapped to '0' */
fun rank2(li:List<Int>): BigInteger {
if (li.isEmpty()) return BigInteger.ZERO
val sb = StringBuilder()
for (i in li) sb.append("1" + "0".repeat(i))
return BigInteger(sb.toString(), 2)
}
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>) {
var li: List<Int>
var r: BigInteger
li = listOf(0, 1, 2, 3, 10, 100, 987654321)
println("Before ranking : $li")
r = rank(li)
println("Rank = $r")
li = unrank(r)
println("After unranking : $li")
println("\nAlternative approach (not suitable for large numbers)...\n")
li = li.dropLast(1)
println("Before ranking : $li")
r = rank2(li)
println("Rank = $r")
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)}")
}
}
You may also check:How to resolve the algorithm Greatest common divisor step by step in the Bc programming language
You may also check:How to resolve the algorithm Permutations by swapping step by step in the Nim programming language
You may also check:How to resolve the algorithm Ackermann function step by step in the ATS programming language
You may also check:How to resolve the algorithm Doubly-linked list/Element insertion step by step in the PL/I programming language
You may also check:How to resolve the algorithm Chat server step by step in the Icon and Unicon programming language