This question evaluates proficiency in Python fundamentals and object-oriented inheritance, focusing on frequency aggregation using a precomputed dictionary, iteration over mappings, and correct handling of ties and empty inputs.
You are given two classes. Parent precomputes frequency counts of items (as strings) from an input list.
class Parent:
def __init__(self, l):
self.l = l
# set of all unique string forms
self.lf = set(str(s) for s in self.l)
# dict: key -> count
self.ldf = {}
for item in self.l:
s = str(item)
if s not in self.ldf:
self.ldf[s] = 1
else:
self.ldf[s] += 1
class Child(Parent):
def most_frequent_key(self):
"""Return the key(s) with the highest frequency.
Constraints:
- You may NOT use Python's built-in `max()`.
- Use the precomputed `self.ldf` from `Parent`.
Return:
- A list of the most frequent key(s) (strings). If there is a tie, include all tied keys.
- If the input list is empty, return an empty list.
"""
# TODO: implement
return output_list
queries = ['park', 'Park', 'McDonalds', 'apple', 'Apple Park', 'park', 'Park']
x = Child(queries)
print("most frequent:", x.most_frequent_key())
Notes/clarifications
str(item)
.
max()
).