Wednesday, January 27, 2016

w3d3 - Linked Lists and binary trees...now with classes!

I have to confess that today was...intense.  It's painfully clear that I know much less about how classes work than I thought.  It was honestly a good lesson in humility for me, and it was a reminder of how much work I have to do.

 Everything was working by the end of the day, even a little extension project on recursive pathfinding through a graph:

graph = {'A': ['B', 'C'],
         'B': ['C', 'D'],
         'C': ['D'],
         'D': ['C'],
         'E': ['F'],
         'F': ['A','C']}

where each key is linked by a one-way path to it's  corresponding nodes, which are in turn linked to other nodes.  Given a start and an end, can you find a path between.  I can get it to work for ONE path, and screen out loops (see that tricksome 'C' --> 'D' --> 'C'-->...) but not to collect ALL paths, or to find the shortest path.

The first task of the day was creating and modifying linked lists.  For linked lists, I could make a "Node" class with  'data' and  'next node' properties.  Using functions,  I could print out the nodes in order, add new data to the tail of the node, return data from a node, and insert a node between two others.  Do it classes, either within the Node class or in a separate "Linked List" class, though?  Nope.  Or, I should say, 'not yet'.  I will get this because I need to know classes better, to really understand how data is held.  I've seen some really elegant solutions online, and I'm debating continuing to try to write my own, or to look at a professional-grade one and try to reverse engineer it.

After linked lists came binary trees.  The concept is fascinating.  I love that a thing that looks so disordered at first glance holds such structured data.  I could get a basic recursive tree-reader function working after a lot of pain, dead ends...and mis-read directions.  The solution to printing out the data in a binary tree in order (least-to-greatest) using a recursive algorithm was painfully simple once discovered.  Getting there?  Very challenging.

I will say that I really love the concept of recursion. There's something really satisfying about defining a data-processing approach that takes in bites from a big pile until it's done.  Or better, than spawns copies of itself to check out branching paths and report back.

Working with classes + recursion ==> painful.  These are hard for me in part because it's a lot more laborious to prototype in the REPL using classes.  Simple functions I can do there easily, or to test out methods on default classes. (huh.  Maybe if I work on treating my created classes like the built in classes (string, list, hash, etc) I'm more comfortable with, the syntax will make more sense.).

Tomorrow is another day.  I'll set an alarm to wake up early and try the classes with linked lists project one more time.

No comments:

Post a Comment