某厂算法,test以后有非必现的bug,算法是求一组排序数的最大差值,算法O(n)
存在值在区间不大时候出现bug的问题,还没修改
代码if那一段可以优化
def suanfa(tlist):
tongNum = len(tlist) + 1
tongList = []
for i in range(tongNum):
tongList.append(tong())
def getLR(tlist):
maxNum , minNum = tlist[0],tlist[0]
for i in tlist:
if i < minNum or i == minNum:
minNum = i
if i > maxNum or i == maxNum:
maxNum = i
return maxNum, minNum
tlist = tlist
maxNum, minNum = getLR(tlist)
tongList[0].min,tongList[-1].max = minNum, maxNum
tlist.remove(maxNum)
tlist.remove(minNum)
bou = (maxNum - minNum) // tongNum
for i in tlist:
if i == minNum or i < minNum + bou:
if tongList[0].max == 'null':
tongList[0].max = i
elif not tongList[0].max == 'null':
if i > tongList[0].max:
tongList[0].max = i
if (i == minNum + bou * 1 or i > minNum + bou * 1) and i < minNum * 2:
if tongList[1].max == 'null':
tongList[1].max = i
elif tongList[1].min == 'null':
tongList[1].min = i
if tongList[1].min > tongList[1].max:
tongList[1].max, tongList[1].min = tongList[1].min, tongList[1].max
else:
if i < tongList[1].min:
tongList[1].min = i
elif i > tongList[1].max:
tongList[1].max = i
if (i == minNum + bou * 2 or i > minNum + bou * 2) and i < minNum * 3:
if tongList[2].max == 'null':
tongList[2].max = i
elif tongList[2].min == 'null':
tongList[2].min = i
if tongList[2].min > tongList[2].max:
tongList[2].max, tongList[2].min = tongList[2].min, tongList[2].max
else:
if i < tongList[2].min:
tongList[2].min = i
elif i > tongList[2].max:
tongList[2].max = i
if (i == minNum + bou * 3 or i > minNum + bou * 3) and i < minNum * 4:
if tongList[3].max == 'null':
tongList[3].max = i
elif tongList[3].min == 'null':
tongList[3].min = i
if tongList[3].min > tongList[3].max:
tongList[3].max, tongList[3].min = tongList[3].min, tongList[3].max
else:
if i < tongList[3].min:
tongList[3].min = i
elif i > tongList[3].max:
tongList[3].max = i
if (i == minNum + bou * 4 or i > minNum + bou * 4) and i < minNum * 5:
if tongList[0].min == 'null':
tongList[0].min = i
elif not tongList[0].min == 'null':
if i > tongList[0].min:
tongList[0].min = i
kongtong = []
for i in tongList:
if i.max == 'null' and i.min == 'null':
num = tongList.index(i)
kongtong.append(tongList[num])
res = []
for i in kongtong:
num = tongList.index(i)
maxromin = tongList[num + 1].min if not tongList[num + 1].min == 'null' else tongList[num + 1].max
re = maxromin - tongList[num - 1].max
res.append(re)
re = res[0]
for i in res:
if len(res) == 1:
return i
else:
if re < i:
re = i
return re