Python语言测试练习题

第一题:求凯撒密码

”’
凯撒密码的加密方法是:每当你想要加密一段文字时,你需要选择一个移位值 S,
它是一个0到25之间的整数。然后,你把文字中的每一个字母用S个位置之后的字母
替换(假设S=1,那么A就用B替换)。如果位置超过了Z,那么就要从A开始继续数

例如:密文: Ifsf up tubz 移位值s=25
输出的明文为:Here to stay

程序要求输入一段明文后,再输入一个移位值,输出相应的凯撒密码
”’

def get_keywords(code,step):
    res=''
    for c in code:
        if c==' ':
            res+=' '
        elif c.isupper() == True:
            res+=chr(((ord(c)-ord('A')+step)%26+ord('A')))
        else:
            res+=chr(((ord(c)-ord('a'))+step)%26+ord('a'))
    print(res)
get_keywords("Ifsf up tubz",25)

第二题:统计出现最多的单词

”’
统计一段话中出现最多次数的单词,单词一样,大小写不同视为同一个单词
输出出现次数最多的单词的时候,将其变成小写输出
例如:
Here is a line like sparkling wine
Line up fast or be the last

输出:line
”’

import re
s='''Here is a line like sparkling wine
Line up fast or be the last'''
from collections import Counter
res=re.findall(r'\b([a-zA-Z]+)\b',s)
for n,words in enumerate(res):
    res[n]=words.lower()
d = Counter(res)
for key in d:
    if d[key]==max(res.count(x) for x in set(res)):
        print(key)

第三题:简单设计一个验证码的生成器

”’
要求生成一个4位验证码,每一位都有可能是数字、大写字母、小写字母,当生成一个验证码后
程序提示输入验证码。用户输入后,程序可以判断输入的验证码的正误,其中验证码验证不分
字母大小写

例如:生成的验证码为:X8p7
请输入验证码:x8P7
输出:验证成功
例如:生成的验证码为:X8p7
请输入验证码:x8L7
输出:验证失败
”’
这题小编不会,摊手,大家还是参考大佬的吧
https://blog.csdn.net/tobe_numberone/article/details/81448359

from random import randint

def gen_Verification_Code():
    res = ''
    for i in range(4):
        choice = randint(1,3)
        if choice == 1:
            res+=chr(randint(65,90))
        elif choice == 2:
            res+=chr(randint(97,122))
        else:
            res+=str(randint(0,9))
    return res
Verification_Code = gen_Verification_Code()
print('请输入验证码:{vfcode}'.format(vfcode=Verification_Code))
code = input(':')
if Verification_Code.lower() == code.lower():
    print('验证成功')
else:
    print('验证失败')

第四题:求最长公共前缀

”’
编写一个函数来查找字符串数组中的最长公共前缀。
如果不存在公共前缀,返回空字符串 “”。

示例 1:
输入: [“flower”,”flow”,”flight”]
输出: “fl”

示例 2:
输入: [“dog”,”racecar”,”car”]
输出: “”
解释: 输入不存在公共前缀。
”’
嘿嘿,苦笑,,,参考大佬的:https://blog.csdn.net/tobe_numberone/article/details/81448359

def longestCommonPrefix(str_list):
    '''
    :param str_list:传入的是元素为字符串的列表 
    :return:  最长公共前缀
    '''
    len_list = [len(w) for w in str_list]
    len_min = min(len_list)
    perfix = ''
    for i in range(1,len_min+1):
        for j in range(1,len(str_list)):
            if str_list[j][0:i] != str_list[j-1][0:i]:
                return perfix
        perfix = str_list[0][0:i]
    return perfix
print(longestCommonPrefix(["flower","flow","flight"]))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值