做業來源:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2684html
1.字符串操做:python
ID=input('請輸入十八位身份證號碼: ') if len(ID)==18: print("你的身份證號碼是 "+ID) else: print("錯誤的身份證號碼") year=ID[6:10] moon=ID[10:12] day=ID[12:14] ID_sex=ID[16:17] ID_check=ID[17] print("生日: "+year+'年'+moon+'月'+day+'日') if int(ID_sex)%2==0: print('性別:女') else: print('性別:男') W=[7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2] ID_num=[18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2] ID_CHECK=['1','0','X','9','8','7','6','5','4','3','2'] ID_aXw=0 for i in range(len(W)): ID_aXw=ID_aXw+int(ID[i])*W[i] ID_Check=ID_aXw%11 if ID_check==ID_CHECK[ID_Check]: print('正確的身份證號碼') else: print('錯誤的身份證號碼')
import os def encryption(): str_raw = input("請輸入明文:") k = int(input("請輸入位移值:")) str_change = str_raw.lower() str_list = list(str_change) str_list_encry = str_list i = 0 while i < len(str_list): if ord(str_list[i]) < 123-k: str_list_encry[i] = chr(ord(str_list[i]) + k) else: str_list_encry[i] = chr(ord(str_list[i]) + k - 26) i = i+1 print ("加密結果爲:"+"".join(str_list_encry)) def decryption(): str_raw = input("請輸入密文:") k = int(input("請輸入位移值:")) str_change = str_raw.lower() str_list = list(str_change) str_list_decry = str_list i = 0 while i < len(str_list): if ord(str_list[i]) >= 97+k: str_list_decry[i] = chr(ord(str_list[i]) - k) else: str_list_decry[i] = chr(ord(str_list[i]) + 26 - k) i = i+1 print ("解密結果爲:"+"".join(str_list_decry)) while True: print (u"1. 加密") print (u"2. 解密") choice = input("請選擇:") if choice == "1": encryption() elif choice == "2": decryption() else: print (u"您的輸入有誤!")
for i in range(2,15): print('http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i))
2.英文詞頻統計預處理函數
STR=''' Twinkle twinkle little star How I wonder what you are Up above the world so high Like a diamond in the sky Twinkle twinkle little star How I wonder what you are Twinkle twinkle little star How I wonder what you are Up above the world so high Like a diamond in the sky Twinkle twinkle little star How I wonder what you are Twinkle twinkle little star How I wonder what you are Twinkle twinkle little star How I wonder what you are ''' 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))#統計單詞出現的次數 print(Count)
3.文件操做編碼
def getMima(): Massage = str(input("輸入明文並保存文本:")) with open('excise1.txt','w') as f: f.write(Massage) f.close() Mima = '' for i in Massage: Mima = Mima + chr(ord(i)+2) print('加密結果:'+Mima+'\n') with open('excise2.txt','w') as f: f.write(Mima) f.close() def getMassageFromTXT(): print("對文本內容解碼..") with open('excise2.txt','r') as f: s = f.read() Massage = '' if s == None: print('沒有可解碼的文本\n') else: for i in s: Massage = Massage + chr(ord(i)-2) print('解碼結果:'+Massage+'\n') if __name__ == '__main__': while 1: a = int(input('加密(1)解碼(2)退出(0):')) if a == 0: break elif a == 1: getMima() elif a == 2: getMassageFromTXT()
詞頻統計:下載一首英文的歌詞或文章或小說,保存爲utf8文件。從文件讀入文本進行處理加密
song = '''Twinkle twinkle little star How I wonder what you are Up above the world so high Like a diamond in the sky Twinkle twinkle little star How I wonder what you are Twinkle twinkle little star How I wonder what you are Up above the world so high Like a diamond in the sky Twinkle twinkle little star How I wonder what you are Twinkle twinkle little star How I wonder what you are Twinkle twinkle little star How I wonder what you areound that type of environment. ''' f = open('excise.txt','a',encoding='utf-8') f.write(song) f.close()
4.函數定義spa
def getMima(): Massage = str(input("輸入明文並保存文本:")) with open('excise1.txt','w') as f: f.write(Massage) f.close() Mima = '' for i in Massage: Mima = Mima + chr(ord(i)+2) print('加密結果:'+Mima+'\n') with open('excise2.txt','w') as f: f.write(Mima) f.close() def getMassageFromTXT(): print("對文本內容解碼..") with open('excise2.txt','r') as f: s = f.read() Massage = '' if s == None: print('沒有可解碼的文本\n') else: for i in s: Massage = Massage + chr(ord(i)-2) print('解碼結果:'+Massage+'\n') if __name__ == '__main__': while 1: a = int(input('加密(1)解碼(2)退出(0):')) if a == 0: break elif a == 1: getMima() elif a == 2: getMassageFromTXT()
def read(): f = open('excise.txt','r') s = f.read() print(s) f.close() if __name__=='__main__': read()