Sunday, 27 October 2013

List Nodes

This week we learned about list nodes. These are lists of nodes within each other that can represent trees. In our examples we used each node and leaf merely carried a number but I can see it’s uses if it were to carry other types of information. We also learned to use the __repr__ method, in which simply inputting an object would return it’s ‘cargo’, as opposed to inputting ‘print(object)’ or ‘object.__str__()’. It seems to be quite useful and convenient.

Most other bloggers seem to understand this concept well.

Monday, 21 October 2013

Oct 14-18

This week we did not learn anything new because we had the Thanksgiving holiday and a test on Wednesday.

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.