作业来源:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2684
1.字符串操作:
解析身份证号:生日、性别、出生地等
# 获取身份证号中的出生地与性别 identy = input("请输入您的身份证号:") while (len(identy)!=18): print("您的身份证号码输入错误") identy = input("请重新输入您的身份证号:") birthplace = input("请输入你的出生地:") print("你的出生地是{}".format(birthplace)) sex = identy[-2] if int(sex) % 2 == 0: print("性别为女") else: print("性别为男")
凯撒密码编码与解码
MAX_KEY_SIZE = 26 def getMode(): while True: print('请选择加密或解密模式,或者选择暴力破解:') print('加密:encrypt(e)') print('解密:decrypt(d)') print('暴力破解:brute(b)') mode = input().lower() if mode in 'encrypt e decrypt d brute b'.split(): return mode else: print('请输入"encrypt"或"e"或"decrypt"或"d"或"brute"或"b"!') def getMessage(): print('请输入你的信息:') return input() def getKey(): key = 0 while True: print('请输入密钥数字(1-%s)' % (MAX_KEY_SIZE)) key = int(input()) if (key >=1 and key <= MAX_KEY_SIZE): return key def getTranslatedMessage(mode, message, key): if mode[0] == 'd': key = -key translated = '' for symbol in message: if symbol.isalpha(): num = ord(symbol) num += key if symbol.isupper(): if num > ord('Z'): num -= 26 elif num < ord('A'): num += 26 elif symbol.islower(): if num > ord('z'): num -= 26 elif num < ord('a'): num += 26 translated += chr(num) else: translated += symbol return translated mode = getMode() message = getMessage() if mode[0] != 'b': key = getKey() print('你要翻译的信息是:') if mode[0] != 'b': print(getTranslatedMessage(mode, message, key)) else: for key in range(1, MAX_KEY_SIZE + 1): print(key, getTranslatedMessage('decrypt', message, key))
网址观察与批量生成
for i in range(2,8): print('http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i))
2.英文词频统计预处理
下载一首英文的歌词或文章或小说。
将所有大写转换为小写
将所有其他做分隔符(,.?!)替换为空格
分隔出一个一个的单词
并统计单词出现的次数
STR=''' But that's alright because I like the way it hurts Just gonna stand there and hear me cry But that's alright because I love the way you lie I love the way you lie I can't tell you what it really is I can only tell you what it feels like ''' STR = STR.lower()#将所有大写转换为小写 print(STR) s = ',.?!' for i in s: STR = STR.replace(i,' ')#将所有其他做分隔符(,.?!)替换为空格 print(STR) STR = STR.split()#分隔出一个一个的单词 print(STR) InfoSet = set(STR) Count = {} for word in InfoSet: Count.setdefault(word,STR.count(word))#统计单词出现的次数
3.文件操作
- 同一目录、绝对路径、相对路径
- 凯撒密码:从文件读入密函,进行加密或解密,保存到文件。
-
f = open('1.txt', 'r', encoding='utf8') f = f.read() Code1 = '' for i in f: Code1 = Code1+chr(ord(i)+3) Code2 = open('2.txt', 'a', encoding='utf8') Code2 = Code2.write(Code1)
4.函数定义
- 加密函数
def JiaMi(dode) : code1 = '' for i in code : i = i + chr(ord(i)+3) return i
解密函数
-
def JieMi(code) : code1 = '' for i in code : i = i + chr(ord(i)-3) return i
读文本函数
-
def read(article) : file = open(article, 'r',encoding = 'utf8') return file.read()