Hi, In my attempted learning of python, I've decided to recode an old anagram solving program I made in C++. The C++ version runs in less than a second, while the python takes 30 seconds. I'm not willing to think it's just python being slow, so I was hoping someone could find a faster way of doing this. Also, I was wondering if there was a more builtin, or just nicer way of converting a string to a list (or using the sort function on a list) than making a function for it.
The words.txt here is just a copy of FreeBSD's /usr/share/dict/words Anyway, the code: import string # Need a function to convert a string to a list to be # able to use the sort() function def string2list(s): l = [] for i in range(0, len(s)): l.append(s[i]) return l words = [] found = [] anagram = raw_input("Find anagrams of word: ") f = open('words.txt', 'r') file = f.read() f.close() words = file.splitlines() sorted_anagram = anagram.lower() sorted_anagram = string2list(anagram) sorted_anagram.sort(lambda x, y: cmp(x, y)) while words: if len(words[0]) == len(sorted_anagram): wordlist = string2list(words[0]) wordlist.sort(lambda x, y: cmp(x, y)) sorted_wordlist = wordlist if sorted_anagram == sorted_wordlist: found.append(words[0]) del words[0] print "Anagrams of " + anagram + ": " while found: print found[0] + " " del found[0] -- http://mail.python.org/mailman/listinfo/python-list