PDA

View Full Version : Combinations



silentsound
01-25-2011, 02:16 PM
Hi,

I need a function which takes an array of elements and a size of desired set as its argument (say cells A1:10) and returns all the possible combinations of those elements in a set of the desired size (so there are N choose R possibilities). I am struggling to understand the algorithm to do so. I have seen some, such as the one below in python but I don't get exactly why it works. I understand that you are shifting an index by one each time, but nothing more than this.

I was wondering if someone could either explain how the algorithm works and/ or post some VBA code to do this.

Here's the python I have seen:

def choose_iter(elements, length):
for i in xrange(len(elements)):
if length == 1:
yield (elements[i],)
else:
for next in choose_iter(elements[i+1:len(elements)], length-1):
yield (elements[i],) + next
def choose(l, k):
return list(choose_iter(l, k))


Thanks guys!

stanleydgrom
01-25-2011, 07:42 PM
silentsound,

See if this helps:

Myrna Larson, July 25, 2000, Microsoft.Public.Excel.Misc
http://www.mydatabasesupport.com/forums/spreadsheets/250560-combinations.html


Have a great day,
Stan