How can I write this function Recursively:
def count(lst, value):
c = 0
for i in range(lst.size):
if get(lst,i) == value:# get(lst, i) is predefined. It gives me the value at i in lst
c = c + 1
return c
This should make the trick:
def recount(lst, value):
if len(lst) == 0:
return 0
if get(lst,0) == value:
return 1 + recount(lst[1:],value)
else:
return recount(lst[1:], value)
return int(get(lst, 0) == value) + recount(lst[1:], value)
for i in range()is unusual, as is theget(lst, i).get(lst, i), I would just suggestlist[i]