8/6/2018 Python programming group homework----Python language test practice questions [10th day]

foreword

The assignment of this group does not specify the direction of Python, but to improve the language skills of members in the group, so that everyone can go further in other directions of Python.
This group welcomes Python learners of any level. Python
programming group number: 651707058

Question 1: Find the Caesar Cipher

The encryption method of the Caesar cipher is: whenever you want to encrypt a piece of text, you need to choose a shift value S,
which is an integer between 0 and 25. Then, you replace each letter in the text with the letter after S
positions (assuming S=1, then replace A with B). If the position exceeds Z, then continue counting from A

For example: Ciphertext: Ifsf up tubz Shift value s=25
The plaintext output is: Here to stay

After the program asks for a piece of plaintext, enter a shift value and output the corresponding Caesar cipher

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

'''
解释:chr((ord(c)-ord('A')+step)%26+ord('A'))
假如把 b移动2位到d
(98-97+2)%26 + 97 = 100
100就为b的ASCII码
'''

Question 2: Count the most frequently occurring words

Count the words that appear the most times in a paragraph. The words are the same, but different capitalization is regarded as the same word. When the word with the most occurrences is
output , it is converted into lowercase output.
For example:
Here is a line like sparkling wine
Line up fast or be the last

output: line

import re
from collections import Counter
s = '''
Here is a line like sparkling wine
Line up fast or be the last
'''
res = re.findall(r'\b([a-zA-Z]+)\b',s)
for n,words in enumerate(res):
    res[n] = words.lower()
count = Counter(res)
most_word = ''
times = 0
for w,t in count.items():
    if t>times:
        most_word = w
        times = t
print(most_word)

'''
解释:主要用到了正则表达式,和一个Counter计数函数,使用.items()可以将
字典变成元组
'''

Question 3: Design a simple verification code generator

It is required to generate a 4-digit verification code, each of which may be a number, uppercase letter, or lowercase letter. When a verification code is generated, the
program prompts to enter the verification code. After the user enters, the program can judge whether the input verification code is correct or not, and the verification code verification does not distinguish between upper and lowercase
letters

For example: the generated verification code is: X8p7
, please enter the verification code: x8P7
output: verification is successful
For example : the generated verification code is: X8p7
, please enter the verification code: x8L7
output: verification failed

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('验证失败')

'''
解释:一种随机是随机某个位置是大写字母、小写字母还是数字,还有一种随机是在上一个随机的
    基础上随机出具体的值
'''

Question 4: Find the longest common prefix

Write a function to find the longest common prefix in an array of strings.
If no common prefix exists, the empty string "" is returned.

Example 1:
Input: ["flower","flow","flight"]
Output: "fl"

Example 2:
Input: ["dog","racecar","car"]
Output: ""
Explanation: The input does not have a common prefix.

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"]))

'''
解释:先求出最短单词的长度,因为公共前缀顶多是它的长度。
求解思路:
    先暂定公共前缀是每个单词的第一位:然后遍历每个单词的第一位,
    只要前一个单词第一位==后一个第一个单词第一位就行
    如果遍历完了,都相等,那么就将单词的第一位存入到perfix中

    然后暂定公共前缀是每个单词的前两位,然后遍历每个单词的前两位
    遍历完了,如果都相等就将结果存入perfix,如果有不相等的就不存入
    直接返回上一次存的perfix

    如果最短单词长度循环完了,也要返回perfix
'''

Finally, I wish the group members a happy study~~

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326204465&siteId=291194637