Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -0,0 +1,61 @@
* Permutations 26/10/2015
PERMUTE CSECT
USING PERMUTE,R15 set base register
LA R9,TMP-A n=hbound(a)
SR R10,R10 nn=0
LOOP LA R10,1(R10) nn=nn+1
LA R11,PG pgi=@pg
LA R6,1 i=1
LOOPI1 CR R6,R9 do i=1 to n
BH ELOOPI1
LA R2,A-1(R6) @a(i)
MVC 0(1,R11),0(R2) output a(i)
LA R11,1(R11) pgi=pgi+1
LA R6,1(R6) i=i+1
B LOOPI1
ELOOPI1 XPRNT PG,80
LR R6,R9 i=n
LOOPUIM BCTR R6,0 i=i-1
LTR R6,R6 until i=0
BE ELOOPUIM
LA R2,A-1(R6) @a(i)
LA R3,A(R6) @a(i+1)
CLC 0(1,R2),0(R3) or until a(i)<a(i+1)
BNL LOOPUIM
ELOOPUIM LR R7,R6 j=i
LA R7,1(R7) j=i+1
LR R8,R9 k=n
LOOPWJ CR R7,R8 do while j<k
BNL ELOOPWJ
LA R2,A-1(R7) r2=@a(j)
LA R3,A-1(R8) r3=@a(k)
MVC TMP,0(R2) tmp=a(j)
MVC 0(1,R2),0(R3) a(j)=a(k)
MVC 0(1,R3),TMP a(k)=tmp
LA R7,1(R7) j=j+1
BCTR R8,0 k=k-1
B LOOPWJ
ELOOPWJ LTR R6,R6 if i>0
BNP ILE0
LR R7,R6 j=i
LA R7,1(R7) j=i+1
LOOPWA LA R2,A-1(R7) @a(j)
LA R3,A-1(R6) @a(i)
CLC 0(1,R2),0(R3) do while a(j)<a(i)
BNL AJGEAI
LA R7,1(R7) j=j+1
B LOOPWA
AJGEAI LA R2,A-1(R7) r2=@a(j)
LA R3,A-1(R6) r3=@a(i)
MVC TMP,0(R2) tmp=a(j)
MVC 0(1,R2),0(R3) a(j)=a(i)
MVC 0(1,R3),TMP a(i)=tmp
ILE0 LTR R6,R6 until i<>0
BNE LOOP
XR R15,R15 set return code
BR R14 return to caller
A DC C'ABCD' <== input
TMP DS C temp for swap
PG DC CL80' ' buffer
YREGS
END PERMUTE

View file

