Looking at the class below I need to write a code to define "character frequency" so it returns something like the examples below.
class EnhancedString:
def __init__(self, contents):
self._contents = str(contents)
def __str__(self):
return str(self._contents)
def __len__(self):
return len(self._contents)
def set_contents(self, contents):
self._contents = str(contents)
def get_contents(self):
return self._contents
**def character_frequency(self):**
"""
Returns a dictionary containing all characters found in _contents as keys, with integer values corresponding to the number of occurrences of each character. Use a while loop to traverse _contents.
:return: A dictionary with characters as keys and integers as values
Example use:
>>> c = EnhancedString("")
>>> print(c.character_frequency())
{}
>>> a = EnhancedString("abcdefg")
>>> print(a.character_frequency())
{'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 1, 'f': 1, 'g': 1} >>> b = EnhancedString("aabcdefgaa")
>>> print(b.character_frequency())
{'a': 4, 'b': 1, 'c': 1, 'd': 1, 'e': 1, 'f': 1, 'g': 1} """