Sunday, 23 February 2014

Recursion

Recursion is a method of problem solving that is based around the idea of taking a complex problem, breaking it down into more easily solves instances of itself, and then finalizing it into a complete solution. Simply put, recursion is a function that calls itself. For example, here is a piece of code that can be used to calculate the factorial of any given number:

def factorial(n):
    fact = 1
    for i in range(1, n + 1):
        fact = fact * i
    return fact

The Factorial of n is calculated by multiplying all the numbers between 1 and n. The above code gives an iterative solution. In order to come up with the recursive solution for the problem, we must first understand that the nth Factorial number is n * Fact(n - 1). Alternatively said, if you know the Factorial for one number, you can calculate the Factorial for the next number. This would be the recursive solution for the factorial number of n:

def recursive_factorial(n):
    if n < 2:
        return 1
    return n * recursive_factorial(n - 1)

In this solution, I placed a threshold to stop the code from iterating through itself forever. This is what is known as the Base Case. In this case, the program returns the number 1 whenever the function is called with an argument value of 1 or less. The program also has something called the Recursive Step, where we call the same method with a slightly modified parameter.

During the first assignment, there were many problems that took me longer than expected to deal with. However, by using the my computer science's "Piazza" website, I was able to conquer many of the problems I came face to face with. The problems I could not solve using this website, I simply went onto Stack Overflow which gave me a greater insight onto how similar problems to the problem I was currently working on were solved. It was also very beneficial asking my friends in the class, as well as my lab partner to give me help on specific parts of the assignment. In all, without communication with my classmates through the Piazza website, and during class hours, I would have had a much more difficult with the assignment. I am very grateful for this, and hope that I can repay their efforts by continuing to be actively social with others taking the CSC148 course.

Saturday, 8 February 2014

Coming To Understand How I Program


Lately, I have come to a more psychological answer to difficulties I am having in programming. This realization is as follows: although it may be true that I can understand a problem I am working on by working alone, but I find it a lot easier to work with someone of my own skill level. By easier I mean less time consuming, less stressful, and very helpful when it comes to debugging and getting the program to work properly. Whenever I find myself staring at a complex or difficult program, I tend to stare at it for minutes on edge just trying to understand the very basic concept of what I am seeing. If I don't do this, I would never be able to understand what I am supposed to do in the program, and it would just take longer for me to get past any confusion. However, working with a partner makes this process faster. We would both give each other ideas on how we can work through bits and pieces of the program, ultimately putting my brain at ease, and helping me become more focussed at the task at hand. If any of us gets confused, we would solve the problem a lot faster than if I were just by myself. Many times during the lab, or even during Assignment 1, I would find myself thinking, "What would have happened if I were to do this problem alone?" The answer would probably be me taking more time on something which is, in reality, a very simple problem.
Generally speaking, I feel a lot more comfortable working on more basic problems with myself than anyone else. I am a quick learner when it comes to basic programming, so I feel that if I were to be paired with anyone else to work on something simple, they would only slow me down. I can be quick to program anything that I have already done before, but when it comes to new ideas, or new concepts on how to apply my knowledge, this is when my programming becomes a lot slower. Therefore for problems that involve applying my knowledge or approaching programming in a different way that  I was taught before, are better done with a partner to help me along the way.