← 返回 nvidia 的题目列表Log Parser for Top N Items
类型:online_judge
Write a program to parse a given log file and count the occurrences of each item, then return the top N items based on their count. Assume each line in the log file represents an item, and all items are strings. Implement a function parse_log_and_find_top_n_items(log: List[str], n: int) -> List[str].
Input:
log is a list of strings, representing the log file, with each element being a record.
n is an integer, representing the number of top items to return.
Output: A list of strings sorted by frequency in descending order, containing the top N items.
Example
log = ["apple", "banana", "apple", "orange", "banana", "apple"]
n = 2
# Return ["apple", "banana"]
Constraints
The length of log is up to 10^6.
The length of each string does not exceed 100.
n is a positive integer that does not exceed the number of unique items in the log.
Example
Input
apple\nbanana\napple\norange\nbanana\napple\n2