from random import randrange
def countitems(item, lst):
'''
Purpose: Count the number of occurrences of elt in lst.
Pre: lst is a list, and item is any value.
Post: Returns the number of elements of lst that are
equal to item.
'''
result = 0
for elt in lst:
if elt == item:
result += 1
return result
def main():
'''Driver program for testing countitems.'''
# Random list of integers
intlist = [ ]
for i in range(20):
intlist.append(randrange(10))
print("Integer test list:", intlist)
okay = True
# Going up to 10 because the values in the list only go up to 9.
# That way we know the last answer should be zero.
for i in range(11):
mycount = countitems(i, intlist)
official = intlist.count(i)
if mycount != official:
print("Incorrect count", mycount, "for", i,
", should be", official)
okay = False
else:
print("Correct count", mycount, "for", i)
if okay:
print("All tests passed successfully.")
main()