Question: This question is about OBJECT-ORIENTED PROGRAMMING. Write a Python class named Histogram that keeps track of how many times each unique element or category in



This question is about OBJECT-ORIENTED PROGRAMMING. Write a Python class named Histogram that keeps track of how many times each unique element or category in a given sequence occurs. You must implement this class from scratch. That is your implementation cannot use the dict, collections.defaultdict, or collections.Counter data types anywhere for any purpose whatsoever! If you do, unfortunately, you will automatically receive a zero for this exam! In addition, your implementation cannot also use any import statements anywhere as well! If you do, unfortunately, you will automatically receive a zero for this exam! WARNING! If your code violates any of these two restrictions, your grade for this entire exam will automatically be zero! The Histogram class will have the following public interface: Histogram( sequence ) will initialize a histogram of all individual elements or categories occurring in the provided sequence. get( category, default=None ) will return the frequency, that is, how many times a given element or category occurs in the given histogram. For example, >>> h = Histogram( 'aaabbbbc' ) >>> h.get('b') 4 If the key or category name provided to get does not occur in the histogram, then get() should return the value provided as default in the default argument. If no default argument is provided, get() should return None for a non-occurring key. For example, >>> h.get('s', 5) 5 since character s does not occur in the input string. However, >>> h.get('s' ) None most_freq(n=5 ) should return the most frequently-occurring elements in the given sequence. If n=1 , then the most frequent category value should be returned in a 2-tuple along with its frequency value. For example, >>> h.most_freq(n=1 ) ('b', 4) If n is larger than 1, then the function should return an ordered list of (key, value) tuples of the top n most frequent values, starting with the highest occurring category entry. For example, >>> h. most_freq(n=2) [('b', 4), ('a', 3)] If n is larger than the number of elements, then this method should return all elements and their corresponding values. For example, >>> h.most frequent( 100 ) [('b', 4), ('a', 3), ('c', 1)] Note that the default value of n is 5. . frequencies() should return the frequency values in the histogram in a list. The order of the returned values is immaterial. However, the order of the frequenc values should match that returned from categories(). See below. categories() should return a list of categories in the histogram, akin to the keys in a dictionary. data() should return a list of all the data in the histogram as category, frequency) two-tuples, in which the first item is the category and second item is the frequency of that category. Note that the order of the returned (category, frequency) pairs is not important. update sequence ) should update the existing histogram data with the category data in the provided sequence. This method does not have an explicit return value. For example, >>> h = Histogram( "aaabbc" ) >>> h.data() [('a': 3), ('b', 2), ('c': 1)] >>> h.update( "fcfcfcfdfeefeee" ) >>> h.data) [('a', 3), ('b', 2), ('C', 4), ('f', 6), ('d', 1), ('e', 5)] >>> h.most_freq [('f', 6), ('e', 5), ('c', 4), ('a', 3), ('b', 2)] delete( category ) should remove the indicated category from the histogram and return the frequency value for that category. If the specified category is not present in the histogram, the delete() method should raise a keyError . See the test cases below. For example, >>> h = Histogram( "aaabbc" ) >>> h.delete( 'b') >>> h.data) [('a', 3), ('c', 1)] >>> h.delete( 'k') KeyError: "No such category: 'k'" VERY IMPORTANT You must solve this question without using any external modules, that is, without any import statements! If you do, you will automatically get a zero for this question. Make sure you do not remove the file magic command, %%file histogram.py, at the top of the code cell below. If you remove or modify this command, the tests below will fail, and your grade will be affected. Make sure that running the code cell below creates a file called histogram.py in the same directory as this notebook. The rest will be handled by the testing code. DONDE VON ANIMATED IN TUTULMATINICODECENT [ ]: %%file histogram.py # YOUR CODE HERE pass class Histogram: SEE ABOVE SPECIFICATIONS # YOUR CODE HERE pass
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
