You can wisely kick it out of that block, to the function level, for instance. Actually this is not the case in load_words().įirst of all, the try block is not the suitable place to declare filename = "dictionary_2.json". To add to the previous answer, it is valuable to mention that a good practice consists in making the try block as light as possible in order to focus only on the code which is susceptible to stumble. Once you've mastered them, move on to dawgs. Find yourself a good tutorial on tries, and learn how they work. You end up doing only 120 lookups total, instead of the tens of thousands of lookups your present approach is doing.Īn even better technique though is to construct a trie or a dawg (a Directed Acyclic Word Graph) from your dictionary, and recursively descend through the graph to discover what words can be made from the rack. Now you are doing seven lookups instead of looking up each of the (again) 5040 six letter permutations from a rack of seven. How do you find the sixes? You look up EINSTV, AINSTV, AENSTV, AEISTV, AEINTV, AEINSV and AEINST. With my approach you sort it to AEINSTV and look that up in your map to discover that the three seven letter words you can make with those letters are NAIVEST NATIVES VAINEST: that's one lookup instead of 5040. In your original approach you generate the 5040 possible re-orderings of that rack and check them all. How much less work? Suppose you have NVSAIET on your rack and you wish to know all the seven letter bingos. Now when you have a rack and you're looking to see what words it forms you sort the rack by letter order first and then you only have to consider the combinations and permutations of letters in alphabetical order to look up which ones are legal scrabble words. #finds all words that can be made with the input by matching combo's to the dictionary and returns them # takes letters from user and creates all combinations of the lettersįor s in scrabble_input(a + a): #make dictionary shorter as there will be maximum 7 letters Valid_words = json.load(english_dictionary) With open(filename,"r") as english_dictionary: #open file and read the words, output as a list At the moment it has a few "loops in loops" and others things that will slow down the process. I'm making a python script that accepts 7 letters and returns the highest scoring word along with all other possible words.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |