Question: USE PYTHON3; DO NOT IMPORT PACKAGES Please do both parts of the question; do not remove or add any parameters unless the problem asks Code
USE PYTHON3; DO NOT IMPORT PACKAGES
Please do both parts of the question; do not remove or add any parameters unless the problem asks



Code format in the text editor:
class Counter: """ # TODO: Add class description # """
def __init__(self): """ # TODO: Add method description # """ # YOUR CODE GOES HERE # self.nelems = ... self.counts = ...
def size(self): """ # TODO: Add method description # """ # YOUR CODE GOES HERE # return ...
def get_count(self, item): """ # TODO: Add method description # """ # YOUR CODE GOES HERE # return ...
def get_all_counts(self): """ # TODO: Add method description # """ # YOUR CODE GOES HERE # return ...
def add_items(self, items): """ # TODO: Add method description # """ # YOUR CODE GOES HERE #
class AlphanumericCounter(Counter): """ # TODO: Add class description # """
def __init__(self): """ # TODO: Add method description # """ # YOUR CODE GOES HERE #
def get_index(self, item): """ # TODO: Add method description # """ # YOUR CODE GOES HERE # return ...
def get_char(self, idx): """ # TODO: Add method description # """ # YOUR CODE GOES HERE # return ...
def get_count(self, item): """ # TODO: Add method description # """ # YOUR CODE GOES HERE # return ...
def get_all_counts(self): """ # TODO: Add method description # """ # YOUR CODE GOES HERE # return ...
def add_items(self, items): """ # TODO: Add method description # """ # YOUR CODE GOES HERE #
In this question, you will implement a counter for iterable objects. A counter counts and stores the occurrences of provided items, and it allows you to query the count of a specific item in the counter. All doctests for this question are in the function counter_doctests(). You need to initialize 1 more instance of each class as constructor tests, and add 3 more doctests for each method you implement. Part 1: Counter The Counter class abstracts a generalized counter for all kinds of iterable objects. In the constructor _init_), you need to initialize the following instance attributes: nelems (int): The total number of items stored in the counter. Initialize it with 0. counts (dict): A dictionary that stores all item (keys) to count (values) pairs. Initialize it with an empty dictionary. Then, implement the following 4 methods: size(self): Returns the total number of items stored in the counter. get_count(self, item): Returns the count of an object item. If the item does not exist in the counter, return O. get_all_counts(self): Returns a dictionary of all item to count pairs. add_items(self, items): Takes an iterable object (like list) of objects (items) and adds them to the counter. Make sure to update both counts and nelems attributes. Note: For the item argument and each object in the items argument, you can assume that they can be used as a dictionary key. All immutable objects and a tuple of immutable objects would qualify as valid dictionary keys. However, mutable objects (such as list) are not valid keys, and you can assume they will not appear as items to count. If you attempt to use a mutable object as a key of dictionary, the following error will occur: >>> counts[[1]] Traceback (most recent call last): File "
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
