记录一些面试的题吧,没事自己看看
1.python,c++,java棋盘中的白子进行八连通区域查找,并返回连通区域块数和连通区域分类情况
# encoding=utf8def show(L): # 直观显示用 非本题核心代码 rows = len(L) cols = len(L[0]) for i in range(rows): for j in range(cols): print(L[i][j], end=" ") print()# 棋盘19*19 1为白色棋子 0为其他L = [[0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1], [0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1], [0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1], [0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1], [0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1], [0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1], [0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1], [0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1], [0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1], [0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1], [0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1], [0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1], [0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1], [0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1], [0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1], [0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1], [0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1], [0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1]]rows = len(L)cols = len(L[0])# 生成flag矩阵 标记处理过的点flag = []for i in range(rows): col = cols * [0] flag.append(col)def means(i, j, k): ''' 聚类查找 :param i: 棋盘行坐标 :param j: 棋盘列坐标 :param k: 聚类类别 ''' # 非白棋或者查找过的点排除 if i < 0 or i >= rows or j < 0 or j >= cols or \ flag[i][j] != 0 or L[i][j] != 1: return 0 flag[i][j] = k # 使用八连通区域查找 means(i, j - 1, k) means(i, j + 1, k) means(i - 1, j, k) means(i + 1, j, k) means(i + 1, j - 1, k) means(i + 1, j + 1, k) means(i - 1, j - 1, k) means(i - 1, j + 1, k)if __name__ == "__main__": # k聚类 k = 1 for i in range(rows): for j in range(cols): # 未查找过的白棋 if L[i][j] == 1 and flag[i][j] == 0: means(i, j, k) k += 1 print('共有{}块'.format(k)) print('聚类效果如下:') show(flag)2.python二维矩阵转置,不能使用numpy
# encoding=utf8def show(L): # 直观显示用 非本题核心代码 rows = len(L) cols = len(L[0]) for i in range(rows): for j in range(cols): print(L[i][j], end=" ") print()def transpose(A): B = [] for i in range(len(A[0])): col = [0] * len(A) B.append(col) for i in range(len(A)): for j in range(len(A[0])): B[j][i] = A[i][j] print('转置前:') show(A) print('转置后:') show(B)if __name__ == '__main__': A = [[1, 2, 3], [4, 5, 6]] transpose(A)
3.python计算两个bbox的iou 人家没说不能用numpy我就用了
# encoding=utf8import numpy as npdef bbox_iou(gt_bbox, pred_bbox): ''' :param gt_bbox: 数据集bbox坐标 :param pred_bbox: 预测bbox坐标 :return: iou是否符合阈值 阈值默认0.5 ''' # 得到union区域左上角和右下角 insert_left = np.maximum(gt_bbox[:2], pred_bbox[:2]) insert_right = np.minimum(gt_bbox[2:], pred_bbox[2:]) # 重合区域面积 insert_area = (insert_right[0] - insert_left[0]) * (insert_right[1] - insert_left[1]) # gt_bbox面积 gt_area = (gt_bbox[2] - gt_bbox[0]) * (gt_bbox[3] - gt_bbox[1]) # pred_bbox面积 pred_area = (pred_bbox[2] - pred_bbox[0]) * (pred_bbox[3] - pred_bbox[1]) iou = insert_area / (gt_area + pred_area - insert_area) if iou > 0.5: return True else: return Falseif __name__ == '__main__': # 例子 gt_bbox = [0.1, 0.1, 0.5, 0.5] pred_bbox = [0.3, 0.2, 0.7, 0.9] if bbox_iou(gt_bbox, pred_bbox): print('预测框达到IOU阈值') else: print('预测框未达到IOU阈值')
4.top_k查找 不允许使用sorted和list.sort
# encoding=utf8def find_good_gay(file_name): ''' scores.txt文件在本项目文件夹中 :param file_name: 文件路径 :return: 成绩最好的三个学生的姓名和成绩 ''' # 读取txt文件 f = open(file_name) L = f.readlines() num = len(L) # 使用字典格式处理数据 res_dict = {} # 生成成绩字典 for i in range(num): res_dict[L[i].split(' ')[0]] = L[i].strip().split(' ')[1] # 排序 for i in range(num): for j in range(num - i - 1): key1 = list(res_dict.keys())[j] key2 = list(res_dict.keys())[j + 1] if res_dict[key1] < res_dict[key2]: res_dict[key1], res_dict[key2] = res_dict[key2], res_dict[key1] print('成绩排在前三名的分别是:') for i in range(3): print(list(res_dict.keys())[i] + ':' + res_dict[list(res_dict.keys())[i]])if __name__ == '__main__': find_good_gay('scores.txt')
来源:https://www.cnblogs.com/haimingv/p/12631152.html