[Newbies] Re: Another extension proposal -> subsets
nicolas cellier
ncellier at ifrance.com
Wed Jul 23 18:29:04 UTC 2008
You have one messsage #combinations:atATimeDo: to enumerate partitions,
but in SequenceableCollection, not in Set:
| collec subset |
collec := 1 to: 4.
subset := OrderedCollection new: (2 raisedTo: collec size).
subset add: #().
1 to: collec size do: [:subSize |
collec combinations: subSize atATimeDo: [:subArray | subset add:
subArray copy]].
subset
Mind the copy, because the iteration recycle same subArray for economy.
cdrick a écrit :
>> 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
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Beginners mailing list
> Beginners at lists.squeakfoundation.org
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
More information about the Beginners
mailing list