r/madeinpython • • Sep 15 '21

Python Script to help my wife cheat at Spelling Bee by New York Times

Hello all, I'm a first time poster here and a newbie - sorry if this isn't the place for this.

I wrote the following python script for fun, my wife plays a word hunt game put out by the New York Times and she was having difficulty finding her last word. So I wondered if I could write something in python that would find all the possible words. This is what I came up with. I'm wondering if there's a way to modify the nested for loop so the user could tell the code what the longest/shortest words it should find. I'm thinking maybe I could change it to a while loop and have it call a function while a counter is below the value given by the user? I'm not sure, I plan on investigating ways to do that tonight.

Anyways, I would love any feedback!

https://github.com/TrippBikes/WordFinder/tree/main

23 Upvotes

3 comments sorted by

12

u/muikrad Sep 15 '21

Hey we can see you had fun with this!

I believe the nested for loop is probably flawed, and nesting indents is almost always a bad idea anyway. Try to keep results in a buffer like a list, set or dictionary. Or, use recursion (but this is harder and can overflow the stack even without bugs).

Something I noticed is that you learned a ton by yourself, but you did not took the time to research and see/use what was already done.

One example is string validation. You probably want to use a regex, or better, Python has a built-in "is_lower()" function on strings, to ensure your input is in the a-z range. But then again, why don't you accept capitals, and just call "casefold()" on the string to convert it to lowercase.

Another example, look at the itertools module for the combination functions. Python already includes optimized algorithms to extract various flavors of combinatorial.

There are online Scrabble helper sites that essentially do the same thing, you should try them and compare the results with your code. This way it will hint you in what words you didn't find, and you'll be able to hunt down the reason!

Good luck!

2

u/TrippBikes Sep 15 '21

Yeah, you got me pegged, I downloaded python during lockdown, learned a bit from a YouTube video and have basically just been fooling around with the couple things I know from there. While typing out the nested loop I knew it most likely wasn't the best way to do it.

Thank you so much for the advice, I'll work on the things you've suggested!

2

u/Fathrnature Sep 16 '21

I think it's a good first try. You had a problem and you solved it. Probably earned some points with the wife too.

The nested loops are very limiting. One idea - If you store the letters in a list along with the counts you could go through the word list once and remove those letters from the word with replace(letter, ""). Check the length before and after to ensure we didn't have more than the letter counts. When you are done make sure the word has no letters left with Len(), if so display it.

When you read in your word list I would skip any words less than your minimum length as well.

This also gives the added bonus of handling any length word.

I'm on my phone right now, so it's tough to really look over the code, but I think it's great to use scripting like this, and I'll try to see what you come up with.