5.4 文本分析与加密

第1关 统计字符数量;第2关 统计单词数量;第3关 计算偏移量;第4关 凯撒密码加密

第1关 统计字符数量

import string
# 读文件,返回字符串  
def read_file(file):  
    ##################Begin##################
    with open(file, 'r', encoding='utf-8') as f:  
        return f.read()
    ###################End###################

# 返回大写字母、小写字母、数字、空格和其他字符的数量  
def classify_char(txt):  
    ##################Begin##################
    upper, lower, digit, space, other = 0, 0, 0, 0, 0  
    for ch in txt:  
        if ch.islower():  
            lower = lower + 1  
        elif ch.isupper():  
            upper = upper + 1  
        elif ch.isnumeric():  
            digit = digit + 1  
        elif ch.isspace():  
            space = space + 1  
        else:  
            other = other + 1  
    return upper, lower, digit, space, other 


    ###################End###################

if __name__ == '__main__':  
    filename = 'mayun.txt'  # 读取的文件名  
    text = read_file(filename)  # text为字符串  
    print(*classify_char(text))  

第2关 统计单词数量

import string
# 读文件,返回字符串
def read_file(file):    
   with open(file, 'r', encoding='utf-8') as f:      
        return f.read()  


# 用空格替换所有符号,切分为列表
def word_list(txt):
    for ch in '!"#$%&()*+,-.:;<=>?@[\\]^_’‘{|}~/':
    #########################Begin############################### 
        txt = txt.replace(ch, " ")  # 所有符号替换为空格  
    return txt.split()  # 切分为列表,返回列表


    #########################End############################### 


# 返回单词数量
def number_of_words(ls):
    #########################Begin############################### 
    return len(ls)

    #########################End############################### 

if __name__ == '__main__':
    filename = 'mayun.txt'  # 读取的文件名
    text = read_file(filename)  # text为字符串
    words_list = word_list(text)  # 单词的列表
    words_counts = number_of_words(words_list)
    print(f'共有{words_counts}单词')

第3关 计算偏移量

import string

# 用字符串中字符ASCII值的和对26取模为偏移量
def offset_cal(day):
    #########################Begin############################### 
    sum_of_ch = 0  
    for c in day:  
        sum_of_ch = sum_of_ch + ord(c)  
    offset = sum_of_ch % 26  
    return offset


    #########################End############################### 


if __name__ == '__main__':
    secret_word = input()
    offset_number = offset_cal(secret_word)
    print(offset_number)

第4关 凯撒密码加密

import string
# 读文件,返回字符串  
def read_file(file):  
    with open(file, 'r', encoding='utf-8') as f:  
        return f.read()

# 用字符串中字符ASCII值的和对26取模为偏移量  
def offset_cal(day):  
    sum_of_ch = 0  
    for c in day:  
        sum_of_ch = sum_of_ch + ord(c)  
    offset = sum_of_ch % 26  
    return offset

def kaisa(txt, number):  
    ###########################Begin###########################
    lower = string.ascii_lowercase  # 小写字母  
    upper = string.ascii_uppercase  # 大写字母  
    before = string.ascii_letters  
    after = lower[number:] + lower[:number] + upper[number:] + upper[:number]  
    table = ''.maketrans(before, after)  # 创建映射表  
    return txt.translate(table)


    ############################End############################

if __name__ == '__main__':  
    filename = 'mayun.txt'  # 读取的文件名  
    text = read_file(filename)  # text为字符串  
    secret_word = input()  
    offset_number = offset_cal(secret_word)  
    print(kaisa(text, offset_number))  

你可能感兴趣的:(python习题分享,python,算法,学习)