In Python write a recursive function recStrBuild that takes

In Python, write a recursive function recStrBuild() that takes arbitrarily nested list as a parameter and returns the concatenation of all the strings found in the list. If the list does not contain any strings or is empty the function should return the empty string. The function must not alter the list passed as a parameter. Recall that you can determine whether an item is a string by writing type(item) == str and you can determine if an item is a list by writing type(item) == list. 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). The function may not use any loops. The function must return the concatenated string and not print it. The following shows some sample runs of the function:

Solution

def recStrBuild(list):
result = \'\'
for item in list:
if type(item) is str:
result = result + item
if type(item) is list:
print(type(item))
result = result + recStrBuild(item)
return result

print(recStrBuild([[\'test\',1,2],[[[[[\'Djengo\']],\'cat\']],3.14,\'WIN\']]))

In Python, write a recursive function recStrBuild() that takes arbitrarily nested list as a parameter and returns the concatenation of all the strings found in

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site