测试面试计算题--python
1,冒牌排序
冒泡排序 程序分析:两两比较一轮,后再剩下的两两循环比较 ''' list_demo = [12, 13, 14, 15, 16, 1] for i in range(len(list_demo)-1): for i2 in range(len(list_demo)-1-i): if list_demo[i2] > list_demo[i2+1]: list_demo[i2], list_demo[i2+1] = list_demo[i2+1], list_demo[i2] # print(sorted(list_demo)) # 仅仅是正序输出,不影响元数据 # print(list_demo.sort()) # 没有返回值,改变了元数据 返回只none,再次print列表显示正序 print(list_demo)
2,数字1、2、3、4组合成每位数不同的三位数
程序分析:可以在百位十位个位,都是1、2、3、4,最后所有的排序再去掉不满足条件的排序 ''' n_list = [1, 2, 3, 4] p_list = n_list for a in n_list: for b in n_list: for c in n_list: if a != b and b != c and c != a: print('%s%s%s' % (a, b, c), end=',') # 格式化输出,并且以,结尾每个输出
3,字符串倒序
tool = 'asdfeqwer' # 使用循环下标倒序连接 a = "" for i in range(len(tool)-1, -1, -1): a = ''.join((a, tool[i])) print(a) # 使用字符串本身存在下标操作 print(tool[::-1]) # 类型转化,反转 tool_list = list(tool) tool_list.reverse() print(''.join(tool_list)) # 使用reversed来操作 tool_list2 = list(reversed(tool)) print(''.join(tool_list2))
4,