You are given the following Python class skeletons. The Parent class precomputes:
-
self.l
: the original list
-
self.lf
: a
set
of the stringified items in the list
-
self.ldf
: a
dict[str, int]
mapping each stringified item to its frequency
class Parent:
def __init__(self, l):
self.l = l
self.lf = set(str(s) for s in self.l)
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()`.
- Do not use `collections.Counter`.
- Find the highest count manually (via iteration).
Output:
- Return a `list[str]` of all keys that are tied for the highest frequency.
- If multiple keys tie, return them in the order of first appearance in the original input list.
"""
# TODO: implement
return output_list
Example:
queries = ['park', 'Park', 'McDonalds', 'apple', 'Apple Park', 'park', 'Park']
x = Child(queries)
print("most frequent:", x.most_frequent_key())
Expected output:
most frequent: ['park', 'Park']
Implement Child.most_frequent_key().