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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值