Hi, I'm new to Python and need some help with this program. I'm trying to take 10 jumbled words at a time and have the program unscramble them and then print them out like: word1,word2,wor d3, etc.
So far the code will unscramble more than one word at once but it will print the answer several times if there is more than 1 of the same letter in the word and it will print them in a column. Example is inwdwo and garnama (window anagram). It will print them like this:
window
window
anagram
anagram
anagram.
I read that a set can help this but I'm not sure where to put that or how to get the %s,%s,%s type of print out. Any help?
So far the code will unscramble more than one word at once but it will print the answer several times if there is more than 1 of the same letter in the word and it will print them in a column. Example is inwdwo and garnama (window anagram). It will print them like this:
window
window
anagram
anagram
anagram.
I read that a set can help this but I'm not sure where to put that or how to get the %s,%s,%s type of print out. Any help?
Code:
import string
def anagrams(s):
if s == "":
return [s]
else:
ans = []
for an in anagrams(s[1:]):
for pos in range(len(an)+1):
ans.append(an[:pos]+s[0]+an[pos:])
return ans
def dictionary(wordlist):
dict = {}
infile = open(wordlist, "r")
for line in infile:
word = line.split("\n")[0]
dict[word] = 1
infile.close()
return dict
def main():
anagram = raw_input("Please enter words: ")
wordLst = anagram.split(None)
diction = dictionary("wordlist.txt")
for word in wordLst:
anaLst = anagrams(word)
for ana in anaLst:
if diction.has_key(ana):
diction[ana] = word
print " ", ana
main()
Comment