Wednesday, January 20, 2016

Week 2 - Day 3: Lambdas, stack and more recursion

Lambda functions: I don't understand why I ever found these challenging.  Maybe it's just that I've done a lot more coding now.  Or, more likely, that I'm more used to thinking in symbols.  Or maybe it's that I'd spent a good bit of time trying to figure out javascript anonymous functions back in November and so the general idea of anonymous functions is less mysterious...

I should say, rather, that I understand the challenges given in class.  The hardest part of understanding something is knowing when to apply your learning to solve real problems in a messy world.  That's what makes the transition from school to the working world so hard.  There's no correlation between "What we read in chapter X" and "Here's a problem to solve!" where you can reasonably expect that the solution will test recently learned techniques.

The simple stack problems in class went well, too.  I really kind of like this one for error-checking closures in html.  The basic problem was "given a string of html open and close tags, determine whether the sequence is correct.  Funtion should return True (for ok strings) or False"  So "<div><h1></div></h1>" and "</div><div>" would be incorrect while "<div><h1></h1><p></p></div>" would be correct.

To be fair, one of the instructors suggested a lookup dictionary to test whether stack and string elements were matched.

It's somewhat "unpythonic" for having a dual-purpose if loop that builds a dyamic tag lookup dictionary as well as an array of all tags, and passing both together to the item-comparison function.  The lookup dictionary, however, is built dynamically based on the input string and running through the html string twice with two different functions would be wasteful.  The dynamic build means that the dictionary could be used for an xml file with unique tags.

Drawbacks:

- my script doesn't check the lookup dictionary to see if a given tag is already present.  That would be trivial to fix.

- the script assumes all the tags were entered properly with < and >.  That kind of typo would not be caught automatically

- worst, the script would not cope at all with self-closing tags in HTML 4.  But then, this isn't meant to be a very useful script beyond teaching stack.


Some problems I'm still struggling with: recursion.  I can get some simple recursion functions to work on my own (Fibonacci and factorial), but I'm still struggling with predicting how each function call affects those downstream.  Enough on that for now.

No comments:

Post a Comment