You cannot use inbuilt functions or slicing or deepcopy or copy, or while or for
Lets say this is our function:
def list_occur(n):
it takes n as int parameter input and returns a list with size n that shows the element n in this series ( output ):
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 [ [], [ [] ], [ [], [ [] ] ] ]
my try to solve this problem, but i get error "AttributeError: 'NoneType' object has no attribute 'append' when i try n = 2 :
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))
CodePudding user response:
For this function n 1
returns n
appended to n
. This can easily be coded recursively:
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))