One-line summary
In this exercise, you find in a file those words which consist of exactly a given number of letters, which are all different.
Exercise description
The function different_letters()
receives as parameters a string filename
, which is the name of a file, and a positive integer count
(which is at least 1). The filename refers to a text file, containing words. You may assume that each line of the file consists of exactly one word.
The function should return a list of all the words in the file, which contain exactly count
letters, whereby all letters in the word are different. You must exclude words which contain other characters than letters (e.g., digits or quotation marks).
If no words in the file meet the requirements, you return an empty list.
Hints
To check whether all letters in a word are different, you can make smart use of sets.
The string method isalpha()
returns True
if all characters in the string are letters, and False
otherwise.
Example
The file shortdict.txt
contains the following words: ape, bowl, dolphin, kinship, not, note, notes, onset, pea, planet5, pumpkin, pumpkins, satin, shepherd, sonnet, stone, tones, typical, typist, wouldn’t.
different_letters( "shortdict.txt", 7 )
returns ['dolphin', 'typical']
. Note that the following words are not included: