[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [sc-users] non random integer partition




Am 20.12.2015 um 13:42 schrieb simdax <simoncornaz@xxxxxxxxx>:

Hello,

nop MagicWindow, what I want is something like
5 => (1, 1, 1, 1, 1) , (2, 1, 1, 1),  (2, 2, 1), (3, 1, 1), (3, 2), (4, 1),
(5)

And as always, thank you Daniel, the enum tool is very useful :D !

But, in reality, my question stays : is it possible to "translate" the first
python func, which seems pretty similar to your function "g" in tutorial,
Daniel



The python code does something different from SC's built-in method 'partitions' !
The latter gives out *ordered partitions* in mathematical sense
wheras the python code only regards *unordered partitions* in mathematical sense -
simpler task, shorter code.

There might a confusion about this as in the implementation we have ordered collections (arrays)
in both cases and when results are additionally ascending (ordered numbers) they represent
the *unordered* partition in mathematical sense, where order doesn't count.

But of course you can also write this recursively in SC
(I take a recursion start of 1 instead of 0 here, as it's more comfortable):

~p = { |n|
var ps;
(n==1).if { [[1]] }{
ps = ~p.(n-1).copy;
ps.collect([1]++_) ++ (
ps.select { |p| (p.size<2) or: { p[1]>p[0] } }
.collect { |p| [p[0]+1] ++ p[1..] }
)
}
}


~p.(5)


The code in the miSCellaneous example with Functions g and h additionally regards 
ordering and size of the partition, which, when summing over all sizes
results in finding all ordered partitions.


Regards

Daniel


-----------------------------
www.daniel-mayer.at
-----------------------------