#coding=utf-8
import numpy as np
def subtract(data_list, reverse=True):
"""
reverse==True 后面减前面值最大
reverse==False 前面减后面的值最大"""
data_list = np.array(data_list)
i = 0
max_value = 0
for data in data_list:
if max_value < data:
max_value = data
max_index = i
i += 1
a = max_value*np.ones_like(data_list)
b = a-data_list
max_sub = np.min(b)
# print(b)
i = 0
for sub in b:
if reverse == True and i <= max_index and max_sub <= sub:
max_sub = sub
elif reverse == False and i >= max_index and max_sub <= sub:
max_sub = sub
i += 1
return max_sub
if __name__ == "__main__":
a = [1, 2, 3]
print(subtract(a))
b = [1, 3, 2]
print(subtract(b))
c = [3, 2, 1]
print(subtract(c, False))
校招面试总结:跳进思维定势,爬都爬不出来。
1.应该问面试官可不可以调包的。
2.差值最大,找到数组最大值,用最大值做差,找到值最大并且满足序列号要求的值。
3.沉着冷静,抗压