反正这是我的答案
题目摆在这里,没有确切的答案,下面是我的解答,对不对不知道。words.txt资源在这里。
There are solutions to these exercises in the next section. You should at least attempt each one before you read the solutions.
Exercise 1: Write a program that reads words.txt and prints only the words with more than 20 characters (not counting whitespace).
Exercise 2: In 1939 Ernest Vincent Wright published a 50,000 word novel called Gadsby that does not contain the letter “e”. Since “e” is the most common letter in English, that’s not easy to do. In fact, it is difficult to construct a solitary thought without using that most common symbol. It is slow going at first, but with caution and hours of training you can gradually gain facility. All right, I’ll stop now. Write a function called has_no_e that returns True if the given word doesn’t have the letter “e” in it. Write a program that reads words.txt and prints only the words that have no “e”. Compute the percentage of words in the list that have no “e”.
Exercise 3: Write a function named avoids that takes a word and a string of forbidden letters, and that returns True if the word doesn’t use any of the forbidden letters. Write a program that prompts the user to enter a string of forbidden letters and then prints the number of words that don’t contain any of them. Can you find a combination of 5 forbidden letters that excludes the smallest number of words?
Exercise 4: Write a function named uses_only that takes a word and a string of letters, and that returns True if the word contains only letters in the list. Can you make a sentence using only the letters acefhlo? Other than “Hoe alfalfa”?
Exercise 5: Write a function named uses_all that takes a word and a string of required letters, and that returns True if the word uses all the required letters at least once. How many words are there that use all the vowels aeiou? How about aeiouy?
Exercise 6: Write a function called is_abecedarian that returns True if the letters in a word appear in alphabetical order (double letters are ok). How many abecedarian words are there?
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | fin = open('words.txt') # 第1小问 for line in fin: if len(line) >= (20+2): word = line.strip() print(word) # counterdemonstrations # hyperaggressivenesses # microminiaturizations def has_no_e(word): # 第2小问 for letter in word: if letter == 'e': return False return True fin = open('words.txt') all = 0 count = 0 for line in fin: word = line.strip() all = all + 1 if has_no_e(word): print(word) count = count + 1 print(count, 'words without e') print('{:.0%}'.format(count/all), 'words without e') # ... # zymosis # zymotic # zymurgy # 37641 words without e # 33% words without e def avoids(word, x): # 第3小问,最后一个问题举手投降 for letterw in word: for letterx in x: if letterw == letterx: return False return True fin = open('words.txt') x = input('withtout: ') num = 0 # word = 'jwrojgre' # input('word is ') # print(avoids(word, x)) for line in fin: word = line.strip() if avoids(word, x): num = num + 1 print(num, 'words without', x) # withtout: aeiou # 107 words without aeiou # count = 0 # import itertools # for i in itertools.combinations('abcdefghijklmnopqrstuvwxyz', 5): # print(''.join(i)) # count = count + 1 # print(count) # 65780个排列组合的可能性啊啊啊啊啊啊 def uses_only(word, x): # 第4小问 for letter in word: if letter not in x: return False return True word = input('word is ') x = input('uses is ') print(uses_only(word, x)) # word is abc # uses is efg # False def uses_all(word, x): # 第5小问 for letter in x: if letter not in word: return False return True fin = open('words.txt') x = input('must use: ' ) num = 0 for line in fin: word = line.strip() if uses_all(word, x): num = num + 1 print(num, 'words with', x) # must use: aeiou # 598 words with aeiou # must use: aeiouy # 42 words with aeiouy def is_abecedarian(word): # 第6小问 index = 1 while index < len(word) - 1: if ord(word[index-1]) > ord(word[index]): return False index = index + 1 return True fin = open('words.txt') num = 0 for line in fin: word = line.strip() if is_abecedarian(word): num = num + 1 print(num, 'words is abecedarian') # 1573 words is abecedarian |