Think Python Exercise 9.4

编写一个名为 avoids 的函数,接受一个单词和一个指定禁止使用字符的字符串,如果单词中不包含任意被禁止的字符,则返回True 。

修改你的程序,提示用户输入一个禁止使用的字符,然后打印不包含这些字符的单词的数量。你能找到一个5个禁止使用字符的组合,使得其排除的单词数目最少么?

你能找到一个"5个禁止使用字符"的组合,使得其排除的单词数目最少么? 分析:从26个字母中挑选5个字母构成一个组合,打印不包含这些字母的单词的数量,看哪个组合下被排除出去的单词数目最少。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
def avoids(word,ause): #ause,avoid use
    for i in ause:
        for letter in word:
            if letter == i:
                return False
    return True

def has_no_string(file_in, nals): #nals指的是no alphabets,字母
    count_nals = 0 #不含用户指定的各个字符的单词的个数
    for line in file_in:
        word = line.strip()
        if avoids(word,nals):
            count_nals += 1
        
    print("不含",nals,"的单词有",count_nals,"个")


text = input("请输入一个禁止使用的字符串,字符串里的字母都不会出现在单词中\n")

fin = open('words.txt')

has_no_string(fin,text)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
def avoids(word,ause): #ause,avoid use
    for i in ause:
        for letter in word:
            if letter == i:
                return False
    return True

def has_no_string(file_in, nals): #nals指的是no alphabets,字母
    count_nals = 0 #不含用户指定的各个字符的单词的个数
    for line in file_in:
        word = line.strip()
        if avoids(word,nals):
            count_nals += 1
        
    # print("不含",nals,"的单词有",count_nals,"个")
    return count_nals


def alp5group(file_in): #5个字母的组合,起名困难
    
    alphabets = "abcdefghijklmnopqrstuvwxyz"
    count_word = 0 # 不含指定的5个字母的单词的数量
    namegroup = '' # 不含5个字符的组合,单词最多
    i = 0
    while i < len(alphabets):
        group = ''
        group += alphabets[i]
        j = 1
        while j < len(alphabets[i:]):
            group += alphabets[i+j]
            k = 1
            while k < len(alphabets[i+j:]):
                group += alphabets[i+j+k]
                l = 1
                while l < len(alphabets[i+j+k:]):
                    group += alphabets[i+j+k+l]
                    m = 1
                    while m < len(alphabets[i+j+k+l:]):
                        group += alphabets[i+j+k+l+m]
                        count = has_no_string(file_in,group)
                        if count > count_word:
                            count_word = count
                            namegroup = group
                        m += 1
                    l += 1
                k += 1
            j += 1
        i += 1
                            
    print("组合",namegroup,"被排除出去的单词数目最少,单词数量为",count_word)
                            


fin = open('words.txt')

alp5group(fin)

下面是使用 in 操作符优化后的方案,可以用 in 真的太幸福了😭。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
def avoids(word,ause): #ause,avoid use
    for i in ause:
        if i in word:
            return False
    return True

fin = open('words.txt')
text = input("请输入一个禁止使用的字符串,字符串里的字母都不会出现在单词中\n")

count = 0 #单词总数
count_n = 0 #不含用户输入字符的单词的个数

for line in fin:
    word = line.strip()
    count += 1
    if avoids(word,text):
        count_n += 1

print("不含",text,"的单词有",count_n,"个,单词一共:",count)