Write a recursive function recFloatCount () that takes an arbitrarily nested lis
ID: 3672848 • 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 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 completed function:
Python 3.4.1 Shell Eile Edit Shell Debug Qptions Windows Help >recFloatCount () 0 val recFloatCount ([3.14, 1, 'one, (4.5, 1), 'tuo'1) >>> val recFloatCount (tttt3.14], 'one', [4.5, 2]1, t017.1, 3.5], >>> recFloatCount ( [4. 51, [ [ [ [ 1 , 17, 3), one'1 , tuo' ), 3 . 14 15), 18] ) Ln: 23 Col:4Explanation / Answer
#If raw_input is not working then please use input()
def recFloatCount(data):
if(type(data)==list):
if(len(data)==0):
return 0
elif(type(data[0])==float):
return 1 + recFloatCount(data[1:])
elif(type(data[0])==list):
return recFloatCount(data[0]) + recFloatCount(data[1:])
else:
return 0 + recFloatCount(data[1:])
else:
return 0
if __name__ == "__main__":
print recFloatCount([])
print recFloatCount([3.14,1,'one',(4.5,1),'two'])
print recFloatCount([[[[3.14], 'one'], [4.5, 2]], [[17.1, 3.5], 'two']])
print recFloatCount([4.51, [[[[1.17, 3], 'one'], 'two'], 3.145], 18])
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.