Count elements
Create a function that takes a vector of numbers and a target value as input and returns the count of occurrences of the target value in the vector.
Here, a vector is a one-dimensional Python list of numbers. Count values equal to the target using ==; an empty list returns 0. Keep the input unchanged.
Complete the supplied function in Listing 1. Keep its signature and use the checks below it.
def count_elements(vector: list[float], target: float) -> int:
# Add code here!
pass
def test_count_elements() -> None:
# Input list 1
vector1 = [1, 2, 3, 2, 4, 2]
# Input list 2
vector2 = [1, 2, 3, 4, 5]
assert count_elements(vector1, 2) == 3
assert count_elements(vector2, 6) == 0
test_count_elements()
TipHint
len(values) returns the number of elements, so len([3, 7]) is 2 and len([]) is 0. List indexing starts at 0: [3, 7][0] is 3. An index must refer to an existing element.
assert condition stops the check with an AssertionError when the condition is false.