Cheaters never win, but at least they can use Python.
Like many other nerds, I love word puzzles. I’m not always great
at them, and I don’t always have time to do them, but when I do, I
really enjoy them.
I recently discovered a new daily puzzle, known as “spelling bee”,
that the New York Times offers online. The idea is simple. There are
seven different letters, one in the center of a circle and six around
it. Your job is to make as many different words as you can from those
seven letters. Each word must be at least four letters long, and each
word also must contain the center letter. You can use each letter as many times
as you want.
So if the letters are “eoncylt”, with a center letter of “y”, some
of the words you could create might be “cyclone”, “eyelet” and “nylon”.
The online game gives you a score based on how many words you’ve
made from the potential pool. If you get them all, you’re
awarded “queen bee” status.
I do pretty well at this puzzle, but I’ve never managed to find all of
the hidden words. Nevertheless, I have become queen bee on a few
occasions. How? The answer is simple. I cheated. How? Using
Python, of course.
Now, cheating at games isn’t necessarily the first order of business
when it comes to programming. And cheating at word games in which
you’re competing against yourself is probably a sign of unhealthy
competition. But, doing so also provides a great way to review some of the
ways you can use Python’s built-in data types and the ease
with which you can process words and text.
So in this article, I explore a number of ways you can cheat—and yes, become the queen bee, if only for a day.
Trying All Combinations
To start, you simply might try to form all of the possible
combinations you can with the letters you’re given. As you might
remember from high-school math class, there’s a difference between
“permutations” and “combinations”. When you generate “permutations”,
the order is important, but when you generate “combinations”, the order
is not important.
You easily can see this using Python’s
itertools module, a part of
the standard library that has functions named
combinations. Each takes both an iterable data structure and the
number of items you want in each resulting list. For example:
Powered by WPeMatico