September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,69 @@
// version 1.1.3
fun nextPerm(perm: IntArray): Boolean {
val size = perm.size
var k = -1
for (i in size - 2 downTo 0) {
if (perm[i] < perm[i + 1]) {
k = i
break
}
}
if (k == -1) return false // last permutation
for (l in size - 1 downTo k) {
if (perm[k] < perm[l]) {
val temp = perm[k]
perm[k] = perm[l]
perm[l] = temp
var m = k + 1
var n = size - 1
while (m < n) {
val temp2 = perm[m]
perm[m++] = perm[n]
perm[n--] = temp2
}
break
}
}
return true
}
fun List<Int>.isMonotonic(): Boolean {
for (i in 1 until this.size) {
if (this[i] < this[i - 1]) return false
}
return true
}
fun main(args: Array<String>) {
val sizes = args.map { it.toInt() }
println("Partitions for $sizes:\n[")
val totalSize = sizes.sum()
val perm = IntArray(totalSize) { it + 1 }
do {
val partition = mutableListOf<List<Int>>()
var sum = 0
var isValid = true
for (size in sizes) {
if (size == 0) {
partition.add(emptyList<Int>())
}
else if (size == 1) {
partition.add(listOf(perm[sum]))
}
else {
val sl = perm.slice(sum until sum + size)
if (!sl.isMonotonic()) {
isValid = false
break
}
partition.add(sl)
}
sum += size
}
if (isValid) println(" $partition")
}
while (nextPerm(perm))
println("]")
}

View file

@ -0,0 +1,29 @@
function partitions(sequence s)
integer N = sum(s)
sequence set = tagset(N), perm,
results = {}, result, resi
for i=1 to factorial(N) do
perm = permute(i,set)
integer n = 1
result = {}
for j=1 to length(s) do
resi = {}
for k=1 to s[j] do
resi = append(resi,perm[n])
n += 1
end for
result = append(result,sort(resi))
end for
if not find(result,results) then
results = append(results,result)
end if
end for
return sort(results)
end function
ppOpt({pp_Nest,1})
pp(partitions({2,0,2}))
pp(partitions({1,1,1}))
pp(partitions({1,2,0,1}))
pp(partitions({}))
pp(partitions({0,0,0}))

View file

