views:

58

answers:

3

I want to save the results of my function binomal_aux to a tuple but I don't have an idea how to, here is my code I have right now.

def binomal (n):    
    i=0
    for i in range(n):
        binomal_aux(n,i) #want this to be in a tuple so, binomal (2) = (1,2,1)
    return

def binomal_aux (n,k):
    if (k==0):
        return 1
    elif (n==k):
        return 1
    else:
        return (binomal_aux(n-1,k) + binomal_aux(n-1,k-1))
A: 
def binomal (n):    
    return tuple(binomal_aux(n,i) for i in range(n+1))
gnibbler
changed return tuple(binomal_aux(n,i) for i in range(n+1))and works perfectly thanks a bunch!
Mike
+2  A: 

In your binomal function, just make the tuple you want to return.

def binomal(n):
  return tuple(binomal_aux(n, i) for i in range(n+1))

Note also that the correct spelling is binomial.

Alex Martelli
A: 

Alternate way:

def binomal(n): 
    from itertools import combinations
    return tuple(len(list(combinations(range(n), r=t))) for t in range(n + 1))
RC