← Back to Recursion
Question 583
Count The Target
Count The Target
Complete count_occurrences(values: list[int], target: int) -> int so it returns how many times target appears in values, by recursion. The base case is written: an empty list holds 0 matches. Add the recursive case: when the first value equals target, return 1 plus the matches in the rest of the list (values[1:]); otherwise return just the matches in the rest. Do NOT write a main block or any top-level test code; the tests construct the calls. Input: two lines, the list as integers separated by spaces (it may be empty) and then the target, which the tests read and pass to your function before printing the value it returns.
Expected Output:
3= newline. The output ends with a newline — print one after the last line.
Hints3 available
Sign in to use hintsTopics:
RecursionSlicing
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Ctrl+Space to expand shortcuts (pr, inp, def)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.