0

Explain this code??

def fun(a, p=0): if(p==len(a)): return 0 z=1 if not a[p] else 0 return z + fun(a, p+1) a = [1,0,1,1,0,1,0] Print (fun(a)) #output 3

9th Jul 2020, 12:45 PM
Emon
1 Answer
0
Emon Your function calculates and print the number of times zero appears in your list a. Try to put more zeros by adding elements with value zero like this: a[1,0,0,4,2,0,0,0,0,0,5,3,6,9] And run it. Surprisingly it counts zeros and returns the number of times they are there Except when it is a number with zero like 10,450,30 etc
9th Jul 2020, 11:46 PM
Aphiwe Ayanda Khumalo