Saving My Mom's Baby Shower in 10 Lines of Python
Sometimes you need to develop your own tools to solve a problem.
A few weeks ago, my mom sent a message to my family's group chat. She was putting on a baby shower, and wanted to play a word-unscrambling game she'd found online. The problem? It didn't come with answers. She and my dad were able to figure out most of the solutions themselves, but they were stumped on one answer, so they decided to crowdsource the information from their children.
I'm a firm believer in the old phrase, work smarter not harder. Therefore, I immediately googled 'word unscrambler' and typed in rceaats. No results. I went to the second result on google. It also failed to unscramble the word. The third result was also unsuccessful. What could I do?
I could write a program. It would only take a few steps:
- Generate all of the permutations of the word in question.
- Check which of those words are actual English words.
That didn't seem too hard, so I got started.
This is what I ended up with:
That was quick!
The time complexity of this function is super high (O(n!)), but since we're probably only using this on inputs of 10 letters or fewer, that shouldn't be an issue.
You're probably wondering what the answer was.
It was carseat.
I texted my family the answer and the baby shower was saved!
Okay, the baby shower probably would've been fine even if I hadn't solved the problem, but I was glad I could help a little.
I also figured out why the word unscrambling websites couldn't solve it. Most dictionaries consider carseat to be two words, so carseat didn't register as a potential correct answer.