Data update

This commit is contained in:
Ingy döt Net 2025-02-27 18:35:13 -05:00
parent 8e4e15fa56
commit 72eb4943cb
1853 changed files with 35514 additions and 9441 deletions

View file

@ -0,0 +1,33 @@
# determine and print all permutations of numbers 1..n
# General variant: simple, single array, recursive, not in lexical order
#
# An array with n places is used, marked by 0 as free.
# The current element l replaces the free places one by one,
# and the next element l+1 is probed recursively with the array.
#
function permute1(l, n, r, i) {
if ( l <= n) {
for (i=1; i<=n; ++i) {
if (r[i] == 0) {
r[i] = l
permute1(l+1, n, r)
r[i] = 0
}
}
return
}
# print result; consumes ca. 50% of CPU time
s = ""
for (i=1; i <= length(r); ++i) # ensure order
s = s r[i] " "
print s
}
# command line parameter is number of elements.
BEGIN {
n = 3 # default
if (ARGC > 1) n = ARGV[1] # number may be given as parameter
for (i=1; i <=n; ++i) r[i] = 0 # fill with zeroes
permute1(1, n, r)
}

View file

@ -0,0 +1,23 @@
function permute(l, k, i, s) {
if (k == length(l)) {
show(s)
return
}
for (i=k; i <= length(l); ++i) {
swap(l, i, k)
permute(l, k+1)
swap(l, k, i)
}
}
function swap(l, i, k, t) {
t = l[i]
l[i] = l[k]
l[k] = t
}
BEGIN {
n = 3 # default
if (ARGC > 1) n = ARGV[1] # number may be given as parameter
for (i=1; i <=n; ++i)
l[i] = i
permute(l, 1)
}

View file

@ -7,7 +7,7 @@ val permute = fn(plist) {
if len(plist) > limit: throw "permutation limit exceeded (currently {{limit}})"
var elements = plist
var ordinals = pseries(len(elements))
var ordinals = series(len(elements))
val n = len(ordinals)
var i, j

View file

@ -1,43 +1,42 @@
Module StepByStep {
Function PermutationStep (a) {
c1=lambda (&f, a) ->{
=car(a)
f=true
}
m=len(a)
c=c1
while m>1 {
c1=lambda c2=c,p, m=(,) (&f, a) ->{
if len(m)=0 then m=a
=cons(car(m),c2(&f, cdr(m)))
if f then f=false:p++: m=cons(cdr(m), car(m)) : if p=len(m) then p=0 : m=(,):: f=true
}
c=c1
m--
}
=lambda c, a (&f) -> {
=c(&f, a)
}
}
k=false
StepA=PermutationStep((1,2,3,4))
while not k {
Print StepA(&k)
}
k=false
StepA=PermutationStep((100,200,300))
while not k {
Print StepA(&k)
}
k=false
StepA=PermutationStep(("A", "B", "C", "D"))
while not k {
Print StepA(&k)
}
k=false
StepA=PermutationStep(("DOG", "CAT", "BAT"))
while not k {
Print StepA(&k)
}
Function PermutationStep {
a=array([])
c1=lambda (&f, a) ->{
=car(a)
f=true
}
m=len(a)
c=c1
while m>1
c1=lambda c2=c, p, m=(,) (&f, a) ->{
if len(m)=0 then m=a
=cons(car(m),c2(&f, cdr(m)))
if f then
f=false
p++
m=cons(cdr(m), car(m))
if p=len(m) then
p=0
m=(,)
f=true
end if
end if
}
c=c1
m--
end while
=lambda c, a (&f) -> {
=c(&f, a)
}
}
display(PermutationStep(1,2,3,4))
display(PermutationStep(100,200,300))
display(PermutationStep("A", "B", "C", "D"))
display(PermutationStep("DOG", "CAT", "BAT"))
sub display(S as lambda)
k=false
while not k : Print S(&k)#str$(): end while
end sub
}
StepByStep