[Newbies] Re: Another extension proposal -> subsets

cdrick cdrick65 at gmail.com
Wed Jul 23 17:26:58 UTC 2008


>
> Set>>subsets
>  | subsets |
>  subsets := Set with: self.
>  self asArray permutationsDo: [:e | subsets add: e asSet].
>  self do: [:e | subsets addAll: (self copyWithout: e) subsets].
>  ^ subsets

nice too and frustrating how you got it quick :)

I tried a recursive method too first but found the byte ressemblance so...
My only consolation is that the recursive solution (subsets2) is
slower and hardly work for Set with more than 10 elements.

set := #(1 2 3 4 5) asSet.
[ set subsets  ] timeToRun ." 1"
[ set subsets2  ] timeToRun. " 8"

set := #(1 2 3 4 5 6 7) asSet.
[ set subsets  ] timeToRun ." 5"
[ set subsets2  ] timeToRun. " 233"

set := #(1 2 3 4 5 6 7 8 ) asSet.
[ set subsets  ] timeToRun . " 11"
[ set subsets2  ] timeToRun. " 1683"

set := (1 to: 10) asSet.
[ set subsets  ] timeToRun . " 46"

set := (1 to: 15) asSet.
[ set subsets  ] timeToRun ." 2484"

set := (1 to: 20) asSet.
[ set subsets  ] timeToRun .  "559953"  "but here the result has (2
raisedTo: 20)  1 048 576 Sets :)"

set := (1 to: 50) asSet.
[ set subsets  ] timeToRun .    "I got a "space is low   wow" :)

I have to go,
That was fun :)

See you

Cédrick


More information about the Beginners mailing list