PYTHON Solution must be recursive. You are not allowed to use loops, or use the
ID: 3792961 • Letter: P
Question
PYTHON
Solution must be recursive. You are not allowed to use loops, or use the python "in" operator.
Write a function called number_of_lists. It is passed a parameter x, that may or may not be a list. If is x is a list it may contain other listts. The function should return the total number of lists in x.
def number_of_lists(x):
return 0
>>> number_of_lists('abc')
0
>>> number_of_lists([ ])
1
>>> number_of_lists([[1,2,3]])
2
>>> number_of_lists([1,[3,1,2],[[3,6,2],[6,1,0,6]]])
Explanation / Answer
def number_of_lists(li):
return 1 + sum(map(number_of_lists, li)) if isinstance(li, list) else 0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.