@ -1,48 +1,46 @@
/*REXX program displays ordered partitions: orderedPartitions(i,j,k,···)*/
call orderedPartitions 2,0,2 /*Note: 2,,2 will also work*/
call orderedPartitions 1,1,1
call orderedPartitions 1,2,0,1 /*Note: 1,2,1 will also work*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────ORDEREDPARTITIONS subroutine────────*/
orderedPartitions: procedure; #=arg(); hdr=; bot.=; top.=; low=; high=
d=123456789 /*handy-dandy literal for digits.*/
t=0 /*T: is the sum of all arguments.*/
do i=1 for #; t=t+('0'arg(i)) /*sum all the highest #s in parts*/
end /*i*/
/* [↓] process each of arguments*/
do j=1 for #; _=arg(j) /* _: is the Jth argument.*/
len.j=max(1,_) /*LEN: length of args, 0=special*/
bot.j=left(d,_); if _==0 then bot.j=0 /*define the bottom num*/
top.j=right(left(d,t),_); if _==0 then top.j=0 /* " " top " */
@.j=left(d,t); if _==0 then @.j=0 /*define VERIFY digits.*/
hdr=hdr _ /*build display header.*/
low=low || bot.j; high=high || top.j /*low and high of loop.*/
/*REXX program displays ordered partitions: orderedPartitions(i, j, k, ···). */
call orderedPartitions 2,0,2 /*Note: 2,,2 will also work. */
call orderedPartitions 1,1,1
call orderedPartitions 1,2,0,1 /*Note: 1,2,1 will also work. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
orderedPartitions: procedure; #=arg(); hdr=; bot.=; top.=; low=; high=; d=123456789
t=0 /*T: is the sum of all the arguments.*/
do i=1 for #; t=t + arg(i) /*sum all the highest numbers in parts.*/
end /*i*/ /* [↑] may have an omitted argument. */
/* [↓] process each of the arguments. */
do j=1 for #; _=arg(j) /* _: is the Jth argument. */
len.j=max(1, _) /*LEN: length of args, 0=special. */
bot.j=left(d, _); if _==0 then bot.j=0 /*define the bottom number. */
top.j=right(left(d,t),_); if _==0 then top.j=0 /* " " top " */
@.j=left(d, t); if _==0 then @.j=0 /*define the digits used for VERIFY. */
hdr=hdr _ /*build (by appending) display header.*/
low=low || bot.j; high=high || top.j /*the low and high numbers for DO below*/
end /*j*/
okD=left(0||d,t+1) /*define legal digits to be used.*/
say center(' partitions for: ' hdr" ",50,''); say
do g=low to high /* [↑] generate ordered parts. */
if verify(g,okD)\==0 then iterate /*filter out the unwanted digits.*/
p=1 /*P: is the position of a digit.*/
$= /*$: will be the transformed #s.*/
do k=1 for # /*verify the partitions numbers. */
/*validate#: dups/ordered/repeats*/
_=substr(g,p,len.k) /*ordered part num. to be tested.*/
if verify(_,@.k)\==0 then iterate g /*is digit ¬ valid ? */
!= /* [↓] validate num.*/
if @.k\==0 then do j=1 for length(_); z=substr(_,j,1)
if pos(z,$)\==0 then iterate g /*prev*/
okD=left(0 || d, t+1) /*define the legal digits to be used. */
say center(' partitions for: ' hdr" ", 60, '') /*display centered title for the output*/
say
do g=low to high /* [↑] generate the ordered partitions*/
if verify(g, okD)\==0 then iterate /*filter out unwanted decimal digits. */
p=1 /*P: is the position of a decimal dig.*/
$= /*$: will be the transformed numbers. */
do k=1 for # /*verify the partitions numbers. */
/*validate number: dups/ordered/repeats*/
_=substr(g,p,len.k) /*ordered partition number to be tested*/
if verify(_, @.k)\==0 then iterate g /*is the decimal digit not valid ? */
!= /* [↓] validate the decimal number. */
if @.k\==0 then do j=1 for length(_); z=substr(_, j, 1)
if pos(z, $)\==0 then iterate g /*previous. */
!=!','z
if j==1 then iterate
if z<=substr(_,j-1,1) then iterate g /*ord.*/
if pos(z,_,1+pos(z,_))\==0 then iterate g /*dup.*/
if j==1 then iterate /*is firstt?*/
if z<=substr(_, j-1, 1) then iterate g /*ordered. */
if pos(z, _, 1+pos(z,_))\==0 then iterate g /*duplicate.*/
end /*j*/
p=p+len.k /*point to next #.*/
$=$ ' {'strip(translate(!,,0),,',')"}" /*dress the # up. */
p=p + len.k /*point to the next decimal number. */
$=$ ' {'strip( translate(!, ,0), ,",")'}' /*dress number up by suppressing LZ ···*/
end /*k*/
say $
say ' ' $ /*display numbers in ordered partition.*/
end /*g*/
say
return

View file

@ -3,7 +3,7 @@ func partitions({.is_empty}) { [[]] }
func part(s, args) {
gather {
s.combinations(args[0], { |c|
s.combinations(args[0], { |*c|
part(s - c, args.ft(1)).each{|r| take([c] + r) }
})
}

View file

@ -0,0 +1,13 @@
fcn partitions(args){
args=vm.arglist;
s:=(1).pump(args.sum(0),List); // (1,2,3,...)
fcn(s,args,p){
if(not args) return(T(T));
res:=List();
foreach c in (Utils.Helpers.pickNFrom(args[0],s)){
s0:=s.copy().removeEach(c);
foreach r in (self.fcn(s0,args[1,*])){ res.append(T(c).extend(r)) }
}
res
}(s,args)
}

View file

@ -0,0 +1,4 @@
args:=vm.arglist.apply("toInt"); // aka argv[1..]
if(not args) args=T(2,0,2);
partitions(args.xplode()).pump(Console.println,Void);
// or: foreach p in (partitions(1,1,1)){ println(p) }