Question: Write a recursive function countTuples () that takes an arbitrarily nested list as a parameter and returns the number of tuples in the list. Note

Write a recursive function countTuples() that takes an arbitrarily nested list as a parameter and returns the number of tuples in the list. Note that the list may contain any Python type, not just tuples. Recall that you can determine whether an item is a tuple by writing type(item) == tuple and whether an item is a list by writing type(item) == list. The function should not use any loops. The only list functions you are allowed to use are len(), indexing (lst[i] for an integer i), or slicing (lst[i:j] for integers i and j). If the list does not contain any tuples or is empty the function should return 0. The following shows several sample runs of the completed function:

Python 3.7.0 Shell File Edit She Debug Options Window Help >>> val = countTuples ([]) >>>val >>> val = countTuples ( [ (1, 2) , >>>val [ [ [ [ ' two' , True] , ( 3, 4) ] ] , 3. 14 15), [ [ ( 5, 6) , ' seven' ] ] ] ) >>> val= countTuples ( [ [ [ [ (1, 2)]]], >>>val [[[['two ' , True ] , (3, 4)11, 3.1415], [[(5, 6), 'seven ' ] ] ] ) >>> val = countTuples ( [ ' one ' , >>>val [ [ [ [ ' two ' , True ] , ( 3, 4) ] ] , 3 . 14 15), [ [ ( 5, 6), ' seven ' ] ] ] ) >>> val = countTuples ( [ {1:2), >>>val [ [ [ [ ' two ' , True ] , ( 3, 4) ] ] , 3 . 14 15), [ [ ( 5, 6), ' seven ' ] ] ] ) >>> val = countTuples ( [ {1:2), >>>val [ [ [ [ ' two", True ] , 3, 411 , 3 . 1415] , [ [ 5, 6, ' seven' ] ] ] ) Ln: 45 Col: 4

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!