Consulting

Results 1 to 2 of 2

Thread: Combinations

  1. #1

    Combinations

    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!

  2. #2
    VBAX Tutor
    Joined
    Nov 2006
    Location
    North East Pennsylvania, USA
    Posts
    203
    Location
    silentsound,

    See if this helps:

    Myrna Larson, July 25, 2000, Microsoft.Public.Excel.Misc
    http://www.mydatabasesupport.com/for...binations.html


    Have a great day,
    Stan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •