前面约了我2次电面,都被飞机了,有点不爽。今日再打电话过来,我就说直接开始吧。。。
没啥说的,对着简历一通问,聊了半个钟。然后丢过来2个笔试题:
题目一:有两个商品列表,分别包含商品的编码,求两个商品列表重复项
题目二:三个线程,顺序循环打印1-100,每个线程打印10个字,如线程A打印1-10,B打印11-20,C打印21-30,A打印31-40
第一题,可以暴力遍历,也可简单写
# 返回两个列表中的相同项 def get_the_same(list1, list2): return [x for x in list1 if x in list2 ]
第二题,着实把我难住了,很少用多线程,也没有仔细去研究过Python的高级用法。下午2点给他丢过去个半成品,3点半才又发了一份过去,用线程+事件解决。不知道结果如何
""" created by yaoxianwei at 2020-12-30 """ import threading, numpy a_event = threading.Event() b_event = threading.Event() c_event = threading.Event() def print_data(event, next_event, data): for d in data: event.wait() # 等待触发 for i in d: print(threading.currentThread().name + '------>' + str(i)) event.clear() # 内部标识设置为True,下一次循环进入阻塞状态 next_event.set() if __name__ == '__main__': # 拆分数据 split_result = numpy.array_split(range(1, 100 + 1), 10) # 每个线程需要打印的数据 data_a = [] data_b = [] data_c = [] for i in range(len(split_result)): if i % 3 == 0: data_a.append(split_result[i]) if i % 3 == 1: data_b.append(split_result[i]) if i % 3 == 2: data_c.append(split_result[i]) print(data_a) print(data_b) print(data_c) # 创建线程 a_thread = threading.Thread(target=print_data, args=(a_event, b_event, data_a)) b_thread = threading.Thread(target=print_data, args=(b_event, c_event, data_b)) c_thread = threading.Thread(target=print_data, args=(c_event, a_event, data_c)) # 启动线程 a_thread.start() b_thread.start() c_thread.start() # 开始触发 a_event.set()
参考:http://www.coolpython.net/python_senior/concurrent/multithreading_exercises.html