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,29 @@
|
|||
(lib 'list)
|
||||
|
||||
(for/fold (sign 1) ((σ (in-permutations 4)) (count 100))
|
||||
(printf "perm: %a count:%4d sign:%4d" σ count sign) (* sign -1))
|
||||
|
||||
perm: (0 1 2 3) count: 0 sign: 1
|
||||
perm: (0 1 3 2) count: 1 sign: -1
|
||||
perm: (0 3 1 2) count: 2 sign: 1
|
||||
perm: (3 0 1 2) count: 3 sign: -1
|
||||
perm: (3 0 2 1) count: 4 sign: 1
|
||||
perm: (0 3 2 1) count: 5 sign: -1
|
||||
perm: (0 2 3 1) count: 6 sign: 1
|
||||
perm: (0 2 1 3) count: 7 sign: -1
|
||||
perm: (2 0 1 3) count: 8 sign: 1
|
||||
perm: (2 0 3 1) count: 9 sign: -1
|
||||
perm: (2 3 0 1) count: 10 sign: 1
|
||||
perm: (3 2 0 1) count: 11 sign: -1
|
||||
perm: (3 2 1 0) count: 12 sign: 1
|
||||
perm: (2 3 1 0) count: 13 sign: -1
|
||||
perm: (2 1 3 0) count: 14 sign: 1
|
||||
perm: (2 1 0 3) count: 15 sign: -1
|
||||
perm: (1 2 0 3) count: 16 sign: 1
|
||||
perm: (1 2 3 0) count: 17 sign: -1
|
||||
perm: (1 3 2 0) count: 18 sign: 1
|
||||
perm: (3 1 2 0) count: 19 sign: -1
|
||||
perm: (3 1 0 2) count: 20 sign: 1
|
||||
perm: (1 3 0 2) count: 21 sign: -1
|
||||
perm: (1 0 3 2) count: 22 sign: 1
|
||||
perm: (1 0 2 3) count: 23 sign: -1
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
# iterative Boothroyd method
|
||||
iterator permutations*[T](ys: openarray[T]): tuple[perm: seq[T], sign: int] =
|
||||
var
|
||||
d = 1
|
||||
c = newSeq[int](ys.len)
|
||||
xs = newSeq[T](ys.len)
|
||||
sign = 1
|
||||
|
||||
for i, y in ys: xs[i] = y
|
||||
yield (xs, sign)
|
||||
|
||||
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]
|
||||
sign *= -1
|
||||
yield (xs, sign)
|
||||
inc c[d]
|
||||
|
||||
if isMainModule:
|
||||
for i in permutations([0,1,2]):
|
||||
echo i
|
||||
|
||||
echo ""
|
||||
|
||||
for i in permutations([0,1,2,3]):
|
||||
echo i
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
func perms(n) {
|
||||
var perms = [[+1]]
|
||||
n.times { |x|
|
||||
var sign = -1;
|
||||
perms = gather {
|
||||
for s,*p in perms {
|
||||
var r = (0 .. p.len);
|
||||
take((s < 0 ? r : r.flip).map {|i|
|
||||
[sign *= -1, p[0..i-1], x, p[i..p.end]]
|
||||
}...)
|
||||
}
|
||||
}
|
||||
}
|
||||
perms;
|
||||
}
|
||||
|
||||
var n = 4;
|
||||
for p in perms(n) {
|
||||
var s = p.shift
|
||||
s > 0 && (s = '+1')
|
||||
say "#{p} => #{s}"
|
||||
}
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
# The helper function, _recurse, is tail-recursive and therefore in
|
||||
# versions of jq with TCO (tail call optimization) there is no
|
||||
# overhead associated with the recursion.
|
||||
|
||||
def permutations:
|
||||
def abs: if . < 0 then -. else . end;
|
||||
def sign: if . < 0 then -1 elif . == 0 then 0 else 1 end;
|
||||
def swap(i;j): .[i] as $i | .[i] = .[j] | .[j] = $i;
|
||||
|
||||
# input: [ parity, extendedPermutation]
|
||||
def _recurse:
|
||||
.[0] as $s | .[1] as $p | (($p | length) -1) as $n
|
||||
| [ $s, ($p[1:] | map(abs)) ],
|
||||
(reduce range(2; $n+1) as $i
|
||||
(0;
|
||||
if $p[$i] < 0 and -($p[$i]) > ($p[$i-1]|abs) and -($p[$i]) > ($p[.]|abs)
|
||||
then $i
|
||||
else .
|
||||
end)) as $k
|
||||
| (reduce range(1; $n) as $i
|
||||
($k;
|
||||
if $p[$i] > 0 and $p[$i] > ($p[$i+1]|abs) and $p[$i] > ($p[.]|abs)
|
||||
then $i
|
||||
else .
|
||||
end)) as $k
|
||||
| if $k == 0 then empty
|
||||
else (reduce range(1; $n) as $i
|
||||
($p;
|
||||
if (.[$i]|abs) > (.[$k]|abs) then .[$i] *= -1
|
||||
else .
|
||||
end )) as $p
|
||||
| ($k + ($p[$k]|sign)) as $i
|
||||
| ($p | swap($i; $k)) as $p
|
||||
| [ -($s), $p ] | _recurse
|
||||
end ;
|
||||
|
||||
. as $in
|
||||
| length as $n
|
||||
| (reduce range(0; $n+1) as $i ([]; . + [ -$i ])) as $p
|
||||
# recurse state: [$s, $p]
|
||||
| [ 1, $p] | _recurse
|
||||
| .[1] as $p
|
||||
| .[1] = reduce range(0; $n) as $i ([]; . + [$in[$p[$i] - 1]]) ;
|
||||
|
||||
def count(stream): reduce stream as $x (0; .+1);
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
(["a", "b", "c"] | permutations),
|
||||
"There are \(count( [range(1;6)] | permutations )) permutations of 5 items."
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
$ jq -c -n -f Permutations_by_swapping.jq
|
||||
[1,["a","b","c"]]
|
||||
[-1,["a","c","b"]]
|
||||
[1,["c","a","b"]]
|
||||
[-1,["c","b","a"]]
|
||||
[1,["b","c","a"]]
|
||||
[-1,["b","a","c"]]
|
||||
|
||||
"There are 32 permutations of 5 items."
|
||||
Loading…
Add table
Add a link
Reference in a new issue