user1768884
user1768884

Reputation: 1047

Program that searches for words that use certain letters

I'm designing a program that looks through a list of words, and counts how many words only have the letters p, y, t, h, o and n in them.

So far, my code is:

def find_python(string, python):
 """searches for the letters 'python' in the word."""
 for eachLetter in python:
    if eachLetter not in string:
        return False
 return True

def main():
 python = 'python'
 how_many = 0

 try:
 fin = open('words.txt')#open the file
 except:
     print("No, no, file no here") #if file is not found
 for eachLine in fin:
    string = eachLine
    find_python(string, python)
if find_python(string, python) == True:
    how_many = how_many + 1#increment count if word found
 print how_many#print out count
 fin.close()#close the file

if __name__ == '__main__':
main()

However, my code is returning the incorrect number of words, for example, it will return the word 'xylophonist' if I put in the print statement for it because it has the letters python in it. What should I do so it will reject any word that has forbidden letters?

Upvotes: 0

Views: 2778

Answers (3)

eyquem
eyquem

Reputation: 27585

from os import listdir

def diagy(letters,li):
    return sum( any(c in letters for c in word) for word in li )

def main():
    dir_search = 'the_dir_in_which\\to_find\\the_file\\'
    filename = 'words.txt'

    if filename in listdir(dir_search):
        with open(dir_search + 'words.txt',) as f:
            li = f.read().split()
        for what in ('pythona','pyth','py','ame'):
            print '%s  %d' % (what, diagy(what,li))

    else:
        print("No, no, filename %r is not in %s" % (filename,dir_search))

if __name__ == '__main__':
    main()

Upvotes: 0

fiacobelli
fiacobelli

Reputation: 1990

Welcome to regular expressions:

import re
line = "hello python said the xylophonist in the ythoonp"
words = re.findall(r'\b[python]+\b',line)
print words

returns

['python', 'ythoonp']

If what you want is to find how many times the actual word python appears, then you should issue a re.findall(r'\bpython\b')

If you don't want to go this route, I suggest you return false if any of the letters of the string is NOT p,y,t,h,o or n.

Upvotes: 0

xvorsx
xvorsx

Reputation: 2442

Correct your test function:

def find_python(string, python):
 """searches for the letters 'python' in the word.
    return True, if string contains only letters from python.
 """
 for eachLetter in string:
    if eachLetter not in python:
        return False
 return True

Upvotes: 3

Related Questions