Write a Python function count_bigger that takes two parameters, a nested list of objects and a threshold number. It returns an integer specifying how many of the objects anywhere in the nested list are numbers that are larger than the threshold. (For our purposes, "numbers" are either integers or floats.) There may be objects in the list other than numbers, in which case you would simply ignore them. Here are a couple of examples of how the function should behave when you're done:
>>> count_bigger([1, 2, 3, 4, 5, 6], 2.5) 4
>>> count_bigger(['boo', [3.0, 'is', 'perfect'], 'today', 5], 3) 1
def count_bigger(lst, threshold): if type(lst) == list: if len(lst) == 0: return 0 return count_bigger(lst[0], threshold) + count_bigger(lst[1:], threshold) elif type(lst) in [int, float] and lst > threshold: return 1 else: return 0 # Testing the function here. ignore/remove the code below if not required print(count_bigger([1, 2, 3, 4, 5, 6], 2.5)) print(count_bigger(['boo', [3.0, 'is', 'perfect'], 'today', 5], 3))
Get Answers For Free
Most questions answered within 1 hours.