Sunday, 13 October 2013

Object-oriented programming has been a great topic. I really enjoy its uses. With object-oriented programming, we are able to write classes that carry out specific instructions and create instance variables that exist only within that class. This makes code easier to organize and keep track of, and have variables that may either be exclusive to the rest of the code or variables that may be called anywhere in the code using ‘self.’ variables. I was, however, having a little trouble with multiple inheritance. At first I was doing it like:

super().__init__(parameter 1, parameter 2)

which was giving me a lot of trouble. But I learned from a lab partner to do it like:

parentclass1.__init__(self, parameter 1, parameter 2)

and that solved all my problems.

A recursive function is a function that will call itself within the function. Recursion and loops are very similar, and I feel that there are only a few cases in which recursion may be more useful than a loop. A recursive function will keep calling itself until the base condition is met. A loop will do the same thing but you may have to specify many conditions for which the loop will continue or stop at. Thus, recursive functions will look neater. Nevertheless, I feel that there are only a few niche cases in which recursion may be viable to use. Using loops seems more straightforward and self-explanatory.

Recursion, however, is a topic I am largely less confident in compared to object-oriented programming. I started off not being able to grasp even the simplest ideas of recursion. It just didn’t make sense to me. So I googled ‘recursion practice problems’ and found this: http://webs.wofford.edu/sloanjd/spring09/COSC350/RecursionPractice.pdf. I remembered that in one of my labs the TA had written down a general structure of how the recursion code should look like: an if statement with the base case and an else statement which will call the recursive function. So I used that model to try and solve the problems. I made a recursive function that emulates multiplication by repeated additions, division by repeated subtractions, a recursive function to sum all the numbers in a list, and another to multiply all numbers in a list by 2. However, it was only after I wrote a factorial function that the idea of recursion actually made sense to me. The function will call itself recursively until it hits the base case and returns whatever value is in the base case (in the factorial function this would be 1). Then that return value can be used to find the next return value, which will be used to find the next, and the next, and so on until you reach the ‘top of the stack’ and your desired value is returned. Nevertheless, these are novice-tier problems and the more difficult problems (e.g. ones that involve multiple lists) are still a mystery to me. Further, interpreting recursive functions in list comprehension format is also very challenging. I just need to keep practicing and maybe I’ll get a hang of this.

After reading through my classmate's slogs, it seems like most people have a good understanding of these topics since most of them already have a decent programming background.

2 comments:

  1. Here's a good place to practice recursion: http://codingbat.com/
    It is helpful to write down the values you have at each step, when first starting off.
    Recursive Fibonacci would be a good function to step through.

    ReplyDelete
  2. Thanks Patricia, I'll look into it!

    ReplyDelete