Wednesday, 18 February 2015
OOP concepts
The topic of this post is a summary of Object-Orientated Programming concepts. To me, the main ones we learned are Abstract Data Types, Inheritance and Recursion.
Abstract Data Types, or ADT, are data types that are defined by their attributes. It's not the implementation that's important, but the interface which allows others to know what the data type can do and what they can do with it in their own code. One example is Stacks, which are implemented in many different programming languages but has the same function in all of them. All of them hold an number of elements and can push -add a new element- or pop - return the last element added. Another example of an ADT is a Tree, which is represented as a tree structure of linked nodes.
Inheritance has to do with extending existing classes to make new classes and prevent having to write the same code multiple times. All classes already inherit from the Object data type, giving them certain functions. A second class can be written as a subclass of a first class, which means that it inherits all of its methods and attributes. Methods and attributes can also be overwritten in the subclass, which is when they are changed from the original class. One reason to use inheritance could be if you need to define different data types that still have much in common, like if you were writing data representations of shows that could all record their views, but wanted to write another class for specifically sitcoms that would could still record views but also stored the amount of times a laugh track was used. Sometimes in inheritance, the original class is known as an abstract class, which means that it is only used through it's subclasses.
Recursion is when the implementation of a function involves calling itself. It can be used to call a function many times until a certain result is returned. The last function call, where it stops calling itself, is referred to as the base case. Recursion can be used for many different reasons - it can be used to create geometric shapes, or add the elements in a set, etc. Recursion allows us to write less code that is more simple than if we hadn't used it.
Monday, 9 February 2015
Recursion
wouldn't really make much sense. Anyways, on to recursion.
I've often recursion described as when a function is defined by itself. A basic example could be:
def count_total(lst):
return lst
else:
return sum([count_total(i) for i in lst])
This function adds all the elements in a list, including elements in nested lists. A comment on my last post asked how I would explain recursion, considering that its solving a problem using the problem. Pretty complicated stuff. The way I see it, it's like you're expanding a problem, or going deeper. In the example above, you call the function count_total on each element in a list, then call it on each list element in the list, and so forth until you get to the non-list elements. From what I've seen of recursion so far, there is always eventually a situation where you stop calling the original function - here, it's when an element is not a list. I haven't seen much recursion, but I'd imagine that you need that to happen unless you want the function to keep calling itself forever, like an infinite loop.
Recursion tracing really shows the expansion aspect - it reminds me of expanding polynomials in math. If we try the above example on the list [1, 3, [6, [1]], 4], we get:
--> sum([count_total(i) for i in lst]
--> sum([count_total(1), count_total(3), count_total([6, [1]]), count_total(4),])
--> sum([1, 3, count_total(6), count_total([1]), 4])
--> sum([1, 3, 6, count_total(1), 4])
--> sum([1, 3, 6, 1, 4])
--> 15
Recursion tracing seems to me like the kind of thing which seems easy at first, but can quickly get complicated and confusing. It is helpful when trying to understand why recursion code I'm writing doesn't work, but it's also a bit tedious at times.
Thanks for reading! Or if you just skimmed/skipped most of this and went to the end, well that's okay too. Everyone's busy, it's understandable.
Sunday, 1 February 2015
The First Few Weeks
The topic for this week is my impression so far of the first few weeks of this course. Honestly, I haven't found the class too challenging so far, though that is probably because we're still in the early stages of the course. The main points of interests are the first assignment, the labs, inheritance and recursion and the slogs.
The first assignment was confusing at first, but was easy enough once I understood what they were asking for. Everything needed for the assignment was taught in CSC108 besides inheritance and abstract classes. I think I did a decent job on it, though I didn't follow the style guides on some areas such as the requirements for whitespace. Having inheritance as part of the requirements meant that I thought more about the structure and the way it worked more than before. I had a bit of trouble writing docstrings. I wasn't sure what to write for examples that depended on randomly generated values.
I'm not too fond of labs. My TA is nice, but as the labs have been fairly easy so far I haven't had a reason to ask him for help. The exercises seem easy enough to do at home and coming to the lab for two hours seemed unecessary. Hopefully it becomes more useful as the material becomes more difficult. However, that wouldn't change the fact that I missed the free Lights concert on campus because of my first lab. And I love Lights.
I already knew about the concepts of recursion and inheritance, so they weren't really new to me. My knowledge of recursion is very basic though so I expect to get frustrated by them in the coming weeks.
Remembering to write slog posts has been one of the most challenging parts so far of this course. I also spent an unreasonably long time thinking of my slog url. Defying my expectations, apparently someone other than my TA visited my blog based on a very strange comment left on my last post. If you're reading this, Razarac00, well I don't actually know how to reply. Thanks for the comment, it was definitely unexpected. I do kind of like the idea of slog-hopping though.
Thanks for reading, TAs and possibly fellow students!