Case study: word play

For the exercises in this chapter we need a list of English words. There are lots of word lists available on the Web, but the most suitable for our purpose is one of the word lists collected (and contributed to the public domain) by Grady Ward as part of Moby lexicon project. It is a list of 113,809 official crosswords; that is, words that are considered valid in crossword puzzles and other word games. In the Moby collection, the filename is 113809of.fic; if you download the swampy, there is a copy of this file, with the simpler name words.txt. this file is in plain text, so you can open it with a text editor, but you can also read it from Python. The built-in function open takes the name of the file as a parameter and returns a file object you can use to read the file. The file object provides several methods for reading, including readline, which reads characters from the file until it gets to a newline, and returns the result as a string. Note that you need to put the file in your work directory. Or you can change your work directory:

                       

Exercises

1. Write a program that reads words.txt and prints only the words with more than 20 characters.

 

2. Write a function called has_no_e to print the words that have no ‘e’ and compute the percentage of the words in the list have no ‘e’.

 

……….

 

Simple way to verify your result:

 

 

3. Write a function named avoids that takes a string of forbidden letters. Print the number of words that don’t contain any of them.

 

 

4. Write a function named uses_only that takes a word and a string of letters, and that returns the word if it contains only letters in the list.

 

 

5. Write a function named uses_all that takes a string of required letters, and that returns the word uses all the required letters at least once.

 

 

6. Write a function named uses_all_only that takes a string of required letters, and that returns the word uses all the required letters at least once and only contains the list of the letters.

 

 

7. Write a function called is_abecedarian that returns the word if the letters in a word appear in alphabetical order (exclude double letters). And also print how many abecedarian words are there.

 

……..

 

 

8. A palindrome is a word that reads the same forward and backward, like ‘rotator’ and ‘noon’. Write a function print all the palindrome words and the print the number as well.

 

……

 

from Thinking in Python

 

原文地址:https://www.cnblogs.com/ryansunyu/p/3826125.html