[Tutor] HELP ON A ANAGRAM PYTHON PROGRAM
jessica cruz
jessica06cruz at yahoo.com
Mon Mar 9 19:38:19 CET 2009
I made this program but it says that there is an error and I have a hard time trying to solve the problem with program. Here is the program:
#this reads all of the words in the file into a list
infile = open('/afs/cats/courses/cmps012a-cm/pa1/wordList.txt')
wdcount = int(infile.readline()) #first item is count of all the words
word_list = infile.readlines()
#print them out from the internal list
i = 0
while i < wdcount:
print word_list[i],
i = i + 1
#map of alphabet to prime numbers with respect to frecuency
#more frequent the letter the smaller the assigment prime
letter_to_prime = {
'a':7,
'b':59,
'c':29,
'd':31,
'e':2,
'f':67,
'g':41,
'h':53,
'i':3,
'j':97,
'k':73,
'l':23,
'm':47,
'n':13,
'o':19,
'p':43,
'q':101,
'r':11,
's':5,
't':17,
'u':37,
'v':71,
'w':79,
'x':89,
'y':61,
'z':83},
j = 0
while j < wdcount:
print word_list [j],
prod = 1
i = 0
while i < len(word_list[j])-2:
prod = prod * letter_to_prime[word_list[j] [i]:
i = i + 1
print prod (right here is where it says that there's an error I try to fix it )
j = j =1
# code that will be compared will be a histogram type code with frequency
# characters
def code(w):
hist = []
chars = list(w)
chars.sort()
for letter in chars:
if not letter in hist: # when the letter is not already in hist,
hist.extend([letter, str(w.count(letter))]) # its added to hist along with its freq.
else:
continue
coding = "".join(hist) # then they are joined as one string
return coding
# new list is made with words in word_list followed by its code
for word in word_list:
wordList.append(word)
wordList.append(code(word[:(len(word)-2)]))
while True:
word1 = raw_input('Enter word:')
word = word1.lower()
sig = code(word)
i = 1
if sig in wordList:
print "Anagrams:"
while i <= len(wordList): # when the sig of the inputed word is in the word list,
if sig == wordList[i]:
print wordList[i-1] # the corresponding words are printed
i += 2 # then adds two because codes are every other entry
else:
print "No anagrams"
choice = raw_input("Continue? (yes/no)")
if choice == 'y' or choice == 'yes':
continue
else:
break
I don't know how to figure out the error since the only message that I get is that "there's an error: invalid syntax"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090309/f093e484/attachment.htm>
More information about the Tutor
mailing list