学校作业5_1字符串_文本分析与加密(头哥作业[Python])

 本系列文章为同学困扰的python做出作业答案汇总,答案皆为本学生自己边学边做汇总上传的,编排不易,希望大家喜欢。

本作业所有内容均在github上有留存,每次作业github上会最先发布,CSDN上需要编辑将会慢一两天,着急完成作业的同学可以前往github抢先学习:Opve2/homework: 这个作业 (github.com)

 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," ")    #所有符号替换为空格
    #print(txt)
    return txt.split()


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

    


# 返回单词数量
def number_of_words(ls):
    #########################Begin############################### 
    word = 0
    for sh in ls:
        if sh.isalnum():
            word += 1
    return word

    #########################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)