Index finite lists of positive integers: Difference between revisions

(→‎{{header|jq}}: simplify)
 
Line 568:
 
=={{header|jq}}==
'''Works with gojq'''
 
'''Works with jq''' within the limits of jq's support for large integer arithmetic
Line 577:
Fibonacci encoding of positive integers (see
e.g. https://en.wikipedia.org/wiki/Fibonacci_coding and
[[:Category:jq/fibonacci.jq]]). This is the focus of the first
subsection. The second subsection focuses on the "n 0s" encoding.
 
The CGo implementation of jq, as of version 1.6, supports arbitrarilyindefinitely large literal integers, and
so, apart from machine limitations, the programs shown here should
work using gojq without further qualification.
 
The C implementation of jq, as of version 1.6, supports arbitrarily
large literal integers, and the `tonumber` filter retains precision
the `tonumber` filter retains precision allowing seamless translation between strings and numbers.
 
The following is slightly more verbose than it need be but for the sake of compatibility with jaq. Also note that trivial
sake of compatibility with jaq. Also note that trivial changes would
changes would be required if using jaq as jaq does not (as of this writing in 2024) support
support the `include` or `module` directives.
 
===Map based on Fibonacci encoding===
 
Since each Fibonacci-encoded integer ends with "11" and
Line 583 ⟶ 599:
the original sequence of integers can trivially be recovered after
simple concatenation of the encodings. However, the Fibonacci
encoding of an integer can begin with 0s, so here we simply reverseprefix
the bitbinary string andwith remove the leadinga "1" to produce an ordinary integer.
 
For example: 1 2 3 => 11 011 0011 => 110110011 => 110011011 => 10011011
 
The Go implementation of jq supports indefinitely large integers and so, apart from machine limitations, the programs shown here should work without further qualification using gojq.
 
For example: 1 2 3 => 11 011 0011 => 110110011 => 110011011 => 100110111110110011
The C implementation of jq, as of version 1.6, supports arbitrarily large literal integers, and
the `tonumber` filter retains precision allowing seamless translation between strings and numbers.
For this reason, in the Fibonacci-based approach, the encoded bitstring is not converted to a decimal number.
 
In the following, we will simply interpret
The following is slightly more verbose than it need be but for the sake of compatibility with jaq. Also note that trivial
this as an integer in base 2 to avoid unnecessary complications
changes would be required if using jaq as jaq does not (as of this writing in 2024) support
arising from implementation-specific limits.
the `include` or `module` directives.
===Map based on Fibonacci encoding===
<syntaxhighlight lang="jq">
include "fibonacci" {search: "./"}; # see https://rosettacode.org/wiki/Category:Jq/fibonacci.jq
 
# Input: an array of integers
Line 605 ⟶ 614:
def rank:
map(fibencode | map(tostring) | join(""))
| "1" + join("");
| explode | reverse[1:] | implode ;
 
# Input a bitstring or 0-1 integer interpreted as a bitstring
# Output: an array of integers
def unrank:
"1" + tostring
| .[1:]
| explode | reverse | implode
| split("11")
| .[:-1]
Line 624 ⟶ 634:
end;
 
### The task
# Encode and decode a random number of distinct positive numbers chosen at random.
# Produce a JSON object showing the set of numbers, their encoding, and
Line 643 ⟶ 654:
'''Invocation''':
<pre>
< /dev/random tr -cd '0-9' | fold -w 1 | jq -nrf index-finite-lists-of-positive-integers.jq
</pre>
{{output}}
Line 649 ⟶ 660:
{
"numbers": [
3270092408,
8345042641,
8780235563,
6870517028,
91654,49093
29271,
36764,
36213,
38347,
37761,
50544
],
"encoded": 101000101000001010101000111000001010001000100101100010101010000000010011001001001001000101011000101010100000010000011,
"encoded": 10000010101010101010000110010010100010100100011100101000010100010100111001000010010010000100110010001001010010101011100000001000000000010111000101001010010101000101101001000010001001000001100010001000100000000001110000100100000010001000011000010101001000001001,
"check": true
}
</pre>
 
 
===Bijective map based on "n 0s" encoding===
2,468

edits