Sunday, February 9, 2014

Week 5

The classes this week once again focused on recursion, but also on the new topic of namespaces and scope. We learned that there is a hierarchy for namespaces that python searches through to use:

  • innermost scope (function body, usually) local
  • enclosing scopes nonlocal
  • global (current module or __main__)
When python is searching for how to perform an action, it searches through this list from top to bottom; if it finds a local solution, it will not use a built-in for example. This can be useful for if you only want to reference something inside a function or clas, while also having it perform a different action outside (or at least, that's what I think it's used for). Going through some other SLOGS, I found many other students have had trouble of tracing the code example of namespace presented to us, and I agree that it went a bit over my head initially. However, now that I have had some more time to analyze it, it seems a bit clearer to me at least.

The lab this week was, well, average as usual. We programmed simple examples of recursion, and I can see now how applicable it can be in large situations with similar problems. Speaking of which..the A1 deadline is approaching, and for (my partner and I) at least I think it is going well. The recursive four stools function looks a bit daunting, but given the move_cheeses example that Prof Heap gave us it seems a bit more manageable (I hope!).


Sunday, February 2, 2014

Week 4

Class

The focus of this week was on recursion. I had a general sense of what recursion was already, but previous to this course I wouldn't have had an idea of how to program it. In a programming sense, recursion is the use of a function within itself to solve a larger problem. That probably isnt the most transparent definition, so I will use an example.
(borrowed from http://stackoverflow.com/questions/479343/how-can-i-build-a-recursive-function-in-python)

def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n - 1)

In this example, to find the factorial of a number involves calling factorial on itself. If we had in input of factorial(3), it would return 3*(factorial(2)). Of course, factorial of two would be 2*(factorial(1), and factorial(1) =1. All this happens within the same function to produce the result of 6. It is a useful concept for solving a problem with many similar and linked parts.

Assignment 1

My partner and I began assignment 1 this week, and I have to say I am enjoying it. As always, when presented with large amounts of code at the beginning it may seem like a towering challenge, but once you dissect it into smaller problems it quickly became easier. At this point we have done steps 1-3, and it was definitely a nice reward that after this  point we were able to play a working version of the puzzle. The contrast in this assignment compared to those in 108 is present. While there it seemed we were spoon-fed what to do, and not given much slack in what we could and could not do, I found in 148 there is much more freedom in how we approach a problem.


That's about it for this week!


Monday, January 20, 2014

Week 3 - Object Oriented Programming

When I started this course object-oriented programming (OOP) was a foreign concept to me; not in the sense that I had no idea how to use it, but in the sense that I really didn't even know what it was. Of course, little did I know that I had been using and manipulating it in Python this entire time! This first SLOG entry I feel is an important one now, because it made me really learn about the subject at hand, OOP. Sure, I may have off-handedly referred to parts of my code as objects, but I had no real sense of what it was. So, for others out there like me, here is my explanation in brief.

So, an intro to OOP. Python is a language based entirely on OOP. The 'objects' in question are things such as the instances of a class, a function, or a value. Objects have two parts, their state and behavior. Say for instance (heh) that we have a Class Door. A door can open and close. The state of the door would be either that it is open or closed at a point in time. The behavior would be that you can either open door or close door. OOP is to build programs around objects of these types.

I can now more clearly see the extent of use of OOP, and I hope that this information will help me to improve in my computer science/programming abilities. Though I may have gone along just fine without actually know what OOP was, now that this SLOG has enticed me to research further, I think I will be more ready for what this course and future computer science courses bring.