[Squeak] Newbie permutations

Richard A. O'Keefe ok at cs.otago.ac.nz
Tue Apr 9 23:34:32 UTC 2002


Michael Grant <mwgrant2001 at yahoo.com> wrote:
	The following expression (Squeak 3.0)writes the
	permutations of #(1 3 5) to the Transcript as expected:
	
	#(1 3 5) permutationsDo: [:each | Transcript cr;
	                                  show: each printString].
	
	However, I can not figure out how to save the
	permutations to a variable, say 'Foo',

Look at the comment in SequenceableCollection>>permutationsDo:
     v sic! v                     vvvvvv
    "Repeatly value aBlock with a single copy of the receiver.
                                  ^^^^^^
     Reorder the copy so that aBlock is presented all
     (self size factorial) possible permutations."

The point which explains everything is that each time your block is
called, it is passed the *same* object.

So if you want to keep any of these permutations (let alone all of them)
you will have to copy them.

    r := OrderedCollection new.
    aSequence permutationsDo: [:each | r add: each copy].

This will require O(s.s!) space where s is (aSequence size),
so it's only useful for really small sequences (s = 12 would be on the
rough order of 20GB, if I'm doing my sums right, and I wouldn't bother
trying even s = 10 myself).




More information about the Squeak-dev mailing list