测试开发笔试题(python)

发布时间 2023-08-13 22:20:04作者: 小鱼小鱼hi

测试开发笔试题(python)_测试开发python笔试题_coco_qa的博客-CSDN博客

1、字符串相关

1.1 字符串中最大长度子串

# 字符串中最大长度子串
# 如字符串abcd13agbf,当重复出现某个字符时,算一个子串,比如abcd13a或bcd13agb都是子串
str1 = 'abcd13agbf'
str_list = []

for i in range(len(str1)):
    try:
        tmp = str1.index(str1[i], i+1)
        str_list.append(str1[i:tmp+1])
    except ValueError:
        continue

print(str_list)

1.2 是否是回文

# 是否是回文 例如 “abccba” 是一个回文
# 方法一:反转reversed()函数,判断是否相等
str1 = "abccba"
if list(str1) == list(reversed(list(str1))):
    print('是回文')
else:
    print('不是回文')

#方法二:利用递归
def ishui(s):
    if len(s) <= 2:
        print('是回文')
    elif s[0] == s[-1]:
        return ishui(s[1:-1])
    else:
        print('不是回文')
ishui(str1)

1.3 找出子序列

# 判断字符串str1 是 字符串str2 的子串
str1 = 'ace'
str2 = 'abcdef'
def isquence(s,t):
    j = 0    #遍历t
    for i in range(len(s)):
        tmp = s[i]  #当前需要查找的字符串tmp
        try:
            res = t.index(tmp,j) #记录当前tmp在s中的位置
            # list.index(x[, start[, end]])
            print(res)
            j = res + 1  # 找到后继续遍历
        except ValueError:
            return False   #/没找到返回错误

    return True
print(isquence(str1,str2))

# 判断字符串是否包含某个子串
def find(s,t):
    try:
        return s.index(t)
    except ValueError:
        return False

s = 'nihao,shijie'
t = 'nihao'
result = find(s,t)
print(result)

1.4 字符串中出现最多的字符

# 方法1
str1 = 'abcaaaaddddd'
str2 = list(set(str1))
print(str2, type(str2))
count_list = []
for i in str2:
    str3 = str1.count(i)
    print(str3)
    count_list.append(str3)
print(count_list) # [5,1,1,5]
print(str2[count_list.index(max(count_list))])

# 方法2
s = "abcaaaaddddd"
count = {}
for i in s:
    if i not in count:
        count[i] = 1
    else:
        count[i] += 1
print(max(count))