Saturday, February 28, 2015

Growing a Tree on a Computer

A tree? Oh, like one of those tree diagrams! That sounds easy to make. Oh, wait. It's not.

At first it seemed like planting and taking care of a real tree would've been easier than learning about trees in Python and recursive methods to make them work. I definitely felt unprepared when I went to my tutorial to get stuck on a question about trees for the whole class. Although I had a lot of trouble grasping the concept of class objects inside other objects of the same class, I decided then and there that trees would not be the end of my programming career. I refused to get defeated by what seems like a simple concept.

You can think of a tree like an elongated list of lists. There is one list that keeps track of other lists (which may or may not keep track of even more lists). This main list and all the other sublists have values that are associated with them. So in a way, trees have more functionality than a list of list of list of etc. Sure, that is not too hard to understand. This is not where I struggled.

The hardest part of trees is the methods that you can implement with them. For example, you can create a method that finds the highest value in the tree. In short, there is a method that searches every node of the tree and returns the highest value among all the nodes. Methods like these are what make trees really tricky. You cannot simply return a max function and get the highest value. Instead, you have to use recursion to search through every node starting from the root all the way to the last leaf. Talk about a whole lot of work.

Sure, implementing this method is not a one-way thing. It is not as simple as plugging in a function and getting the result. But then again, this is exactly what I asked for in my previous posts: I want this course to challenge me with concepts that are not straight-forward. As I take time to better myself in these recursive methods, I will become a better programmer (and in return a better man).

Saturday, February 14, 2015

Getting Oriented with Objects: Object Oriented Programming

Back before I started programming, I used to think that programmers would write very long lines of code to create their programs. Old me used to believe that the people who make video games have thousands and thousands of lines that make the game. Well, a part of this is true. There ARE many lines of code that make up games. However, as my old self would have believed, they are not all on the same file.

Before I knew what object oriented programming was, I thought that all methods, functions and systems all fit into one file and class. Oh boy, was I so wrong. I am very glad to be wrong, too. If programming like this were the case, I would've given up ages ago. Thanks to the genius that is creating classes and subclasses that work with each other, I am able to create elaborate projects piece by piece. I do not have to focus entirely on the whole picture and instead concentrate on what a certain part of the program does and how to implement it.

How easy is it that I can create a separate file for a subclass of a computer strategy system? I do not have to mesh a bunch of different strategies in the same file but instead I can organize each different strategy in a subclass that is separate from the rest. It makes thinking about computer strategies a whole lot easier. Through object oriented programming, I can mark common features of each specific strategy and implement that in the parent class. Then I can put specific traits of  each unique strategy in a subclass. These two classes would then work together to implement said strategy in a game. This kind of orientation of codes ensures that I have an organized setup that I can easily analyze and fix later on.

It may seem like such a simple concept but without it our lives would be a whole lot more difficult. Via object oriented programming, I am one step closer to becoming a master programmer. Well, at least I will have the organization skills of one.

Thursday, February 5, 2015

The Recurseption

Slow down; can you say that again? A function within the same function within the same function? If you were to tell me at the beginning of the semester that you could have a function that calls itself then I wouldn't have believed you. In fact, I sometimes still can't grasp the concept that there are functions that call itself repeatedly. How is it that you can write a function whose steps are dependent on itself? It was hard for me to wrap my head around this concept for quite a while.

It is handy, however, to have friends who are in the computer science field. One friend of mine called Matt brought me into the light that is recursions (he also gave me other helpful tips, such as my personal favorite "try googling it"). He told me to think of how factorials such as "5!" work. You are presented, literally, with a number and an exclamation mark. (At this point, I saw little of how this rationalization of factorials would help me understand recursion any better.) Then he told me to look at the number separate from the exclamation mark. If the number is not 1, then you multiply the number with the factorial of that number minus one. That would look something like this: (5 * 4!).

Woah, hold on a second. There it is! A factorial is inside the same factorial function! Lo and behold, I witnessed the genius of recursions right before my eyes. I finally figured out that recursions are not indeed endless loops that break the paradigms of the universe but actually a two-step process. If a value does not meet its requirements then you implement a change to the value and check for its requirements again. If it does meet the requirements then you return whatever value you need to. It's so simple yet why was it so difficult for me to figure out?

Through the help of my friends (and my ability to grasp my friends' knowledge) I was able to understand the seemingly impossible. No longer did recursions seem to me like a paradox, similar to the idea that someone can travel back in  time to meet their former selves. Now that I realize that there are no loopholes in the theory of recursion, I feel like I can do the impossible. Maybe I should consider figuring out time travel now.