Problem 1: Count (100pts)
Implement count
, which takes in an iterator t
and returns the number of times the value x
appears in the first n
elements of t
.
A value appears in a sequence of elements if it is equal to an entry in the sequence.
Note: You can assume that
t
will have at leastn
elements.
def count(t, n, x):
"""Return the number of times that x appears in the first n elements of iterator t.
>>> s = iter([10, 9, 10, 9, 9, 10, 8, 8, 8, 7])
>>> count(s, 10, 9)
3
>>> s2 = iter([10, 9, 10, 9, 9, 10, 8, 8, 8, 7])
>>> count(s2, 3, 10)
2
>>> s = iter([3, 2, 2, 2, 1, 2, 1, 4, 4, 5, 5, 5])
>>> count(s, 1, 3)
1
>>> count(s, 4, 2)
3
>>> next(s)
2
>>> s2 = iter([4, 1, 6, 6, 7, 7, 8, 8, 2, 2, 2, 5])
>>> count(s2, 6, 6)
2
"""
"*** YOUR CODE HERE ***"