# 360 enterprise safety written test

## First question

Finding the greatest common divisor of a series

```def gys(a):
res_a=[]
for i in range(1,a+1):
if a%i==0:
res_a.append(i)
return res_a
def maxgys(a,b):
res_a=gys(a)
res_b=gys(b)
res=1
for i in res_a:
if i in res_b:
res=max(res,i)
return res
def findmax(newlist):
if len(newlist)==2:
return maxgys(newlist[0],newlist[1])
a=newlist[0]
b=newlist[1]
return findmax([maxgys(a,b)]+newlist[2:])
```

## Second questions

Given a two-dimensional array, output the maximum ascending sequence of two-dimensional array
Input
[

​ [9,9,4],

​ [8,6,9],

​ [2,1,1]

]

Output:
4 note:[1,2,8,9]

```def tj(newlist):
hang=len(newlist)
lie=len(newlist[0])
res={}
for i in range(hang):
for j in range(lie):
if newlist[i][j] not in res:
res[newlist[i][j]]=[[i,j]]
else:
res[newlist[i][j]].append([i,j])
return res

def maxzxl(newlist):
tongji=tj(newlist)
values=sorted(list(tongji.keys()))
hang=len(newlist)
lie=len(newlist[0])
mod_newlist=[[-10e9]*(lie+2)]+[[-10e9]+i+[-10e9] for i in newlist]+[[-10e9]*(lie+2)]
res=[[0 for i in range(lie+2)] for j in range(hang+2)]
while len(values)>0:
value=values.pop()
valuelist=tongji[value]
for ind in valuelist:
i=ind[0]
j=ind[1]
maxsize=0
if mod_newlist[i][j+1]>mod_newlist[i+1][j+1]:
maxsize=max(maxsize,res[i][j+1])
if mod_newlist[i+2][j+1]>mod_newlist[i+1][j+1]:
maxsize=max(maxsize,res[i+2][j+1])
if mod_newlist[i+1][j]>mod_newlist[i+1][j+1]:
maxsize=max(maxsize,res[i+1][j])
if mod_newlist[i+1][j+2]>mod_newlist[i+1][j+1]:
maxsize=max(maxsize,res[i+1][j+2])
res[i+1][j+1]=maxsize+1
return max(tj(res).keys())
```

# Three questions of Huawei's written examination

## First question:

Input an integer sequence, 6 elements, and ask the maximum time that can be composed. If it can't be composed, input invalid

```def findk(sortnewlist,k):
for i in range(len(sortnewlist)-1):
if sortnewlist[i]<=k and sortnewlist[i+1]>k:
return i
if sortnewlist[-1]<=k:
return len(sortnewlist)-1
else:
return None
def maxtime(newlist):
newlist.sort()
if newlist[0]>2:
return 'invalid'
hour_shi_index=findk(newlist,2)
if hour_shi_index is None:
return 'invalid'
hour_shi=newlist[hour_shi_index]
newlist = newlist[:hour_shi_index] + newlist[hour_shi_index+1:]
if hour_shi==2:
hour_ge_index=findk(newlist,3)
if hour_ge_index is None:
return 'invalid'
hour_ge=newlist[hour_ge_index]
res=str(hour_shi)+str(hour_ge)+':'
newlist=newlist[:hour_ge_index]+newlist[hour_ge_index+1:]
else:
res=str(hour_shi)+str(newlist[-1])+':'
newlist=newlist[:-1]
minute_shi_index=findk(newlist,5)
if minute_shi_index is None:
return 'invalid'
minute_shi=newlist[minute_shi_index]
newlist=newlist[:minute_shi_index]+newlist[minute_shi_index+1:]
res=res+str(minute_shi)+str(newlist[-1])+':'
newlist=newlist[:-1]
minute_shi_index = findk(newlist, 5)
if minute_shi_index is None:
return 'invalid'
minute_shi = newlist[minute_shi_index]
newlist = newlist[:minute_shi_index] + newlist[minute_shi_index + 1:]
res = res + str(minute_shi) + str(newlist[-1])
newlist = newlist[:-1]
if len(newlist)!=0:
return 'invalid'
return res
```

## Second question:

Xiaoming has some spare money in his hand. He does a little fruit business. m stands for the cost price of fruit, n stands for the selling price, and the cost is k yuan. How much can he earn at the end?
eg:
m = [4, 2, 6, 4]
n = [5, 3, 11, 7]
k=15
Buy 0, 1, 2 fruits first, then sell, finally buy the last fruit, sell
Idea: for the knapsack problem, give the optimal strategy, then delete these elements from m, delete these elements from n, k represents the final price, recalculate the optimal until the two are equal

```"""
//Basic knapsack problem, output route strategy to be selected
"""
res=[[0 for i in range(k+1)] for j in range(len(weight)+1)]
road=[[[] for i in range(k+1)] for j in range(len(weight)+1)]
for i in range(1,len(weight)+1):
for j in range(1,k+1):
if j>=weight[i-1]:
if res[i-1][j]>=res[i-1][j-weight[i-1]]+value[i-1]:
res[i][j]=res[i-1][j]
else:
res[i][j]=res[i-1][j-weight[i-1]]+value[i-1]
else:
res[i][j]=res[i-1][j]
def delete(newlist,ind_list):
res=[]
for ind,val in enumerate(newlist):
if ind not in ind_list:
res.append(val)
return res
def findmax(weight,value,k):
weight=delete(weight,resr)
value=delete(value,resr)
while secondk>resk and len(weight)!=0:
resk=secondk
weight=delete(weight,resr)
value=delete(value,resr)
return resk
```

## Third questions

```def calctime(m,n,time):
if m>n:
return max(time)
else:
sumtime=0
time.sort()
time.reverse()
process=time[-m:]
time=time[:-m]
while len(time)>0:
need=time.pop()
minut=min(process)
process=[i-minut for i in process]
sumtime+=minut
for i in range(len(process)):
if process[i]==0:
process[i]=need
break
sumtime+=max(process)
return sumtime
```

Posted on Sat, 09 Nov 2019 11:59:26 -0500 by WebMonkey