Index finite lists of positive integers: Difference between revisions

(→‎{{header|jq}}: simplify)
 
(8 intermediate revisions by the same user not shown)
Line 568:
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq'''
 
'''Works with jq''' within the limits of jq's support for large integer arithmetic
 
'''Works with jaq within the limits of jaq's support for large integers'''
Line 576 ⟶ 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 Go implementation of jq and,supports asindefinitely oflarge versionintegers 1.6, the Cand
so, apart from machine limitations, the programs shown here should all work
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 allowing seamless
allowing seamless translation between strings and numbers. Thus, apart from
 
The following is slightly more verbose than it need be but for the
the 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.
 
===Fibonacci-Map based on Fibonacci encoding===
 
Since each Fibonacci-encoded integer ends with "11" and
Line 582 ⟶ 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 towith producea an ordinary integer"1".
 
For example: 1 2 3 => 11 011 0011 => 110110011 => 1100110111110110011
 
In the following, we will simply interpret
The Go implementation of jq and, as of version 1.6, the C
this as an integer in base 2 to avoid unnecessary complications
implementation both support arbitrarily large literal integers, and
arising from implementation-specific limits.
the `tonumber` filter retains precision allowing seamless
translation between strings and numbers. Thus, apart from
machine limitations, the programs shown here should all work
for arbitrary positive integers.
 
The following is slightly more verbose than it need be but for
the sake of compatibility with jaq. Also note that trivial
changes would be required if using jaq as jaq does not (as of this writing in 2024) support
the `include` or `module` directives.
===Fibonacci-based encoding===
<syntaxhighlight lang="jq">
include "fibonacci" {search: "./"}; # see https://rosettacode.org/wiki/Category:Jq/fibonacci.jq
 
# Input: an array of integers
Line 606 ⟶ 614:
def rank:
map(fibencode | map(tostring) | join(""))
| "1" + join("");
| explode | reverse | implode ;
 
# Input a bitstring or 0-1 integer interpreted as a bitstring
# Output: an array of integers
def unrank:
tostring | explode | reverse | implode
| .[1:]
| 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": 110000010101010101010000110010010100010100100011100101000010100010100111001000010010010000100110010001001010010101011100000001000000000010111000101001010010101000101101001000010001001000001100010001000100000000001110000100100000010001000011000010101001000001001,
"check": true
}
</pre>
 
 
===Bijective map based on "n 1s0s" encoding===
<syntaxhighlight lang="jq">
### Infrastructure
2,442

edits