您不能使用內置函式或切片或深度復制或復制,或 while 或 for
讓我們說這是我們的功能:
def list_occur(n):
它將 n 作為 int 引數輸入并回傳一個大小為 n 的串列,該串列顯示了該系列中的元素 n(輸出):
n = 0: returns []
n = 1: returns list with size 1 that takes in index=0 [], means returns [ [] ]
n = 2: returns list with size 2 that takes in index=0 [] and index = 1 [ [] ], means returns:
[ [], [ [] ] ]
n = 3: returns list with size 3 that takes in index=0 [], index =1 [ [] ], index = 2 [ [], [ [] ] ]
means its returns [ [], [ [] ], [ [], [ [] ] ] ]
我嘗試解決這個問題,但是當我嘗試 n = 2 時出現錯誤“AttributeError: 'NoneType' object has no attribute 'append':
def occur(n) :
return occr_helper(n)
def occr_helper(n) :
if (n == 0):
return []
return occr_helper(n-1).append(occr_helper(n-1))
if __name__ == "__main__":
print(occur(1))
uj5u.com熱心網友回復:
對于此函式n 1回傳n附加到n. 這可以很容易地遞回編碼:
num = 3
def occr(n):
if n == 0:
return []
else:
new_l = occr(n-1)
new_l.append(occr(n-1))
return new_l
print(occr(num))
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/375117.html
