Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 7th, 2005, 6:14 AM   #1
Gilward Kukel
Newbie
 
Join Date: Jun 2005
Location: Vienna, Austria
Posts: 15
Rep Power: 0 Gilward Kukel is on a distinguished road
Smile Combinations

Here are two functions that I wrote:
comb_count(n, k) returns the number of k-combinations of n elements.
comb_list(seq, k) returns a list of all k-combinations of the elements of sequence seq.
So comb_count(n,k) gives the same result as len(comb_list(seq,k)) when len(seq)==n

More about combinations:
http://en.wikipedia.org/wiki/Combination
http://www.theory.csc.uvic.ca/~cos/i...tionsInfo.html
http://mathworld.wolfram.com/Combination.html
http://web.hamline.edu/~lcopes/SciMa...pts/ccomb.html

Example usage:
>>> comb_count(5,3)
10
>>> comb_list(range(0,5),3)
[[0, 1, 2], [0, 1, 3], [0, 1, 4], [0, 2, 3], [0, 2, 4],
 [0, 3, 4], [1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]
>>> len(_)
10

function definitions:
def comb_count(n, k):
    "returns the number of k-combinations of n elements"
    if n<0 or k<0 or n!=long(n) or k!=long(k):
        raise Exception,"n and k should be nonnegative integers"
    if n<k:
        return 0
    elif n==k:
        return 1
    if k>n/2:
        k=n-k
    h=1
    for i in range (1,k+1):
        h*=n-i+1
        h/=i
    return h

def comb_list(seq, k):
    "returns a list of all k-combinations of the elements of sequence seq"
    n=len(seq)
    if not 0<=k<=n:
        raise Exception,"0<=k<=len(seq) is not true"
    v=[]   #list of combinations
    #x: number of taken elements
    #y: number of rejected elements
    #we want to take k elements and reject n-k elements
    def f(x,y,a):
        if x==k:
            #we have taken enough elements, reject all remaining elements
            v.append(a)
            return
        if y==n-k:
            #we have rejected enough elements, take all remaining elements
            a.extend(seq[x+y:])
            v.append(a)
            return
        if (x<k):
            #take element seq[x+y]
            h=a+[seq[x+y]]
            f(x+1,y,h)
        if (y<n-k):
            #don't take element seq[x+y]
            f(x,y+1,a)            
    f(0,0,[])
    return v
There are probably better algorithms for this but I'm glad I was able to write at least a correct algorithm.

Last edited by Gilward Kukel; Jun 7th, 2005 at 1:47 PM.
Gilward Kukel is offline   Reply With Quote
Old Jun 7th, 2005, 1:06 PM   #2
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 4 uman is on a distinguished road
nice :-P
uman is offline   Reply With Quote
Old Jun 7th, 2005, 1:10 PM   #3
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 4 uman is on a distinguished road
now do it for permutations!
uman is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:57 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC