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,17 @@
|
|||
;; This efficient sort method uses the list library for permutations
|
||||
|
||||
(lib 'list)
|
||||
(define (in-order L)
|
||||
(cond
|
||||
((empty? L) #t)
|
||||
((empty? (rest L)) #t)
|
||||
(else (and ( < (first L) (second L)) (in-order (rest L))))))
|
||||
|
||||
(define L (shuffle (iota 6)))
|
||||
→ (1 5 4 2 0 3)
|
||||
|
||||
(for ((p (in-permutations (length L ))))
|
||||
#:when (in-order (list-permute L p))
|
||||
(writeln (list-permute L p)) #:break #t)
|
||||
|
||||
→ (0 1 2 3 4 5)
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
iterator permutations[T](ys: openarray[T]): seq[T] =
|
||||
var
|
||||
d = 1
|
||||
c = newSeq[int](ys.len)
|
||||
xs = newSeq[T](ys.len)
|
||||
|
||||
for i, y in ys: xs[i] = y
|
||||
yield xs
|
||||
|
||||
block outter:
|
||||
while true:
|
||||
while d > 1:
|
||||
dec d
|
||||
c[d] = 0
|
||||
while c[d] >= d:
|
||||
inc d
|
||||
if d >= ys.len: break outter
|
||||
|
||||
let i = if (d and 1) == 1: c[d] else: 0
|
||||
swap xs[i], xs[d]
|
||||
yield xs
|
||||
inc c[d]
|
||||
|
||||
proc isSorted[T](s: openarray[T]): bool =
|
||||
var last = low(T)
|
||||
for c in s:
|
||||
if c < last:
|
||||
return false
|
||||
last = c
|
||||
return true
|
||||
|
||||
proc permSort[T](a: openarray[T]): seq[T] =
|
||||
for p in a.permutations:
|
||||
if p.isSorted:
|
||||
return p
|
||||
|
||||
var a = @[4, 65, 2, -31, 0, 99, 2, 83, 782]
|
||||
echo a.permSort
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
function inOrder(sequence s)
|
||||
for i=2 to length(s) do
|
||||
if s[i]<s[i-1] then return 0 end if
|
||||
end for
|
||||
return 1
|
||||
end function
|
||||
|
||||
function permutationSort(sequence s)
|
||||
for n=1 to factorial(length(s)) do
|
||||
sequence perm = permute(n,s)
|
||||
if inOrder(perm) then return perm end if
|
||||
end for
|
||||
?9/0 -- should never happen
|
||||
end function
|
||||
|
||||
?permutationSort({"dog",0,15.545,{"cat","pile","abcde",1},"cat"})
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
func psort(x, d=x.end) {
|
||||
|
||||
if (d.is_zero) {
|
||||
for i in (1 .. x.end) {
|
||||
(x[i] < x[i-1]) && return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
(d+1).times {
|
||||
x.prepend(x.splice(d, 1)...);
|
||||
x[d] < x[d-1] && next;
|
||||
psort(x, d-1) && return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
var a = 10.of { 100.irand };
|
||||
say "Before:\t#{a}";
|
||||
psort(a);
|
||||
say "After:\t#{a}";
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
def permutations:
|
||||
if length == 0 then []
|
||||
else
|
||||
. as $in | range(0;length) | . as $i
|
||||
| ($in|del(.[$i])|permutations)
|
||||
| [$in[$i]] + .
|
||||
end ;
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
def sorted:
|
||||
def until(cond; next):
|
||||
def _until: if cond then . else (next|_until) end;
|
||||
_until;
|
||||
|
||||
length as $length
|
||||
| if $length <= 1 then true
|
||||
else . as $in
|
||||
| 1 | until( . == $length or $in[.-1] > $in[.] ; .+1) == $length
|
||||
end;
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
def permutation_sort_slow:
|
||||
reduce permutations as $p (null; if . then . elif ($p | sorted) then $p else . end);
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
def permutation_sort:
|
||||
# emit the first item in stream that satisfies the condition
|
||||
def first(stream; cond):
|
||||
label $out
|
||||
| foreach stream as $item
|
||||
( [false, null];
|
||||
if .[0] then break $out else [($item | cond), $item] end;
|
||||
if .[0] then .[1] else empty end );
|
||||
first(permutations; sorted);
|
||||
|
|
@ -0,0 +1 @@
|
|||
["too", true, 1, 0, {"a":1}, {"a":0} ] | permutation_sort
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
$ jq -c -n -f Permutation_sort.jq
|
||||
[true,0,1,"too",{"a":0},{"a":1}]
|
||||
Loading…
Add table
Add a link
Reference in a new issue