How to resolve the algorithm Index finite lists of positive integers step by step in the Arturo 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 Arturo 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 Arturo programming language
Source code in the arturo programming language
rank: function [arr][
if empty? arr -> return 0
from.binary "1" ++ join.with:"0" map arr 'a -> repeat "1" a
]
unrank: function [rnk][
if rnk=1 -> return [0]
bn: as.binary rnk
map split.by:"0" slice bn 1 dec size bn => size
]
l: [1, 2, 3, 5, 8]
print ["The initial list:" l]
r: rank l
print ["Ranked:" r]
u: unrank r
print ["Unranked:" u]
You may also check:How to resolve the algorithm Bulls and cows step by step in the Euphoria programming language
You may also check:How to resolve the algorithm Euler method step by step in the МК-61/52 programming language
You may also check:How to resolve the algorithm Cholesky decomposition step by step in the OCaml programming language
You may also check:How to resolve the algorithm Terminal control/Positional read step by step in the Racket programming language
You may also check:How to resolve the algorithm Multiplicative order step by step in the Java programming language