Question
Python inheritance print(' Testing popdict') d = popdict([('a',100)],b=200,c=300) print('initial') print(d) print([(k,d(k)) for k in 'abcx']) d['a'] d['b'] d['b'] d['a'] = 103 d['a'] += 1 #
Python inheritance
print(' Testing popdict') d = popdict([('a',100)],b=200,c=300) print('initial') print(d) print([(k,d(k)) for k in 'abcx']) d['a'] d['b'] d['b'] d['a'] = 103 d['a'] += 1 # accesses d['a'] 2 times: to get value and store value d['c'] += 5 # accesses d['c'] 2 times: to get value and store value d['c'] += 1 # accesses d['c'] 2 times: to get value and store value d['c'] += 1 # accesses d['c'] 2 times: to get value and store value try: d['x'] # should raise exception: print('Did not raise exception') except: pass print(' after some updates') print(d) print([(k,d(k)) for k in 'abcx']) print(' iteration order =',[k for k in d]) del d['a'] print(' after delete') print(d) print([(k,d(k)) for k in 'abcx'])
d.clear() print(' after clear') print(d) print([(k,d(k)) for k in 'abcx'])
6. (7 pts) Define a class named popdict, derived from the dict class; in addition to being a dictionary, it remembers how often each key is accessed (how popular it is), and iterates through the popdict in decreasing order of how frequently the key was accessed. Besides storing the standard dictionary of keys and their values, a popdict stores an auxiliary dictionary remembering how often each key has been accessed The keys in the actual dictionary should always be the same as the keys in the auxiliary dictionary. See the notes for how pdefaultdict is derived from dict; this derivation is similar. Define the derived class popdict with only the following methods .__init__ (self,initial dict-[l,**kargs): initializes the dictionary and also creates an auxiliary popularity dictionary (I used a defaultdict) for remembering how often each key is accessed: initialize this popularity dictionary so that it shows each key in the newly initialized dictionary as being used once so far .getitem_ (self,key): for any key in the dictionary, return its associated value and increase its popularity (how often it has been used) by 1 in the popularity dictionary ._setitem (self,key , value): set key to associate with value in the dictionary and increase its popularity (how often it has been used) by 1 in the popularity dictionary delitem (self,key): for any key in the dictionary, remove it from both the dictionary and popularity dictionary .__call_(self,key): returns the popularity of key (the number of times it has been used), for a key not in the dictionaries, return 0 clear (self): remove all keys (and their associated values) from both dictionaries _iter-(self): return a generator that will yield all the keys in order of decreasing popularity
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started