On Tue, 5 Jan 2010, John Toohey wrote:
But this doesn't get 1-1, 2-2 or 3-3. I had tried something like that use #reverse on each combination, but #combinations does not generate -duplicates- from the number sequences.
I guess you are looking for this: allPairsFromCollectionDo := [ :collection :block | | o | o := OrderedCollection newFrom: collection. o size timesRepeat: [ o with: collection do: block. o add: o removeFirst ] ]. Transcript open. allPairsFromCollectionDo value: #(1 2 3 4) value: [ :a :b | Transcript print: { a. b }; cr ]. Transcript flush Levente
On Tue, Jan 5, 2010 at 12:16, Henrik Johansen <henrik.s.johansen@veloxit.no>wrote:
On Jan 5, 2010, at 5:19 32PM, John Toohey wrote:
Hi, I'm trying to generate the permutations of a sequence of numbers. I've found # permutationsDo: aBlock, but this generated the permutation of the entire sequence. I want to do it in groups of 2. The #combinations: anInteger atATimeDo: aBlock method does what I need for combinations, but I cannot find the equivalent for permutations. Does something like this exist in the standard library, or in an external package.
-- -JT
How about: #(1 2 3 4) combinations: 2 atATimeDo: [:array | array permutationsDo: [:each | Transcript show: each printString; cr]] ?
Cheers, Henry
_______________________________________________ Pharo-project mailing list Pharo-project@lists.gforge.inria.fr http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
-- -JT