Sunday, 1 March 2015

SLOG 5: Tracing Recursion ALL OVER AGAIN

Hey all!! Hope everyone had a good reading week and a great recovery from the reading week. I sure did. I definitely feel this was a productive reading week where I caught up to most of my course work and studied extra hard to be prepared for the second half of the course. 

This week we are asked to revisit one of the topics previously written and to be honest I love that idea. I feel that revisiting something you have already written not only helps you to understand the material better but it helps you fill in those gaps and detect the important information from the not-so-important/ necessary info. I literally re read my slog for recursion and then looked at my test and the only thing that went through my head was
                'WHY DIDN'T I JUST READ MY SLOG BEFORE THE TEST!!!' 
Reason for this reaction, well I did super poorly on the recursion question of the test mainly because I thought it was a smart idea to not write each of the tracing steps clearly. I get it now that that's the whole point of tracing. Well there is still room for improvement and I am learning from my mistakes. One thing I mentioned in my previous SLOG on recursion was that at that time we didn't have to worry about writing the code for recursion. However, now by week 7 we have gone over some coding for recursion. To be honest, when asked not to worry about the code for recursion I was scared it might be something super difficult. But turns out it isn't really and the coding aspect actually helps understand the tracing part even more.

I feel my last post on recursion did a pretty good job at defining and explaining what recursion is, so I will not repeat that here. You can actually just re-read that part from my previous post. However  I will write a little bit about the coding part of recursion. Its honestly simple if you break it down into two steps, just how we did while doing the class activities. These 2 steps are:
1) Write the base case: this is the case where no recursion is needed. It helps to write the  examples and see the simplest example and turn that into the code. 
2) Recursive code: This is the main part of the code. More like base case is the appetizer while recursive is the main course(Lame... I know :( ). This is the part of the code that will actually do the tracing. It usually involves multiple if/elif statements that ensures that all conditions have been met. 

Here is an example of the code that I stole from one of the class notes posted on course website:

def koch(t, order, size):
    if order == 0:
        t.forward(size)
    else:
        for angle in [60, -120, 60, 0]:
           koch(t, order-1, size/3)
           t.left(angle)

In this example, the base case will be if order is equal to 0, then t (which is referred to turtle) proceeds forward based on the size provided. The recursive part is under the else statement. It checks for the angle and then tells turtle to turn  left according to the angle.

Although this example is quiet easy to visualize how the code works, there are of course much difficult examples such as those we work on in the lab. But I feel as long as you use the 2-step-process, as I like to call it, coding recursion becomes a piece of cake. I hope my post helps you understand recursion as its doing it for me. Thanks for reading and GOOD LUCK to everyone for the second half of the course.

No comments:

Post a Comment