Saturday, January 16, 2021

Dynamic Programming with Python and More!

Last week I alluded to the fact that I've been working to improve and increase my Python skills using resources I've found online. One of those resources has been using Leetcode. I was initially inspired by an article, which I can't seem to find, that had a handful of recommended Leetcode exercises to try as part of preparation for a technical interview. While I haven't been working on any recently, I was finding that, by attempting them in Python and reading the comments on the exercise when I got stuck, I was expanding my knowledge of both Python and algorithms. In the coming weeks I hope to start working on more Leetcode problems.

freeCodeCamp is another resource that I highly recommend.  I think initially they started with courses in web development, covering HTML5, CSS, Java, and Javascript, among other things.  They've greatly expanded their resources in the time since I've discovered them.  I'm signed up for their weekly email digest and it includes links to five different articles and videos covering a variety of topics.  Their main YouTube channel now has a bunch of full-length courses.  One such course covers two dynamic programming techniques:  memoization and tabulation.  So far I've only finished the exercises in the memoization part of course and it has taken a bit of effort to translate from the Javascript code that is presented in the course to Python.

So what is dynamic programming?  In short, it is a way of optimizing recursive problems.  In the case of memoization, it allows a recursive program to remember values it learned in previous steps so that it doesn't have to find them again.  This method allows a programmer to reduce what is known as the time complexity of a problem, allowing larger initial inputs to run through the recursion faster.  And faster is better: everyone would prefer to sit waiting at a computer screen for less than a minute instead of an hour.

In Python, memoization can be a little tricky to implement, as I discovered.  The technique requires a memo dictionary to be added as an optional input to the recursive method.  The problem I encountered seems to have to do with how Python initializes keyword parameters, which didn't cause issues in the first few exercises of the course, but started interfering with later results.  After doing some googling, I discovered a work-around, which might not be the cleanest way of doing it, but it worked for me.  Instead of adding a keyword parameter that was an empty dictionary upon initialization, I set it to be a Nonetype.  Then, the first thing I checked for in the method was if it had a value of None. If it did, I created a empty dictionary; if not, I left it alone.

I have yet to finish the tabulation portion of the dynamic programming video course, but I'll keep you posted if I learn anything interesting. There are a few other videos on freeCodeCamp's channel covering algorithms that I would like to go through as well.  In the meantime, I've also started working on one of my Python projects.

This first project is a bit of a pet project that I've been wanting to do for a long time, and my learning goals for it have evolved over the years. Initially, I was just going to learn a database program, specifically Microsoft Assess, to build a searchable catalog for my own personal music library. I knew of an organist that had a note card catalog that he used to quickly search through his music. After giving it some thought, I decided I wanted a little more functionality than what note cards give, so I thought an electronic format might better serve my needs. I never got any further than that (being in grad school and all). Now, with my explorations in programming, I've decided I might as well go whole hog and write my own user interface.  (Did you know you can create GUIs in Python?  Neither did I.  Now we know!)  The added benefit of this is that I can distribute my program to my fellow church musicians for them to use as well.

Right now, I'm trying to finish up the design phase. I've laid out a top-down design chart with the functionality my program needs to have. There's a list of the information I need to include for each record. I have a couple of logic flowcharts describing what needs to happen for some of the functionality to work. Now I just need to finish sketching what various parts of the GUI should look like. This coming week I hope to have a GUI prototype finished so I can get feedback on the design from some friends of mine. How do I plan to build the GUI? As hinted above, I'm going to be using a toolkit (wxPython) to build it out. I already completed a toy application earlier this week to get a feel for how things are constructed.

Next week, in addition to my application's progress, I will share a little bit about what I'm picking up in perl.  Until then, I'll keep on coding!

No comments:

Post a Comment