Blah. Tests.

So we just wrote the second test yesterday. I really hate sometimes how hindsight is 20/20. You’re sitting there going through the test and thinking about your answers, you write down some code, and you eventually finish the test. Then you re-read it and are checking your answers and the time is up – and you’ve just realized some mistakes in your code.

But it’s too late to fix it. Just another minute! Just another two minutes! That kind of stuff really sucks.

Anywho, let’s talk about what I did wrong on my test (to my knowledge)…

Question one had us working with binary search trees and posed the question of whether or not an inorder traversal is a sorted list. The fool that I am, I totally forgot that in a binary search tree, all nodes to the left of a root node are smaller than the root node. So you can probably interpolate that I answered no to the question when the answer is, yes, an inorder traversal of a BST is indeed an ordered list.

Here’s the binary tree I constructed as an example (or it was something like this):

5

4            6

3          7            9

So the inorder traversal of that tree would be 3,4,7,5,6,9. The seven and the 5 are out of order. I thought I had proved my point – but at the end of the test, when I was quickly making sure of my answers, I realized that this -wasn’t- a BST since 7 is not allowed to be to the left of 5 in a BST. I did not have time to fix it. I was fixing other mistakes when the time ran out.

The other thing I made a mistake on was returning None if a tree whose leaves were removed became empty. I forgot to make that happen in my code for question #3. It’s such a small, easy-to-do thing, but yet I only realized that I had neglected it in the last 15 or so seconds. Quite literally the last 15 or so seconds. I remember the feeling in my gut.

I know striving to write the correct, perfect code, the first time around is what we should aim for. But that’s quite difficult to do, and I also know that. I should have taken a bit more time to think through what I was doing, probably. But dang, that sucks. Identifying mistakes yourself, -during- a test and simply not having the time to fix them.

Damn. Damn, damn, damn. Ah well. You live and you learn.

Advertisements

Exercise 3 and Assignment 2, Frustrations and Success

Exercise 3 and Assignment 2 were challenging. I quite enjoyed  coding  their  functions, but I won’t lie and say I wasn’t partially defeated by Assignment 2. I know there was an easier way of coding all_regex_permutatins, and I was determined to figure  it out on my own before the deadline of the assignment. Unfortunately  I  could not, and had to submit the ‘find-all-permutations-and-check-them’ method.

That being said, coding regex_match and seeing it work was a real joy. It really was much fun. The biggest hitches I had with that function had to be handling DotTrees and StarTrees, but I got them to work, and my tests completed. Though I’m not sure I successfully tested every scenario of regex, so I’m hoping all goes well during the marking.

Now when it came to Exercise 3, part A was a breeze. PArt B took some thinking, however. Finding out how I wanted to utilize a helper function to identify the deepest paths was the trick. It really m ade me appreciate the use of helper functions in a function itself, especially for recursive purposes. It’s really interesting though – because Stacks (an iterative method) and recursive functions can both achieve the goals of some functions, but deciding which one to use is a bit of a grey area for me.

I’ve usually favoured the recursive code over using Stacks, but I always wonder if it would be terrible to use Stacks instead – considering they’re such an awesome ADT. Then again, I don’t even use an explicit Stack. I just create a list and treat it as a Stack for the purposes of my function.

Recursion is Beautiful

Reading week is over and we’re back at it in classes now. I played an absolutely unhealthy amount of Dark Souls during my break, and of course did work as well! I also made my presence on the course discussion board again, which I haven’t done in a while. I missed my post last week as well, which I can’t make a good excuse for, but I’m aware of it!

I spent a bunch of time looking at Assignment 2 and understanding it. I actually thoroughly love using regular expressions. My first real foray into it was not very long ago, where I looked into the nuances and limitations of regular expressions for a website I was working on as a hobby. It’s really cool what a well-designed RegEx can identify for you.

After working on and finishing part 1 of the assignment, then talking to Dan about it and refining my code (it’s pretty damn sexy code now, if I do say so myself), I took a break. This was primarily due to the fact that part 2’s base is only to be released to us after part 1’s due-date was reached. Then I realized that I could just use placeholder names for classes until such time as I knew what they were!

So I started working on part 2 of A2. And man, has it helped me see how absolutely stunning recursion is. I was a bit worried earlier, because Dan had mentioned in class that he felt that the concept of recursion and utilizing it is like a gateway concept for most computer scientists/programmers. I was worried about the possibility of me not grasping this concept. But I’ve got to say, once the grasp is in place, it is, quite frankly, a whole new world. *Cue Aladdin music*

No but really. Really.. Recursion makes writing these seemingly complex functions so simple at times. Remembering back to when I first looked at the details for A2 Part 2, I thought to myself, “Oh boy. That’s going to be interesting to code,” as I looked at the function list. But when I am actually sitting down and working on said functions, I think and realize how useful recursive calls are. How simple they are, and then all of a sudden, I know what to do. And I write the code, and… “Wow.” It was that simple? It boggles the mind.

And there’s this kind of unspoken joy I experience when I comment a section of code. I format the comment, even. I also love – absolutely LOVE getting a bunch of things done in few lines of code. Of course I’m also very much for readability, but minimizing lines of code is one heck of a thing I’m into as well. Recursion just makes me so happy.

Damn.

‘Til next time!