Am having a problem with mutation in ps5, and my is_valid_word function. It works for deciding if a word is valid but it also changes the hand so when i comes to playing the game although it will tell me a word is false correctly the hand it return is stillminus the letters I typed in . Any suggestions how to avoid mutating lists? Thanks
I assume you iterate over each letter in the word and try removing it from the hand. If you end up with a negative count for any letter in the hand you say it's not in the hand. You could undo the mutation after finding the result. Go through the letters you removed and add them back. (worst) You could make a copy of the hand data before doing your existing operation. That way it only mutates the copy and leaves the real hand intact. (better) Or you could come up with another way of checking that the letters are in the hand that doesn't mutate the hand. Try calling get_frequency_dict(word) and see if you can use that somehow. (best)
My second suggestion is easiest to implement. The last one is the hardest. You should probably use the second one to get your code working, then go back later and implement the last one. But save a copy of your working code before changing it. I usually make a complete copy of the function I'm messing with and rename the working one to something like is_valid_word_orig(). That way I can call either one easily and compare results, and look back at the code that was working when I've confused myself or broken something.
dmancine: Finally figured this out after a lot of trial and error. Your tip on calling get_frequency_dict actually made it much easier! Thanks.
I think I hacked up a terrible solution to it, too. But when you do PS6 they give you their solution to a lot of the PS5 problems, and that was how they did it. Seemed obvious, once I knew the answer. ;)
Join our real-time social learning platform and learn together with your friends!