Friday, 4 March 2016

Week 7: Recursion..Is it really hard?

Recursion was the nightmare that many csc148 students dreaded since the beginning of the year. The amount of fear the word "Recursion" generated was actually quite hilarious. The negativity and the rumours about its difficulty can be justified in certain terms, but overall, the concept of recursion is actually quite straightforward when analysed under scrutiny. The key to understanding recursion is to approach the idea not as you would approach other ideas in programming, but thinking logically. Instead of trying to fit the notion into a familiar depiction of coding, we have to understand recursion by looking at a completely new slate.

You get the idea
I can write an entire essay on how recursive functions work, and how they can be utilized in everyday programming. But the point of this log is to share my thoughts and experiences about this topic, rather than trying to explain the topic itself. So to keep it simple and straightforward, a recursive function is basically any function that calls upon itself or exists in a potential cycle of function calls. Immediately, this can throw some people off. How can a function call itself if it's still running? Well that's the speciality of recursion. A recursive function infinitely loops upon itself until it comes across the desired result for whatever problem the function is trying to solve. For example: When trying to find the factorial of an integer (Let's assume 6), the recursive function keeps calling upon itself, starting from the integer 6, until it reaches 1, and upon each call, keeps multiplying the previous value obtained from the function. Factorials are just scratching the surface of the potential that recursion has in programming. From Fibonacci numbers to much more complicated mathematical problems, recursion can make programming quite efficient at times.

A helpful visualization of a recursive function finding the factorial of 5
I was first introduced to Recursion in high school, and to be honest, it took me an entire week to figure out how this miracle functioned. But once I started to notice the reasoning and efficiency behind recursion, I gained a whole new perspective in the world of coding. The utility of using a recursive function is actually quite immense, but finding out how to solve a problem using recursion is usually the hardest part. When trying to solve a problem in programming, our mind immediately starts thinking in terms of loops, classes and simple helper functions. But in order to utilize recursion, it is vital to approach the problem in a different way. It is necessary to find a pattern where the problem can be solved through an finite loop of the same series of events until it reaches a "base case". A base case is for a recursive function, what a "break" is, for an infinite loop. It ensures that instead of running infinitely, the function comes to a stop at a certain condition. When we look back at the factorial example. the "base case" for the factorial of 6 is when the value called into the function is 1. The moment it reaches 1, instead of calling itself again, the function finally returns the expected factorial value of 6. Thus, once we establish a base case for a problem that requires the occurrence of the same set of events multiple times with changing values called in each time, then a recursive function is the most effective choice for coding. Of course, this perspective of recursion is just the preliminary explanation, and we are learning much more complex ways to use recursive functions in our lectures. Recursive functions are always very tricky to come up with, and I always struggle with finding how to solve a problem using recursion. I have started practising recursion frequently in order to get more used to the idea of this different method of problem solving, and I can thankfully keep up with the lectures during class. I can't wait to learn more about recursion and get to utilize recursive functions in the next assignment or just programming in general. So to answer the question, recursion is not hard. It is just a different concept that might take a while for everyone to get used to, but understanding it is not a huge ordeal, just a little time-consuming at most. So if you're still struggling with recursion, just remember that you're not supposed to understand it instantly. Give it some time, and ponder over recursive functions before going to sleep, and sooner or later you will reach your Eureka moment.

 I will be back with another log for next week, but until then, Adios friends!

Thursday, 3 March 2016

Week 6: Assignment 1 Final Lap

This week was the week when the first Assignment was due. My group has been consistently working on the assignment since the beginning of the term test, and we have made huge progress, but small errors always keep popping up, making the coding process much more frustrating than usual. The presence of so many different files, and the necessity to check the unittests for each file to make sure there are no failures is a great ordeal every time we are trying to make some changes to the backbone of any class. The connectivity between every class also makes it quite a hassle to find errors sometimes, as it can be interconnected deeply throughout multiple classes. But overall, we worked as a team, and finished up the assignment before the due date. Two days before the due date, me and my group actually stayed up till 3 am at our residence common room, just grinding away at all the bugs and errors, as well as finishing up docstrings here and there throughout the assignment. The final product worked as it should, and we happily submitted it the day before the due date with a satisfactory confidence.

As for the lab for this week, I learned some very useful codes that I never used before, namely, the zip tool for tuples or lists, the multiplication of vectors with vectors, and vectors and matrix were the highlight of our lab and the week's quiz. I found these tools extremely helpful, and paired with the list comprehension strategies we learned in the past weeks, it makes a serious change in the efficiency of coding. Overall, it was quite an interesting week, with some hectic last-minute fixes on our assignment and introduction to quite a few new techniques for coding.

Week 5: Term Test 1 and a final look at Linked Lists

This week was not as eventful as others in terms of learning new materials, as the first term test was scheduled for February 10th. Thus, the Monday lecture was a brief review of everything we needed to know for the test, as well as occasional references to our first assignment. Speaking of the assignment, I finally put together a group for this assignment, consisting of 2 of my friends living on the same floor of my residence, and we had been brainstorming different methods to solve this assignment quickly and efficiently. The night before the test, we actually worked on our assignment, because everything on the assignment is pretty much a review for the test, so if we were able to do the assignment, we would be quite prepared for the test. After a lot of trial and error runs, we finished up almost 50% of our assignment that night, and checked each function and classes for syntax or logic errors. We even completed the docstrings for all the functions that were done, for practice before the test. On the day of the test, I did not have the lecture, as mine usually goes from 10-11, and that's when my test was. I woke up around 7:30 and got ready for the test. During the test, I actually started panicking because of the incredible amount of coding we had to get done within that 50 minute time frame. Luckily, I made a really crucial decision to skip the 2nd question with the multiple sub-classes and finish the last stack function first, before going back and finishing up question 2. This last-minute resolution helped me complete the test and I was very satisfied with my answers.

During Friday's lecture, we learned a bit more about linked lists, and as I mentioned in the last post, the professor explained to us why it is sometimes more efficient to use a linked list, and sometimes why it is not the most effective tool to be used. Of course, it was related to the index positioning of the list tool, and the nodal connection of the linked list, and how each has its pros and cons. The lecture was very useful for anyone who was having a hard time understanding the reasoning behind using such a complicated tool as linked list. And that officially ended my learning and experiences from CSC148 for Week 5. I'll be back next week with more updates and new material. Until then, Adios friends!