How to resolve the algorithm Index finite lists of positive integers step by step in the Sidef programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Index finite lists of positive integers step by step in the Sidef 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 Sidef programming language
Source code in the sidef programming language
func rank(Array arr) {
Number(arr.join('a'), 11)
}
func unrank(Number n) {
n.base(11).split('a').map { Num(_) }
}
var l = [1, 2, 3, 10, 100, 987654321]
say l
var n = rank(l)
say n
var l = unrank(n)
say l
func unrank(Number n) {
n == 1 ? [0]
: n.base(2).substr(1).split('0', -1).map{.len}
}
func rank(Array x) {
x.is_empty ? 0
: Number('1' + x.map { '1' * _ }.join('0'), 2)
}
for x in (0..10) {
printf("%3d : %-18s: %d\n", x, unrank(x), rank(unrank(x)))
}
say ''
var x = [1, 2, 3, 5, 8]
say "#{x} => #{rank(x)} => #{unrank(rank(x))}"
You may also check:How to resolve the algorithm Dice game probabilities step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm Fibonacci sequence step by step in the ECL programming language
You may also check:How to resolve the algorithm String interpolation (included) step by step in the Ursala programming language
You may also check:How to resolve the algorithm Roots of unity step by step in the RPL programming language
You may also check:How to resolve the algorithm Flow-control structures step by step in the Forth programming language