华为OD机试 Python -测试用例执行计划
题目描述
某个产品当前迭代周期内有 N 个特性(F1,F2,…FN)需要进行覆盖测试,每个特性都被评估了对应的优先级,特性使用其 ID 作为下标进行标识。
设计了 M 个测试用例(T1,T2,…,TM),每个测试用例对应一个覆盖特性的集合,测试用例使用其 ID 作为下标进行标识,测试用例的优先级定义为其覆盖的特性的优先级之和。
在开展测试之前,需要制定测试用例的执行顺序,规则为:优先级大的用例先执行,如果存在优先级相同的用例,用例 ID 小的先执行。
输入描述
第一行输入为 N 和 M,之后 N 行表示特性 ID=1 到特性 ID=N 的优先级,再接下来 M 行表示测试用例 ID=1 到测试用例 ID=M 关联的特性的 ID 的列表。
输出描述
按照执行顺序(优先级从大到小)输出测试用例的 ID,每行一个ID。测试用例覆盖的 ID 不重复。
样例1
input
5 4
1
1
2
3
5
1 2 3
1 4
3 4 5
2 3 4
output
3
4
1
2
样例2
input
3 3
3
1
5
1 2 3
1 2 3
1 2 3
output
1
2
3
代码:python
def sort_test_cases(N, M, feature_priorities, test_cases):
# Calculate the priority of each test case
test_case_priorities = []
for i, case in enumerate(test_cases, start=1):
priority = sum(feature_priorities[f - 1] for f in case)
test_case_priorities.append((priority, i))
# Sort the test cases by priority, then by ID
test_case_priorities.sort(reverse=True, key=lambda x: (x[0], -x[1]))
# Output the sorted test case IDs
return [id for _, id in test_case_priorities]
N, M = map(int, input().split())
feature_priorities = [int(input()) for _ in range(N)]
test_cases = [list(map(int, input().split())) for _ in range(M)]
sorted_test_cases = sort_test_cases(N, M, feature_priorities, test_cases)
for test_case in sorted_test_cases:
print(test_case)