(Re)Learning to Code: Software Design Patterns

Believe it or not I got a Computer Science degree as an undergrad at Brown, back in 1982. Hot shit! Then I went to work at BBN in Cambridge, MA as an entry level programmer. BBN (still there, part of Raytheon now) is the think tank that invented a lot of the hardware and software of the ARPAnet, for DARPA. This network, built for the Defense Department, was the predecessor of, and evolved into the earliest part of the Internet. I was working alongside ultra-alpha geeks who were, at the time, some of the greatest software minds alive. Fun, but like being a Little League player in the outfield, playing in the MLB World Series. They were so much better than me at everything related to software engineering that quickly, they just said to me “Please, stop. Here is an important job that involves very little coding you can do.” Which I did, before getting my MBA and going over to the Dark Side – Marketing. But the idea that I suck at coding has been part of “who I am” since then.

A few years ago, just post-retirement, I took a Coursera class to learn Python. At the time I didn’t really have anything I wanted to do with it. I finished the class, it was fun, but without a project and ongoing practice I forgot a lot of it. Now, with an interest in home automation, I have more stuff I’d like to do with code. I have a Raspberry Pi, and a bunch of entertaining ideas for what to make it do.

I’m taking a Udemy Python 3 class now to refresh my memory and update my knowledge. I just did a homework assignment: write a Python function to check if a string is a pangram or not. A pangram is a string containing every letter of the alphabet at least once – famous example: “The quick brown fox jumps over the lazy dog.”

Here’s my solution:

import string

def ispangram(str1, alphabet=string.ascii_lowercase):
    # Build list of alphabet characters
    alphabet_list = [c for c in alphabet]
    # lowercase input string, then iterate
    # through string deleting each character
    # from alphabet_list until alphabet_list
    # is empty (True), or end of input
    # string (False)
    str1 = str1.lower()
    for c in str1:
        if c in alphabet_list:
            if len(alphabet_list) == 0:
                return True
    return False

Complete with a list comprehension, and the remove() method. Works fine, doesn’t get confused by punctuation or whitespace, seems fairly straightforward.

Here is the instructor’s solution:

import string

def ispangram(str1, alphabet=string.ascii_lowercase):  
    alphaset = set(alphabet)  
    return alphaset <= set(str1.lower())

Yikes! OK, so my solution works and there is nothing wrong with it, but by comparison, it is inelegant and inefficient. What is the difference between mine and the instructor’s? The instructor recognized that the essential nature of a pangram check is testing for set membership. Once you have that insight, if you know that Python has a set data type (yeah, I know that), then the better approach is obvious. Use set() to cast the test string to the set of characters in the test string, then compare this set to the alphabet set. If the alphabet set is a subset of, or equal to, the test string set, the test string is a pangram. Duh!

I get it. It isn’t complicated. But how do I learn to have insights like that? How do I learn a toolbox of common software design patterns (preferably in Python) and how to recognize when a problem fits one of those patterns? Back in the day, I was rotten at this and it was one of the defining parts of “I suck at coding.” But I am sure a lot of other people start out rotten at this, but nevertheless learn that toolbox of patterns, and how to recognize when a pattern fits a problem.

Nerd friends, I am listening! Do you have suggestions for learning this? Favorite Python design pattern books (or Jupyter notebooks)? Exercises to sharpen my insight? How did you, back when you were wet behind the ears, learn that toolbox of patterns you now apply subconsciously as you work?


Quick fox and lazy dog cartoon: http://clipart-library.com/clipart/1280964.htm licensed for personal use.

Instructor’s code from Udemy class – used under fair use principles.

One thought on “(Re)Learning to Code: Software Design Patterns

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s