Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -33,6 +33,6 @@ Note: Do not use functions that are not in the standard library of the programmi
'''Notation'''
Remarks on the used notation for the task in order to understand it easierly.
Here are some explanatory remarks on the notation used in the task description:
<math>\{1, \ldots, n\}</math> denotes the set of consecutive numbers from <math>1</math> to <math>n</math>, e.g. <math>\{1,2,3\}</math> if <math>n = 3</math>. <math>\Sigma</math> is the mathematical notation for summation, e.g. <math>\Sigma_{i=1}^3 i = 6</math> (see also [http://en.wikipedia.org/wiki/Summation#Capital-sigma_notation]). <math>\mathit{arg}_1,\mathit{arg}_2,...,\mathit{arg}_n</math> are the arguments — natural numbers — that the sought function receives.

View file

@ -0,0 +1,25 @@
import std.stdio, std.algorithm, std.range, std.array, std.conv,
combinations3;
alias iRNG = int[];
iRNG[][] orderPart(iRNG blockSize...) {
iRNG tot = iota(1, 1 + blockSize.sum).array;
iRNG[][] p(iRNG s, in iRNG b) {
if (b.empty)
return [[]];
iRNG[][] res;
foreach (c; s.combinations(b[0]))
foreach (r; p(setDifference(s, c).array, b.dropOne))
res ~= c.dup ~ r;
return res;
}
return p(tot, blockSize);
}
void main(in string[] args) {
auto b = args.length > 1 ? args.dropOne.to!(int[]) : [2, 0, 2];
writefln("%(%s\n%)", b.orderPart);
}

View file

@ -0,0 +1,44 @@
import core.stdc.stdio;
void genBits(size_t N)(ref uint[N] bits, in ref uint[N] parts,
uint mask, uint all, uint res, uint n, uint pid)
nothrow @nogc {
static void showPart(in uint x) nothrow @nogc {
'['.putchar;
for (uint i = 0; (1 << i) <= x; i++)
if (x & (1 << i))
printf("%d ", i + 1);
']'.putchar;
}
while (!n) {
bits[pid] = res;
pid++;
if (pid == N) {
foreach (immutable b; bits)
showPart(b);
'\n'.putchar;
return;
}
all &= ~res;
mask = all;
res = 0;
n = parts[pid];
}
while (mask) {
immutable uint i = mask & -int(mask);
mask &= ~i;
genBits(bits, parts, mask, all, res | i, n - 1, pid);
}
}
void main() nothrow @nogc {
immutable uint[3] parts = [2, 0, 2];
uint m = 1;
foreach (immutable p; parts)
m <<= p;
uint[parts.length] bits;
genBits(bits, parts, m - 1, m - 1, 0, parts[0], 0);
}

View file

@ -1,29 +0,0 @@
import std.stdio, std.algorithm, std.range, std.array, std.conv;
import combinations4: Comb;
alias iRNG = int[];
iRNG setDiff(iRNG s, iRNG c) {
return setDifference(s, c).array;
}
iRNG[][] orderPart(iRNG blockSize...) {
iRNG sum = iota(1, 1 + blockSize.sum).array;
iRNG[][] p(iRNG s, in iRNG b) {
if (b.length == 0)
return [[]];
iRNG[][] res;
foreach (c; Comb.On(s, b[0]))
foreach (r; p(setDiff(s, c), b.dropOne))
res ~= c.dup ~ r;
return res;
}
return p(sum, blockSize);
}
void main(in string[] args) {
auto b = args.length > 1 ? args.dropOne.to!(int[]) : [2, 0, 2];
writefln("%(%s\n%)", b.orderPart);
}

View file

@ -4,9 +4,9 @@ def partitions = { int... sizes ->
Set parts = perms.collect { p -> sizes.collect { s -> (0..<s).collect { p.pop() } as Set } }
parts.sort{ a, b ->
if (!a) return 0
def comp = [a,b].transpose().find { it[0] != it[1] }
if (!comp) return 0
def recomp = comp.collect{ it as List }.transpose().find { it[0] != it[1] }
def comp = [a,b].transpose().find { aa, bb -> aa != bb }
if (!comp) return 0
def recomp = comp.collect{ it as List }.transpose().find { aa, bb -> aa != bb }
if (!recomp) return 0
return recomp[0] <=> recomp[1]
}

View file

@ -1,48 +1,19 @@
sub partitions {
my $sum = 0;
$sum += $_ for @_; # total number of elements
make_part ( $_[-1], # desired partition size
0, # initial trial position
[ (0) x $sum ], # table recording of used element
[], # current pick for current partition
[ $#_, # total number of partitions
\@_, # partition sizes
[] # for output, each partition's elements
] # Note: last group of args wrapped in array ref
); # to reduce argument passing overhead
use List::Util 1.33 qw(sum pairmap);
sub partition {
my @mask = @_;
my $last = sum @mask or return [map {[]} 0..$#mask];
return pairmap {
$b ? do {
local $mask[$a] = $b - 1;
map { push @{$_->[$a]}, $last; $_ }
partition(@mask);
} : ()
} %mask[0..$#mask];
}
sub make_part {
my ($n, $pos, $used, $picked, $more) = @_;
return if $pos > @$used;
# Input & Output handling:
# the making-next-partition part
if (!$n) {
my ($part_idx, $sizes, $q) = @$more;
push @$q, $picked;
if ($part_idx > 1) {
make_part($sizes->[$part_idx-1], 0, $used, [],
[ $part_idx-1, $sizes, $q]);
} else {
my @x = grep { !$used->[$_] } 0 .. (@$used-1);
print "[ @$_ ]" for @$q;
print "[ @x ]\n";
}
pop @$q;
return;
}
# the picking-element-to-make-partition part
for my $i ($pos .. @$used - 1) {
next if $used->[$i];
push @$picked, $i;
$used->[$i] = 1;
make_part($n - 1, $i + 1, $used, $picked, $more);
$used->[$i] = 0;
pop @$picked;
}
}
partitions(4, 2, 4);
print "(" . join(', ', map { "{".join(', ', @$_)."}" } @$_) . ")\n"
for partition( @ARGV ? @ARGV : (2, 0, 2) );

View file

@ -0,0 +1,48 @@
/*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.*/
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*/
!=!','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.*/
end /*j*/
p=p+len.k /*point to next #.*/
$=$ ' {'strip(translate(!,,0),,',')"}" /*dress the # up. */
end /*k*/
say $
end /*g*/
say
return

View file

@ -0,0 +1,6 @@
def partition(mask)
return [[]] if mask.empty?
[*1..mask.inject(:+)].permutation.map {|perm|
mask.map {|num_elts| perm.shift(num_elts).sort }
}.uniq
end

View file

@ -0,0 +1,10 @@
def part(s, args)
return [[]] if args.empty?
s.combination(args[0]).each_with_object([]) do |c, res|
part(s - c, args[1..-1]).each{|r| res << ([c] + r)}
end
end
def partitions(args)
return [[]] if args.empty?
part((1..args.inject(:+)).to_a, args)
end

View file

@ -0,0 +1,5 @@
[[],[0,0,0],[1,1,1],[2,0,2]].each do |test_case|
puts "partitions #{test_case}:"
partition(test_case).each{|part| p part }
puts
end

View file

@ -1,15 +0,0 @@
def partition(mask)
return [[]] if mask.empty?
sum = mask.inject(:+)
a = Array.new(sum){|e| e}
res = a.permutation.map do |perm|
mask.map {|num_elts| perm.shift(num_elts).sort }
end
res.uniq
end
[[],[0,0,0],[1,1,1],[2,0,2]].each do |test_case|
p test_case
partition(test_case).each{|part| p part }
puts
end