Returning binomal as a tuple
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))
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 binom
ial
.
def binomal (n):
return tuple(binomal_aux(n,i) for i in range(n+1))
Alternate way:
def binomal(n):
from itertools import combinations
return tuple(len(list(combinations(range(n), r=t))) for t in range(n + 1))
精彩评论