Skip to content

Business Python Posts

How to use Python to cheat at word games

As you probably know, Python is really great at parsing text, filtering and sorting. What you might not know, as I didn’t until yesterday (when I went looking for it), that there is a PyPI library available with lists of English words, making it easy to import a standard word list for all your searching needs.

I have to make a confession here. Over the Christmas holiday, I allowed myself a little indulgence of playing (single-player) word games on my phone, in which I had a limited selection of letters with which I had to make English words of given lengths. Only problem is, sometimes I got stumped by the letters I was given. No matter how long I stared at the screen, I just couldn’t see any more words that the app was expecting me to deliver – and I couldn’t skip the rest of the level, either.

This functionality was of course by design. For a price, the app will provide hints to help you solve the hardest puzzles, but out of principle, I just couldn’t bring myself to pay real money for this. On the other hand, if I couldn’t solve the puzzles without hints, then I was blocked from continuing to play.

You can see where this is going.. 🙂

First, to install the English word list, I used pip (I did it on a Python 3.8.5 environment but I expect it will work equally well on other versions):

C:\Users\Agent Smith>call C:\ProgramData\Anaconda3\Scripts\activate.bat experiment3

(experiment3) C:\Users\Agent Smith>pip install english-words
Collecting english-words
  Downloading english-words-1.0.3.tar.gz (352 kB)
     |████████████████████████████████| 352 kB 3.2 MB/s
Building wheels for collected packages: english-words
  Building wheel for english-words (setup.py) ... done
  Created wheel for english-words: filename=english_words-1.0.3-py3-none-any.whl size=352088 sha256=bb189ee3b3c209f8b3608b1dfc75709e794ea6f25ddd1a9a13ea27addb763963
  Stored in directory: c:\users\agent smith\appdata\local\pip\cache\wheels\a8\e6\be\028f88f1b1fd742fa710dd72a583813f6245b8528b8e6cbaf8
Successfully built english-words
Installing collected packages: english-words
Successfully installed english-words-1.0.3

(experiment3) C:\Users\Agent Smith>

Then, having launched the Python interpreter, I defined the following (very simple) function, right from the interactive prompt:

>>> from english_words import english_words_lower_alpha_set as words
>>> def findword(letters, length=0):
...     return [word for word in words if (length in [0, len(word)]) and (set(letters) >= set(word)) and all([list(letters).count(letter) >= list(word).count(letter) for letter in letters]) ]
...
>>>

What the findword() function does is, given a text string (the letters variable), it returns a subset (in the form of a list) of the english_words.english_words_lower_alpha_set word set provided by the installed package. The subset is composed of the words that match all three criteria:

  • length in [0, len(word)]: If a nonzero length is included as an argument in the function call, then we want to capture only those words that have the exact length specified
  • set(letters) >= set(word): words captured by the filter should not include letters that are not available, though not all available letters have to be in the words captured
  • all([list(letters).count(letter) >= list(word).count(letter) for letter in letters]): here we ensure that letters are not used more than they are allowed to be used. (If your game allows re-use of the letters you have been given, then you may leave out this last condition.)

Here are a few examples of findword() in action:

>>> findword('skhay')
['a', 'ah', 'ak', 'as', 'ash', 'ashy', 'ask', 'h', 'ha', 'has', 'hay', 'hs', 'k', 'kay', 'ks', 'ky', 's', 'sa', 'say', 'shaky', 'shay', 'shy', 'sky', 'y', 'yah', 'yak', 'ys']
>>> findword('lodge', 4)
['doge', 'dole', 'geld', 'gold', 'loge', 'ogle']
>>> findword('grica')
['a', 'ac', 'air', 'ar', 'arc', 'c', 'ca', 'car', 'cia', 'cigar', 'crag', 'craig', 'g', 'ga', 'gar', 'i', 'ia', 'ir', 'ira', 'r', 'rag', 'rca', 'ri', 'rica', 'rig', 'riga']
>>> findword('tupes')
['e', 'es', 'est', 'et', 'p', 'pest', 'pet', 'ps', 'pus', 'put', 's', 'se', 'sept', 'set', 'setup', 'st', 'step', 'sue', 'suet', 'sup', 't', 'ts', 'u', 'up', 'upset', 'us', 'use', 'ut']
>>> findword('skeet')
['e', 'eke', 'es', 'est', 'et', 'k', 'ks', 's', 'se', 'see', 'seek', 'set', 'skeet', 'st', 't', 'tee', 'ts']
>>> findword('stluo')
['l', 'lo', 'lost', 'lot', 'lotus', 'lou', 'ls', 'lust', 'o', 'os', 'oust', 'out', 's', 'slot', 'so', 'sol', 'sou', 'soul', 'st', 't', 'to', 'ts', 'u', 'us', 'ut']
>>> findword('blame')
['a', 'abe', 'abel', 'able', 'al', 'alb', 'ale', 'am', 'amble', 'b', 'bale', 'balm', 'bam', 'be', 'beam', 'bel', 'bela', 'bema', 'blame', 'e', 'el', 'elba', 'elm', 'em', 'l', 'la', 'lab', 'lam', 'lamb', 'lame', 'lea', 'm', 'ma', 'mabel', 'mae', 'male', 'mba', 'me', 'meal', 'mel']
>>> findword('blame', 4)
['abel', 'able', 'bale', 'balm', 'beam', 'bela', 'bema', 'elba', 'lamb', 'lame', 'male', 'meal']
>>> findword('damel', 4)
['dale', 'dame', 'deal', 'lame', 'lead', 'made', 'male', 'mead', 'meal', 'meld']
>>>

