Chapters 9-12
Word Play
Write a program that reads words.txt and prints only the words with more than 20 characters (not counting whitespace).
words = open('words.txt')
for word in words:
word = word.strip()
if len(word) > 20:
print word
Output:
counterdemonstrations
hyperaggressivenesses
microminiaturizations
def has_no_e(word):
return 'e' not in word
def pct_no_e(words):
number_no_e = 0.0
for word in words:
if has_no_e(word):
number_no_e += 1
return number_no_e / len(words)
pct_no_e(words)
def avoids(word, chars):
for char in chars:
if char in word:
return False
return True
def number_avoid(words):
words_which_avoid = 0.0
chars = raw_input('Enter forbidden characters: ')
for word in words:
if avoids(word, chars):
words_which_avoid += 1
print words_which_avoid
Got which five-letter combo has the most words? It's qkjzw, with 91,293 words (80.22%)