練習011-012

第 0011 題: 敏感詞文本文件 filtered_words.txt,裏面的內容爲以下內容,當用戶輸入敏感詞語時,則打印出 Freedom,否則打印出 Human Rights。

北京
程序員
公務員
領導
牛比
牛逼
你娘
你媽
love
sex
jiangge

第 0012 題: 敏感詞文本文件 filtered_words.txt,裏面的內容 和 0011題一樣,當用戶輸入敏感詞語,則用 星號 * 替換,例如當用戶輸入「北京是個好城市」,則變成「**是個好城市」。

011.py

#coding:utf-8

def recode():
    list = []
    with open('11.txt','r') as file:
        for i in file.readlines():
            #print i.decode('gbk')
            list.append(i.decode('gbk'))

    file.close()
    return list
def check(word):
    #print word 
    list = recode()
    flag = 0 
    for i in range(len(list)):
        if list[i].strip()==word.decode('utf-8').strip():
            flag = 1 
            break
        else:
            flag = 0
    return flag 


if __name__ == "__main__":
    word = raw_input("word: ")
    while word:
        if check(word):
            print "Freedom"
        else:
            print "Human Right"
        word = raw_input("word: ")

012.py

#coding:utf-8
import string
def recode():
    list = []
    with open('11.txt','r') as file:
        for i in file.readlines():
            #print i.decode('gbk')
            list.append(i.decode('gbk').strip())
    file.close()
    return list
def check(word):
    #print word 
    word = word.decode('utf-8').strip()
    list = recode()
    flag = 0 
    star ="*"
    for i in range(len(list)):
        if list[i] in word:

            for j in range(1,len(list[i])):
                star +="*"

            xx = word.replace(list[i],star)    

            break

    return xx


if __name__ == "__main__":
    word = raw_input("word: ")
    while word:
        print check(word)
        word = raw_input("word: ")

011結果

012結果

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章