UA-5095748-1

Thursday, December 5, 2024

What would be a good Python demo for non-technical students?

This is a follow up entry “How to teach programming to bright non-technical students?

Python is amazing for 3 key reasons

  1. Easy to learn - Simple, expressive, and versatile language
  2. Useful - Used in many applications, and well supported
  3. Productive - Rich libraries of tools and frameworks

How do I demonstrate the power of algorithm thinking and benefits of Python? I want to make the lesson more show and tell by building a word game solver. 

Wordscape and variations are fun games!

The rules are simple

  • Use the given letters in the circle to fill out the blanks words. 
  • Use each letter only once
  • Complete the level by filling all the blank words. 
  • Extra points given for words not listed

It’s frustrating sometimes with so many possible combinations of letters. The levels get more difficult with more letters used for guessing and more blank words to fill. I regularly get stuck while playing. It would be great to build a solver and show that I can build a solver faster than the time it takes to solve a few levels. 

Here I go

  1. Download a list of English words. I used a free wordlist with 69,903 English words. Credit to John Lawler of UMich. 
  2. Open the Python interpreter
  3. Read the file and do some data clean up
    • rawwords = open("wordlist.txt").readlines()
    • words = [word.strip() for word in rawwords if len(word.strip()) > 0]
  4. Set the given letters
    • findchar = list("swnho")
  5. I need to apply the game rules to filter down the list of ~70K words to a list of possible candidates. There are 4 rules.
    1. A word must be 3 or more letters
    2. A word must be shorter or same length of all the given letters
    3. A word use only the given letters
    4. A word use given letter only once
  6. Converting the English rules to Python code. There is one to one match for each of the above condition to the boolean conditions in the code.  
    • list(filter(lambda x: len(x)>=3 and len(x) <= len(findchar) and all(c in findchar for c in x) and len(set(tuple(x))) == len(x), words))
  7. Bam! Faster than you can read this line. Python spits out the solution. 
    • ['how', 'nos', 'now', 'ons', 'own', 'owns', 'show', 'shown', 'snow', 'son', 'sow', 'sown', 'who', 'whos', 'won']

In a few minutes and just with a few lines of code. I build a word game solver in less time than it takes to finish a few levels. This would work even as the level gets more difficult with more letters. This is the power of algorithm thinking paired up with a powerful and expressive programming language. 

Here is the code in its entirety 

rawwords = open("wordlist.txt").readlines()
words = [word.strip() for word in rawwords if len(word.strip()) > 0]
findchar = list("swnho")
list(filter(lambda x: len(x)>=3 and len(x) <= len(findchar) and all(c in findchar for c in x) and len(set(tuple(x))) == len(x), words))

For readers that are really paying attention, there is a major flaw with the solution! I’ll post the flaw and a better solution tomorrow. 

Can you find the flaw? 

No comments: