Need help with a Python recursion problem: You must include appropriate doc stri
ID: 3796502 • Letter: N
Question
Need help with a Python recursion problem:
You must include appropriate doc strings (e.g. strings that appear on the line following the function header) to the class that clearly and concisely describe what the functions are doing. The functions written for this assignment must be recursive and must not use global variables. The functions may not use loops. In some cases certain built-in Python functions are disallowed. Function that are described as returning values should not do any printing. Please note that local variables are absolutely fine in any of the functions.
Write a recursive function recStrCount() that takes a one-dimensional list as a parameter and returns the number of strings that are found in the list. Recall that you can determine whether an item is a string by writing type(item) == str. 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 following shows several sample runs of the function:
Python 3.4.1 Shell File Edit Shell Debug Options Windows Help rec StrCount One 2, three A five 6, 7.5 8. 11 nine val recs trCount (11, 2, 3. 14.15, 4, 5.5, 6] val reestr Count Ln: 44 Col: 4Explanation / Answer
#!/usr/bin/env python
def recStrCount(n):
"""returns the count of the strings present in the list
Example: Finding the count of Strings in the list
['one',2,'three',4,'five',6,7.5,8.11,'nine']
So the total count of Strings present in this is 4
Similarly if list is []
then total count of Strings are 0
"""
if len(n) == 0:
return 0
elif type(n[0]) == str:
return 1 + recStrCount(n[1:])
else:
return 0 + recStrCount(n[1:])
print recStrCount(['one',2,'three',4,'five',6,7.5,8.11,'nine'])
print recStrCount([1,2,3.1415,4,5.5,6])
print recStrCount([])