Discuss / Python / 第y层x个的数用递归得出来,但是对于题意不太好效率低

第y层x个的数用递归得出来,但是对于题意不太好效率低

Topic source

# y行 x列

#                           |-  fun(y-1,x-1) + fun(y-1,x)  ,其他    

#            fun(y,x) =|             

#                           |-           1                           ,x = y || x == 1

def t(y,x):

    # 左边右边为1

    if x <= 1 or x == y:

        return 1

    return t(y-1,x)+t(y-1,x-1)

def triangles():

    for i in range(1,11):

        L = []

        for x in range(1,i+1):

            num = t(i,x)

            L.append(num)

        yield L 

    return "Done"

def t(y,x): 
    pass

#要改成另外一个函数名字 可能跟测试代码的t 有冲突
#否则报错  TypeError: 'list' object is not callable while trying to access a list

  • 1

Reply