wuzhenhu

测试面试计算题--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,

分类:

技术点:

相关文章:

  • 2022-02-09
  • 2021-07-26
  • 2021-06-15
  • 2022-02-07
  • 2022-02-17
  • 2021-09-28
  • 2022-01-04
  • 2021-11-28
猜你喜欢
  • 2022-02-19
  • 2022-01-16
  • 2021-10-18
  • 2021-04-23
  • 2021-11-21
相关资源
相似解决方案