Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
|
|
@ -0,0 +1,16 @@
|
|||
func lis(a) {
|
||||
var l = a.len.of { [] }
|
||||
l[0] << a[0]
|
||||
for i in (1..a.end) {
|
||||
for j in ^i {
|
||||
if ((a[j] < a[i]) && (l[i].len < l[j].len+1)) {
|
||||
l[i] = [l[j]...]
|
||||
}
|
||||
}
|
||||
l[i] << a[i]
|
||||
}
|
||||
l.max_by { .len }
|
||||
}
|
||||
|
||||
say lis(%i<3 2 6 4 5 1>)
|
||||
say lis(%i<0 8 4 12 2 10 6 14 1 9 5 13 3 11 7 15>)
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
func lis(deck) {
|
||||
var pileTops = []
|
||||
deck.each { |x|
|
||||
var low = 0;
|
||||
var high = pileTops.end
|
||||
while (low <= high) {
|
||||
var mid = ((low + high) // 2)
|
||||
if (pileTops[mid]{:val} >= x) {
|
||||
high = mid-1
|
||||
} else {
|
||||
low = mid+1
|
||||
}
|
||||
}
|
||||
var i = low
|
||||
var node = Hash(val => x)
|
||||
node{:back} = pileTops[i-1] if (i != 0)
|
||||
pileTops[i] = node
|
||||
}
|
||||
var result = []
|
||||
for (var node = pileTops[-1]; node; node = node{:back}) {
|
||||
result << node{:val}
|
||||
}
|
||||
result.reverse
|
||||
}
|
||||
|
||||
say lis(%i<3 2 6 4 5 1>)
|
||||
say lis(%i<0 8 4 12 2 10 6 14 1 9 5 13 3 11 7 15>)
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
def until(cond; update):
|
||||
def _until:
|
||||
if cond then . else (update | _until) end;
|
||||
try _until catch if .== "break" then empty else . end;
|
||||
|
||||
# binary search for insertion point
|
||||
def bsearch(target):
|
||||
. as $in
|
||||
| [0, length-1] # [low, high]
|
||||
| until(.[0] > .[1];
|
||||
.[0] as $low | .[1] as $high
|
||||
| ($low + ($high - $low) / 2 | floor) as $mid
|
||||
| if $in[$mid] >= target
|
||||
then .[1] = $mid - 1
|
||||
else .[0] = $mid + 1
|
||||
end )
|
||||
| .[0];
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
def lis:
|
||||
|
||||
# Helper function:
|
||||
# given a stream, produce an array of the items in reverse order:
|
||||
def reverse(stream): reduce stream as $i ([]; [$i] + .);
|
||||
|
||||
# put the items into increasing piles using the structure:
|
||||
# NODE = {"val": value, "back": NODE}
|
||||
reduce .[] as $x
|
||||
( []; # array of NODE
|
||||
# binary search for the appropriate pile
|
||||
(map(.val) | bsearch($x)) as $i
|
||||
| setpath([$i];
|
||||
{"val": $x,
|
||||
"back": (if $i > 0 then .[$i-1] else null end) })
|
||||
)
|
||||
| .[length - 1]
|
||||
| reverse( recurse(.back) | .val ) ;
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
( [3,2,6,4,5,1],
|
||||
[0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15]
|
||||
) | lis
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
$ jq -c -n -f lis.jq
|
||||
[2,4,5]
|
||||
[0,2,6,9,11,15]
|
||||
Loading…
Add table
Add a link
Reference in a new issue