Hi, I propose a simpler implementation: 4 in: [ :e | (1 to: e) combinations ] Vincent Le 2015-10-22 23:25, Ferlicot D. Cyril a écrit :
Le 22/10/2015 22:58, stepharo a écrit :
Hi
I was programming an exercise with one of my son (well in Python.... arghhhhhh) and I end it up doing it in Pharo (I'm save now).
The idea was to write one function that computes the powerset
powerset(4) = a Set(a Set(1) a Set(1 2) a Set(3) a Set(2) a Set(1 3) a Set(2 3) a Set(1 2 3) a Set(4) a Set(1 4) a Set(2 4) a Set(1 2 4) a Set(3 4) a Set(1 3 4) a Set(2 3 4) a Set(1 2 3 4))
I did it without thinking too much in fact
| s n ps | ps := Set new.
1 to: ((2 raisedTo: 4) -1) do: [ :i | s := Set new. n := 0. 1 to: 4 do: [ :b | n := n + 1. ((i bitAt: b) = 1 ) ifTrue: [ s add: n]. ps add: s ]]. ps
but I wonder if we want to add it to our lib.
Stef
Hi,
Want would be the uses of this method?