Write a recursive function recFloatCount() that takes an arbitrarily nested list
ID: 3840378 • Letter: W
Question
Write a recursive function recFloatCount() that takes an arbitrarily nested list as a parameter and returns the number of floating point values that are found in the list. Recall that you can determine whether an item is a floating point value by writing type(item) = float 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 1en(): indexing (1st [1] for an integer 1). or slicing (1st[i:j] for integers 1 and j). The following shows several sample runs of the completed function: >>> recFloatCount([]) 0 >>> val = recFloatCount([3.14, 1, 'one', (4.5, 1), 'two']) >>> val 1 >>>recFloatCount([[[[3.14], 'one'], [4.5, 2]], [[17.1, 3.5], 'two']]) 4 >>> recFloatCount([4.51, [[[[1.17, 3], 'one'], 'two'], 3.1415], 18]) 3 >>>Explanation / Answer
def recFloatCount(x):
count = 0;
if type(x) == list:
for n in x:
if type(n) == float:
count += 1
elif type(n) == list:
count += recFloatCount(n)
return count
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.