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

USE PYTHON3; DO NOT IMPORT PACKAGES Please do both parts of thequestion; do not remove or add any parameters unless the problem asksCode format in the text editor: class Counter: """ # TODO: Add

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 "", line 1, in TypeError: unhashable type: 'list' Part 2: Alphanumeric Counter In this part, we will build a counter for only Alphanumeric characters in strings. When we need to count the characters in a string, we could further optimize the counter in terms of time and space by replacing the dictionary with a counter list, where each index represents an item, and the value at this index is the count of this item. To use a list as a counter, we need to figure out a way to convert characters to integer indices, so that we could use the indices to query the list. Luckily, we already have such a conversion rule available in our system. Since computers can only understand numbers, each character is internally mapped to a non-negative integer. The most basic conversion rule is ASCII, which provides mappings to alphanumeric characters and some special characters. You can refer to the ASCII table for the specific mapping. In Python, we could perform the ASCII mapping through built-in functions ord) and chro). The ord() function takes a character and converts it to its integer representation, and the chr() function does the opposite. For example: >>> ord('0') >>> ord('A') >>> ord('a') 65 >>> chr(48) >>> chr(65) >>> chr(97) '0' 'A' 'a' 48 97 After obtaining this mapping, we can start to build our counter list for the alphanumeric characters. The list will be of length 10 + 26 + 26 = 62, which will store digits (0-9), lowercase letters (a-z) and uppercase letters (A-2). Specifically, the mappings between each index and the character it represents are as follows: index 0 1 10 35 36 37 61 char o '1' '9' 'z' 'A 'B' 'Z 'al 'b' ... To use this counter list (let's call it counts), if we want to query the count of character 'b', we first need to convert it to index 11, then retrieve counts[11]. For this class, you must use ord() and chr() for the conversion. You cannot hardcode the entire mapping using list, dictionary, string, or if-elif-else statements. In the constructor __init_), you need to initialize the following instance attributes (you can reuse Counter's constructor): nelems (int): The total number of items stored in the counter. Initialize it with o. counts (list): A counter list of length 62 as described above. Since this counter performs a conversion between characters and indices to store and query the count, you need to implement the following helper methods: get_index(self, item): Given an item, return its corresponding index (0-9 for digits, 10-35 for lowercase letters, 36-61 for uppercase letters). If the item is not an alphanumeric character, return -1. get_char(self, index): Given an index (0-61), return the corresponding character. You can use the built-in functions str.isnumeric ), str.islower and str.isupper() to distinguish different kinds of characters. Then, overwrite the following methods: get_count(self, item): Returns the count of a character 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. You need to build the dictionary by iterating through the counts list and add all pairs with non-zero counts to the new dictionary. The dictionary should have characters as keys (instead of indices) and counts as values. add_items(self, items): Takes a string items and adds each character to the counter. Note that you should not add non-alphanumeric characters to this counter. Make sure to update both counts and nelems attributes

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!