Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Python, write a recursive function recStrBuild () that takes arbitrarily nest

ID: 3933649 • Letter: I

Question

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:

Explanation / Answer

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']]))

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote