[Huawei OD Machine Test 2023 Q1 Exam Questions Paper A] Top 5 Outstanding Student Statistics

topic

        A certain department of the company is organizing daily check-in learning activities for new employees. They have carried out this learning activity for a month, and now they want to count the excellent check-in employees this month.

Each employee will correspond to an id,

The daily clock-in record records the id collection of the clock-in employees on that day, a total of 30 days.


        Please use the code to help count the top 5 employees who clocked in :

1. If the number of check-in times is the same, the employee who participated in the check-in earlier will be ranked first.

2. If the time to start clocking in is still the same, the employee with the smaller id will be ranked first.
3. Regardless of the parallel situation, just return the IDs of the top 5 employees according to the rules.

4. If there are less than 5 employees who clocked in in the current month, return all employee ids who have clocked in according to the rules.

        enter 

1. The input in the first line is the number of new employees N, indicating that the new employee number id is from 0 to N-1, and the range of N is [1,100] 2. The input in the second line is 30
integers, indicating the number of employees who clock in every day. At least one employee punches in
3, and the next 30 rows are a collection of employee ids who punch in every day, and the ids will not be repeated

        output 

Output the ids of the top5 employees who clock in in sequence, separated by spaces.

Remarks
There is no difference in time for employees who clock in on the same day.

There is no guarantee that all employees will clock in.

The ranking is only for employees with clock-in records.

        example one 

Input
11
4 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 0 1 7 10 0 1 6
10
10
10
10 10
10
10
10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 6 10 7 10 Output 10 0 1 7 6 1 indicates that the employee number range is 0-10, and the employee with id 10 has punched in for 30 consecutive days, ranking first . Employees with ids 0, 1, 6, and 7 clock in on two days. Employees with ids 0, 1, and 7 clock in on the first day, which is earlier than employees with id 6, and they are in the front. 0, 1, 7 Sort by id in ascending order, so output [10,0,1,7,6]   





























The implementation code is as follows
# Time:2023 2023/6/26 9:48
# Author: Jasmay
# -*- coding: utf-8 -*-


from collections import defaultdict

def solve_method(res,n):
    dic = defaultdict(int)
    dic_index = {}
    for i,r in enumerate(res):
        for s in r:
            dic[s] += 1
            if s not in dic_index:
                dic_index[s] = i
        result = []
    for k,v in dic_index.items():
        result.append([k,dic.get(k),v])

    #排序
    result = sorted(result,key=lambda x:(-x[1],x[2],x[0]))
    h = []
    for i in result:
        h.append(i[0])
    return h[:5]

if __name__ == "__main__":
    n = int(input().strip())
    nums = list(map(int,input().split()))
    res = []
    for _ in range(len(nums)):
        res.append(list(map(int,input().split())))
    print(*solve_method(res,n))

 

Guess you like

Origin blog.csdn.net/Moonlight_16/article/details/131390747