[Huawei OD test questions] microservice integration test (python) 100% pass rate super detailed code comment code optimization

[Huawei OD machine test real questions 2022&2023] real test catalog @click here @
[Huawei OD machine test real questions] signal transmission and reception & trial reading & @click here @
[Huawei OD machine real test questions] rent a car to ride the greenway & trial reading & @点here@

Integration testing for microservices

Deep search of knowledge points
Time limit: 1s Space limit: 256MB Limited language: unlimited

Title description:

Now there are n container services, and the startup of the service may have certain dependencies (some services have no dependencies), and secondly, the startup and loading of the service itself will consume some time.
Give you an nxn two-dimensional matrix useTime, where useTime[i][i]=10 means that service i itself needs to consume 10s to start and load, useTime[i][j]=1 means that service i starts to depend on service j to start, useTime [i][k]=0, which means that service i does not depend on service k.
In fact, 0<=i, j, k<n. There is no circular dependency between services (there will be no loops). If you want to perform an integration test on any service i (the service itself needs to be loaded), ask for the minimum waiting time.

Enter a description:

The first line enters the total number of services n, and the next n lines represent the dependencies of service startup and the time it takes to start and load itself. Finally, enter k to indicate how long the calculation needs to wait before you can perform an integration test on service k. 1<=k<=n ,1<=n<=100

Output description:

Service k can be integrated tested after the minimum waiting time (s)

Example 1

enter:

3
5 0 0
1 5 0
0 1 5
3

output:

15

illustrate:

Service 3 starts dependent service 2, and service 2 starts dependent service 1. Since services 1, 2, and 3 need to consume 5 seconds to load themselves, so 5+5+5=15, service 3 can be integrated after waiting for 15 seconds.
insert image description here

Example 2

enter:

3
5 0 0
1 10 1
1 0 11
2

output:

26

illustrate:

The startup of service 2 depends on service 1 and service 3. The startup of service 3 depends on service 1. The loading of services 1.2 and 3 takes 5s, 10s, and 11s. Therefore, 5+10+11=26s, and service 2 can be executed after waiting 26s. Integration Testing
insert image description here

Example 3

enter:

4
2 0 0 0
0 3 0 0
1 1 4 0
1 1 1 5
4

output:

12

illustrate:

The startup of service 3 depends on service 1 and service 2, the startup of service 4 needs to depend on services 1, 2, 3, and the loading of services 1, 2, 3, and 4 takes 2s, 3s, 4s, 5s, so 3+4+5=12s (Because service 1 and service 2 can be started at the same time), service 4 can be integrated tested after waiting for 12s
insert image description here

Example 4

enter:

5
1 0 0 0 0
0 2 0 0 0
1 1 3 0 0
1 1 0 4 0
0 0 1 1 5
5

output:

11

illustrate:

The start of service 3 depends on service 1 and service 2. The start of service 4 needs to depend on services 1 and 2. The start of service 5 needs to depend on services 3 and 5. The loading of services 1, 2, 3, 4, and 5 takes 1s, 2s, and 3s. 4s, 5s, so 2+4+5=11s (because service 1 and service 2 can be started at the same time, service 3 and service 4 can be started at the same time), after waiting 11s, service 5 can be integrated tested
insert image description here

Problem-solving ideas:

Obtain the start time of all services that the service that is finally started depends on by backtracking.
This question is relatively difficult, and the correct rate is only 8%.
As example 3:

4
2 0 0 0
0 3 0 0
1 1 4 0 
1 1 1 5
4

First traverse the fourth row, index=3, times3={}
when I=0, ints[3][0]==1, dependent service 1, times0={};
traverse the first row, because it does not depend on other services , so in the end time0={}, return 0+2;
when I=1, ints[3][1]==1, dependent service 2, times1={};
traverse the second line, because it does not depend on other services, So in the end time1={}, return 0+3;
when I=2, ints[3][2]==1, depend on service 3, times2={};
traverse the third line, because service 1 and service 2, So in the end time2={2, 3}, take the maximum value of 3, and return 3+4=7; after
the traversal is completed, the final times3={2, 3, 7}, take the maximum value of 7, and return 7+5=12;
Final result 12.

python code:

ints = []  # 定义一个空列表,用于存储输入的矩阵

def handle(k):
    times = []  # 定义一个空列表,用于存储从当前节点到叶子节点的最长路径
    for i in range(len(ints)):
        if k != i and ints[k][i] == 1:  # 如果当前节点与下一个节点相邻且未被访问过
            times.append(handle(i))  # 递归调用 handle 函数,将返回值添加到 times 列表中
    max_time = 0  # 定义一个变量,用于存储从当前节点到叶子节点的最长路径
    for time in times:
        max_time = max(max_time, time)  # 找到 times 列表中的最大值
    return max_time + ints[k][k]  # 返回从当前节点到叶子节点的最长路径加上当前节点的值

if __name__ == '__main__':
    n = int(input())  # 输入矩阵的大小
    for i in range(n):
        ints.append(list(map(int, input().split())))  # 输入矩阵的每一行,并将其转换为整数列表
    k = int(input())  # 输入起始节点的编号
    print(handle(k - 1))  # 调用 handle 函数,并输出结果

Guess you like

Origin blog.csdn.net/weixin_45541762/article/details/130911015
Recommended