Problem Complete the function recursiveCount() to take in an array and a target
ID: 3719959 • Letter: P
Question
Problem
Complete the function recursiveCount() to take in an array and a target element and return the number of times this element appears in the array.
You must implement this using recursion :) You may also not slice the array (i.e. must use a helper).
Example
recursiveCount([1, 3, 4, 3, 2], 3) # 2
1- def recursiveCount(array, target): Problem Complete the function recursiveCount () to take in an array and a target element and return the number of times this element appears in the array You must implement this using recursion :) You may also not slice the array (i.e. must use a helper) Example recursiveCount ([1, 3, 4, 3, 2), 3) # 2Explanation / Answer
Solution:
code:
def recusiveCount(array, target):
count= 0
if len(array)== 1:
if array[0] == target:
return 1
else:
return 0
else:
return count+recursiveCount(array[1:], target)
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.