Working on ps6.py, #3. Maybe I'm having a dead brain moment, but how should I be going about creating a list of all possible word combos in my current hand? I know I could set up a massive nest of loops to go through all the combinations in the hand, but that would take forever and I'm assuming there's an easier way to do it. Any help someone could provide would be great!
I just finished that one, and got a computer player that will come back with the right answer in about .4 seconds on my macbook: just go through the words in your word list one by one and run is_valid_word() on each one. If it is valid, grab the score and go to the next one. When you get all the way through the word list, take the word that had the highest score and return it. is_valid_word() should be subtracting the letters in each word out of your hand, and will return False if any of the letters in your hand go below 0. To see if it couldn't find a valid word, just test if the max score is 0 after you've finished going through the dictionary, and return '.' to end the hand. Not too elegant, but it doesn't make any mistakes and it's verrrry simple. Let me know if you have any questions on how to code it. Good luck!
I made a dictionary with the hand mapping each letter to its point value, the idea being I could check and score each word faster. Then I checked each word... -first I checked if word length was over hand length. discarded and move on to next word if it was. -then I checked each word letter by letter to to see if that letter was in the hand dictionary. First fail=discard and move on to next word. I kept score as I went and if all letters were in word at the end the score got checked to see it was bigger than the previous score and if it was it replaced it. -in hindsight it would have been even faster to check each candidate word score(using dictionary made w/ get_words_to_points), and if it was under the current high score (from previous step) to not even look at it and go on to next word. It took a few short functions linked together to do this and was pretty fast.
@baronvonholbach: Interesting, so you're taking all the words in the entire wordlist and checking if they are in your hand? I was thinking of taking the opposite approach, looking for all possible words in the hand and comparing them to the wordlist for validation. Your method might be the better way to do it simply. I'm sure there's a more elegant way, but that makes sense. Thanks!
One more "elegant" way would be to check scores before or rather than words... Dictionaries are super fast to search. -Max= max possible score of your hand, Min=0 -For word in word list: if Word_Score(from dictionary you made as first step) > Max or < Min: Continue -If score is under Max, and over Min, check word, if it matches save word and now Min=Word_Score. -Repeat
Join our real-time social learning platform and learn together with your friends!