Inventory sequence: Difference between revisions

Line 27:
 
 
 
=={{header|jq}}==
'''Works with both jq and gojq, the C and Go implementations of jq'''
 
... but note that gojq takes about 5 times longer (and requires much more memory) to complete the task.
 
The definition of `_nwise` can be omitted if using the C implementation of jq.
<syntaxhighlight lang=jq>
def _nwise($n):
def n: if length <= $n then . else .[0:$n] , (.[$n:] | n) end;
n;
 
# Emit the inventory sequence ad infinitum
def inventory_sequence:
{num: 0,
emit: 0,
inventory: {} }
| foreach range(0; infinite) as $n (.;
.emit = (.inventory[.num|tostring] // 0)
| if .emit == 0 then .num = 0 else .num += 1 end
| .inventory[.emit|tostring] += 1 )
| .emit ;
 
# Report on the progress of an arbitrary sequence, indefinitely
# Emit [.next, $x, .n]
def probe(s; $gap):
foreach s as $x ({n: 0, next: $gap};
.n += 1
| if $x >= .next then .emit = {next, $x, n} | .next += $gap
else .emit = null
end)
| select(.emit).emit;
 
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
def task($n):
[limit($n; inventory_sequence)] | _nwise(10) | map(lpad(3)) | join(" ");
 
task(100),
"",
(limit(10; probe(inventory_sequence; 1000))
| "First element >= \(.next) is \(.x) at index \(.n - 1)")
</syntaxhighlight>
{{output}}
<pre>
0 1 1 0 2 2 2 0 3 2
4 1 1 0 4 4 4 1 4 0
5 5 4 1 6 2 1 0 6 7
5 1 6 3 3 1 0 7 9 5
3 6 4 4 2 0 8 9 6 4
9 4 5 2 1 3 0 9 10 7
5 10 6 6 3 1 4 2 0 10
11 8 6 11 6 9 3 2 5 3
2 0 11 11 10 8 11 7 9 4
3 6 4 5 0 12 11 10 9 13
 
First element >= 1000 is 1001 at index 24255
First element >= 2000 is 2009 at index 43301
First element >= 3000 is 3001 at index 61708
First element >= 4000 is 4003 at index 81456
First element >= 5000 is 5021 at index 98704
First element >= 6000 is 6009 at index 121342
First element >= 7000 is 7035 at index 151756
First element >= 8000 is 8036 at index 168804
First element >= 9000 is 9014 at index 184428
First element >= 10000 is 10007 at index 201788
</pre>
 
=={{header|Python}}==
Line 84 ⟶ 151:
</pre>
[[File:Inventory Sequence.png|500px|thumb|center]]
 
 
=={{header|Raku}}==
2,496

edits