Of course, there is an ethical component to all this. Personally, I don’t feel bad about using my own code to get past the blocks in the app that (IMHO) ought not be there to begin with. On the other hand, one could imagine using code like this to obliterate one’s friends (or enemies?) while playing something like Words with Friends. I personally wouldn’t do that because I would want the satisfaction of knowing that I won fairly, but I can’t stop you. 🙂

Leave a Comment

My “Mad Men” Moment

I recently wrote about my quest to produce a book cover for Business Python – a cool story and a lot more challenging than I had first anticipated! Check it out here. (This “friend link” to medium.com doesn’t count against your free quota of Medium articles, in case you don’t have a paid subscription.)

Leave a Comment

Thank you!

The launch phase of Business Python: an example-based guide is complete. Many thanks to those who helped me get the word out via email and social media. Because of you, many people have found and ordered the book, and are now able to start automating tasks and doing things that never would have been possible before! Check back on this website for more content as it is available, and as always, I would love to hear your feedback as you dig into the book and start to use this powerful tool for yourself! You can reach me via the contact page or at author@business-python.com.

Leave a Comment

Book Launch: “the million dollar giveaway”

Days
Hours
Minutes
Seconds
Oops, looks like you just missed the launch window. Be sure to subscribe to the newsletter for future deals and offers, as well as all the latest code and videos!

My name is Theodore Deden and I would like to ask for your help to give away a million dollars’ worth of intellectual product in just a few short days.

As I will explain, this is not as crazy as it sounds.  You see, I’ve written a book.  It’s a great book about my on-the-job experience with the Python programming language and how this skill I casually picked up has transformed my business career.  The book is called Business Python: an example-based guide

Here’s my challenge.  The people who can benefit from this bookbusiness people who work with standard office software all day longaren’t looking for it.  They don’t know that Python exists, or if they know, they don’t know that it can help them.  They think Python is something for hackers and IT people only, even though I know that that isn’t true.  

I have learned from experienced publishers that I need a critical mass of 10’000 people to read my book just to “put it on the map”.  From that point, it will live or die based on its own merits – I am convinced that Business Python will live, because I have experienced firsthand just how transformative this information can be, and because I know I have created a quality product.  But how to get that critical mass of the first 10’000?

My answer: The million dollar giveaway.  Let me explain how it works:

The printed book, distributed through the Amazon network, is worth 59 dollars – this is what you have to pay for a book of this type with this quality of material.  But until the end of November (through Cyber Monday), I’m setting the Amazon price at the absolute bottom limit of what Amazon will let me setUnder 9 bucksOver a 50 dollar discount on the printed book, and I won’t be earning any royalties on the sales at that price. 

I have taken the same approach with the Kindle version, normally priced at 29 dollars but reduced to just 2 bucks during this launch phase in November

On this website, business-python.com I have even more flexibility.  There I have a downloadable PDF file version of the book that is normally priced at 30 dollars, but is absolutely free with the coupon code LAUNCH2020.  And for those who prefer audiobooks, I have created an abridged audiobook version priced at 10 bucks, but again, absolutely free with the LAUNCH2020 coupon

To summarize, that’s a discount of 50 bucks for the print book, 27 for the Kindle, 30 for the PDF and 10 for the audiobook.  Altogether, that’s 117 dollars’ worth of stuff per person that I am giving away during the initial launch over the Thanksgiving weekend.  These are high quality products that deserve their normal retail prices.  I’m not giving them away because they are junk, but for the opposite reason: I want people to recognize them for the high quality products they are.

I aim to build that critical mass of 10’000 people who have read my book so that others in the future will come to know it and be able to benefit from it.  If I succeed in this, I will have given away as much as 1.2 million dollars in free stuffmaybe more if I overshoot and get more than 10’000 people to download and order my book during this time.

Now you know why I’m doing this million dollar giveaway, what about you?  Are you ready to let Python transform your career, as it did mine?  After all, this would be a perfect time with all the COVID lockdowns, especially with the holidays coming up.  Why not take a little of that time in lockdown to start learning a new skill that is easy to learn and will yield benefits for the rest of your life?  

How about Christmas gifts?  Your friends don’t have to know that you bought the book during the launch phase discount – by the time you give those extra copies to them, the price will be back to normal.  They will be impressed and grateful for your generosity. 

Is this book for you?  Maybe.  What I know for sure is that there are a lot of people out there who can benefit from it, even if they are in non-technical roles and even if they don’t have an affinity for programming.  Want to see a preview?  Check out this trailer:

 

So please, help yourself to your share of the free and drastic-sale goodies in my million dollar giveaway.  But beyond that, I would just ask that you please help me get the word out about this giveaway, so that others like you can benefit, too.  Together, we can put this book on the map, where it deserves to be. 

And who knows?  Maybe we transform the world in the process.

Are you ready to give Business Python a try?

It will never be easier or cheaper than right now. Remember to use the coupon code "LAUNCH2020" for the digital products on this website (valid in November only!), or click on the links provided to take you right to the Amazon page of the book!

Will you help me get the word out, and help others take advantage of this rare opportunity?

Amazon reviews are one of the best ways to let people know what you think of Business Python: an example-based guide

The links on the right will take you right there – you can be done in just a minute or two!

Theodore Deden is giving away free copies of his book during the November launch. I got one for myself. Check it out!

-You

If you use a computer for your daily work but you haven't heard about Python, you should check out this site - absolutely life-changing!

-You
Comments closed