@ -0,0 +1,35 @@
(defn- iter-perm [v]
(let [len (count v),
j (loop [i (- len 2)]
(cond (= i -1) nil
(< (v i) (v (inc i))) i
:else (recur (dec i))))]
(when j
(let [vj (v j),
l (loop [i (dec len)]
(if (< vj (v i)) i (recur (dec i))))]
(loop [v (assoc v j (v l) l vj), k (inc j), l (dec len)]
(if (< k l)
(recur (assoc v k (v l) l (v k)) (inc k) (dec l))
v))))))
(defn- vec-lex-permutations [v]
(when v (cons v (lazy-seq (vec-lex-permutations (iter-perm v))))))
(defn lex-permutations
"Fast lexicographic permutation generator for a sequence of numbers"
[c]
(lazy-seq
(let [vec-sorted (vec (sort c))]
(if (zero? (count vec-sorted))
(list [])
(vec-lex-permutations vec-sorted)))))
(defn permutations
"All the permutations of items, lexicographic by index"
[items]
(let [v (vec items)]
(map #(map v %) (lex-permutations (range (count v))))))
(println (permutations [1 2 3]))

View file

@ -1,34 +1,47 @@
permute(a: ARRAY[INTEGER]; k: INTEGER)
require
ar_not_void: a.count>=1
k_valid_index: k>0
local
i,t: INTEGER
do
if k=a.count then
across a as ar loop io.put_string (ar.item.out) end
io.put_string ("%N")
else
from
i:= k
until
i> a.count
loop
t:= a[k]
a[k]:= a[i]
a[i]:= t
permute(a,k+1)
t:= a[k]
a[k]:= a[i]
a[i]:= t
i:= i+1
class
APPLICATION
create
make
feature {NONE}
make
do
test := <<2, 5, 1>>
permute (test, 1)
end
test: ARRAY [INTEGER]
permute (a: ARRAY [INTEGER]; k: INTEGER)
-- All permutations of 'a'.
require
count_positive: a.count > 0
k_valid_index: k > 0
local
t: INTEGER
do
if k = a.count then
across
a as ar
loop
io.put_integer (ar.item)
end
io.new_line
else
across
k |..| a.count as c
loop
t := a [k]
a [k] := a [c.item]
a [c.item] := t
permute (a, k + 1)
t := a [k]
a [k] := a [c.item]
a [c.item] := t
end
end
end
end
make
do
test:= << 2,5,1>>
permute(test, 1)
end
test: ARRAY[INTEGER]
end

View file

@ -0,0 +1,8 @@
defmodule RC do
def permute([]), do: [[]]
def permute(list) do
for x <- list, y <- permute(list -- [x]), do: [x|y]
end
end
IO.inspect RC.permute([1, 2, 3])

View file

@ -1,41 +1,27 @@
program nptest
integer n,i,a
logical nextp
external nextp
parameter(n=4)
dimension a(n)
do i=1,n
a(i)=i
enddo
10 print *,(a(i),i=1,n)
if(nextp(n,a)) go to 10
end
function nextp(n,a)
integer n,a,i,j,k,t
logical nextp
dimension a(n)
i=n-1
10 if(a(i).lt.a(i+1)) go to 20
i=i-1
if(i.eq.0) go to 20
go to 10
20 j=i+1
k=n
30 t=a(j)
a(j)=a(k)
a(k)=t
j=j+1
k=k-1
if(j.lt.k) go to 30
j=i
if(j.ne.0) go to 40
nextp=.false.
return
40 j=j+1
if(a(j).lt.a(i)) go to 40
t=a(i)
a(i)=a(j)
a(j)=t
nextp=.true.
end
program allperm
implicit none
integer :: n, i
integer, allocatable :: a(:)
read *, n
allocate(a(n))
a = [ (i, i = 1, n) ]
call perm(1)
deallocate(a)
contains
recursive subroutine perm(i)
integer :: i, j, t
if (i == n) then
print *, a
else
do j = i, n
t = a(i)
a(i) = a(j)
a(j) = t
call perm(i + 1)
t = a(i)
a(i) = a(j)
a(j) = t
end do
end if
end subroutine
end program

View file

@ -0,0 +1,41 @@
program nptest
integer n,i,a
logical nextp
external nextp
parameter(n=4)
dimension a(n)
do i=1,n
a(i)=i
enddo
10 print *,(a(i),i=1,n)
if(nextp(n,a)) go to 10
end
function nextp(n,a)
integer n,a,i,j,k,t
logical nextp
dimension a(n)
i=n-1
10 if(a(i).lt.a(i+1)) go to 20
i=i-1
if(i.eq.0) go to 20
go to 10
20 j=i+1
k=n
30 t=a(j)
a(j)=a(k)
a(k)=t
j=j+1
k=k-1
if(j.lt.k) go to 30
j=i
if(j.ne.0) go to 40
nextp=.false.
return
40 j=j+1
if(a(j).lt.a(i)) go to 40
t=a(i)
a(i)=a(j)
a(j)=t
nextp=.true.
end

View file

@ -0,0 +1,30 @@
(function () {
// [a] -> [[a]]
function permutations(xs) {
return xs.length ? (
chain( xs, function (x) {
return chain( permutations(deleted(x, xs)), function (ys) {
return ( [[x].concat(ys)] );
})})) : [[]]
}
// monadic bind/chain for lists
function chain(xs, f) {
return [].concat.apply([], xs.map(f));
}
// drops first instance found
function deleted(x, xs) {
return xs.length ? (
x === xs[0] ? xs.slice(1) : [xs[0]].concat(
deleted(x, xs.slice(1))
)
) : [];
}
return permutations(['Aardvarks', 'eat', 'ants'])
})();

View file

@ -0,0 +1 @@
[["Aardvarks", "eat", "ants"], ["Aardvarks", "ants", "eat"], ["eat", "Aardvarks", "ants"], ["eat", "ants", "Aardvarks"], ["ants", "Aardvarks", "eat"], ["ants", "eat", "Aardvarks"]]

View file

@ -0,0 +1,11 @@
term = "RCode"
i = 0
pcnt = factorial(length(term))
print("All the permutations of ", term, " (", pcnt, "):\n ")
for p in permutations(split(term, ""))
print(join(p), " ")
i += 1
i %= 12
i != 0 || print("\n ")
end
println()

View file

@ -0,0 +1,26 @@
function permutation ($array) {
function generate($n, $array, $A) {
if($n -eq 1) {
$array[$A] -join ' '
}
else{
for( $i = 0; $i -lt ($n - 1); $i += 1) {
generate ($n - 1) $array $A
if($n % 2 -eq 0){
$i1, $i2 = $i, ($n-1)
$A[$i1], $A[$i2] = $A[$i2], $A[$i1]
}
else{
$i1, $i2 = 0, ($n-1)
$A[$i1], $A[$i2] = $A[$i2], $A[$i1]
}
}
generate ($n - 1) $array $A
}
}
$n = $array.Count
if($n -gt 0) {
(generate $n $array (0..($n-1)))
} else {$array}
}
permutation @('A','B','C')

View file

@ -0,0 +1,26 @@
def perm1(n):
a = list(range(n))
def sub(i):
if i == n - 1:
yield tuple(a)
else:
for k in range(i, n):
a[i], a[k] = a[k], a[i]
yield from sub(i + 1)
a[i], a[k] = a[k], a[i]
yield from sub(0)
def perm2(n):
a = list(range(n))
def sub(i):
if i == n - 1:
yield tuple(a)
else:
for k in range(i, n):
a[i], a[k] = a[k], a[i]
yield from sub(i + 1)
x = a[i]
for k in range(i + 1, n):
a[k - 1] = a[k]
a[n - 1] = x
yield from sub(0)

View file

@ -0,0 +1,15 @@
for u in perm1(3): print(u)
(0, 1, 2)
(0, 2, 1)
(1, 0, 2)
(1, 2, 0)
(2, 1, 0)
(2, 0, 1)
for u in perm2(3): print(u)
(0, 1, 2)
(0, 2, 1)
(1, 0, 2)
(1, 2, 0)
(2, 0, 1)
(2, 1, 0)

View file

@ -0,0 +1,39 @@
def nextperm(a):
n = len(a)
i = n - 1
while i > 0 and a[i - 1] > a[i]:
i -= 1
j = i
k = n - 1
while j < k:
a[j], a[k] = a[k], a[j]
j += 1
k -= 1
if i == 0:
return False
else:
j = i
while a[j] < a[i - 1]:
j += 1
a[i - 1], a[j] = a[j], a[i - 1]
return True
def perm3(n):
if type(n) is int:
if n < 1:
return []
a = list(range(n))
else:
a = sorted(n)
u = [tuple(a)]
while nextperm(a):
u.append(tuple(a))
return u
for p in perm3(3): print(p)
(0, 1, 2)
(0, 2, 1)
(1, 0, 2)
(1, 2, 0)
(2, 0, 1)
(2, 1, 0)