Monday, March 16, 2015

ADTs? Abstract Data Types

One of the Abstract Data Types that was introduced is a tree. The below is an example of a tree.

Image courtesy of Wikipedia.org. To learn more, visit http://en.wikipedia.org/wiki/Binary_tree for more.

It's a very abstract concept, and will take some time to fully understand. From wikipedia, the above picture is an example of an unbalanced binary tree. 

A binary tree, for example, consists of nodes where each node may refer to two nodes or less. The top node is called a parent, and the nodes it connects to are called children. So, (2) is the parent, while (7) and (5) are children, and therefore called siblings. So in other words, this is a family data structure where the nodes are related to each other in some way. 

One of the main features about a binary structure is that it can be 'balanced' in a way. For example, the parent node can contain some sort of data, say an integer, and the left child will have an integer that is less than the parent's integer, and the right child will have an integer that is greater than the parent's integer.

The below is an example from wikipedia.

Image courtesy of Wikipedia.org. To learn more, visit http://en.wikipedia.org/wiki/Binary_search_tree for more.

The most common example, as used by many sources such as wikipedia, users from Stack Overflow, and computer scientists are to make searching for data faster. Take the first picture for example. We want to find 11. At first glance, we can't just pick a child from the parent node and search from there. We have go through each node until we find 11. What if we wanted to find 4? That would take a bit more time, and we would probably have looked at the other nodes first already.

Now, take a look at the 'balanced' binary tree picture (second picture). What if we needed to find four? Well, we can simply check the parent. Is 4 less than 8? Yes. Onto the left child. Is 4 less than 3? No. It's greater than 3, so we check the right child. Is it less than six? Yes, let's check the left child, and there it is! What if we wanted to find 13? We don't have to look at all the nodes. We can simply look at the parent node (8), and find that we'll be going to take a look at the right child. From there, we will eventually find 13. We won't have a need to look at 3, 1, 6, 4 or 7. We've eliminated the unnecessary nodes.

It's a simple concept in itself, but actually applying it to practical situations will take some time.  This concept will likely be revisited at a future date. 

the object-oriented programming concept

My initial impression of 'object-oriented' programming came from CSC108H (Introduction to Computer Programming) as did most of my classmates in CSC148H (Introduction to Computer Science). The initial impression was that it was confusing to learn. Why did we need to learn about this concept as opposed to designing functions and eventually creating a program from it? The answer was not as obvious at first, but it became more clearer after the completion of the first assignment (for CSC148H). It's a tool for programmers to write code in a more efficient way that is intuitive, and potentially, reduce the amount of code written in the future.

In object-oriented programming, there are classes and sub-classes. The classes are meant to contain, if suitable, generic code that could be applied in numerous situations. The game state is an example of this from Assignment #1. From there, we could implement generic methods such as legal moves, score, number of players. If we decided to implement tic-tac-toe and blackjack, we immediately notice that we will run into some issues. A generic method implemented into the class that establishes that we are designing two-player games, such as setting up 'p1' and 'p2' will be perfectly fine for both games. However, we have completely different legal moves, and score tracking for both games. If we didn't use classes, we could just use various functions and eventually implement it into the program. On the other hand, we can use a sub-class for each two-player game we design, one for tic-tac-toe and blackjack. This will allow us to override each method for the main class, if we need to, in order to create individual methods for each game. In the end, we'll be able to reduce the amount of code to put together a program that allows you to play tic-tac-toe or blackjack. For instance, in order to determine the score, we can call upon the method for the game that the user has selected. Using classes and sub-classes, you can see why programmers might elect to go that route. By writing code to run the program, we can structure code for any general game to run, rather than write more code to run each specific game.

In conclusion, it's a very good idea to practice with classes and subclasses. Perhaps, even visualizing what else you could do with the code. For example, I want to develop a text-based hockey game. I've got a class for each period of play. But wait, what about overtime and the shootout? So we'll create a generic class for the Hockey game state, and have a class for a period in hockey, overtime, and shootout. In the period state, the game is over when the time has run down to zero, and we have already gone through two periods of hockey. We can call that the game_over method, and we can put it in the generic Hockey game state to be overrode by the subclasses. The overtime subclass will have to override game_over, since the game ends when a goal is scored. The same goes for the shootout subclass where it's more complex with the skater-by-skater round process. Designing an actual hockey text-based game is more complex, but you can see how the framework are being put together using classes and sub-classes.


Sunday, March 1, 2015

tracing recursion

One of the new concepts in CSC148H (Introduction to Computer Science) come with a bit of a learning curve in terms of understanding. Recursion, as described in the CSC148H course notes (which can be found here (http://www.cdf.utoronto.ca/~csc148h/winter/148notes.pdf), is defined as an "...useful and beautiful problem-solving technique..." which is intriguing. As described in the course notes, we can break down a problem into several smaller problems, produce a solution to those problems, and combine those solutions into a solution for the original problem. For example, you wanted in the largest value in a list, but within a list you had sub-lists that consisted of various values. You would use recursion to go through each value at a specific index, and if that specific index contained a list, then recursion would permit you to go through each value in that list in order to get the value you wanted. The end result would be a list of values where the end-cycle of the recursion function would return the value that you wanted in the first place. For example, that could have been the max value of integers inside list.

The idea behind it is relatively simple, but not as easy to implement it. The tracing recursion exercises were integral in understanding how recursion works. It helps you understand how recursion works, and gives insight into proper recursion code. The initial expression is that it isn't that difficult to understand and implement, but time being spent on Assignment #2 will help provide a better understanding of that situation.

the first few weeks...

It's been a while since the last blog post for the sLOG, so it's about time to get into some topics.

In the first few weeks of the course, which is roughly up to the end of Week 3, there has been a lot of focus on classes and subclasses. Classes are not an unique concept to CSC148H (Introduction to Computer Science) as they were introduced in the CSC108H (Introduction to Computer Programming). Admittedly, my grasp on the subject was not comfortable and it was confusing at first. Eventually, working on the lab exercises, and Assignment 1's focus on classes and subclasses, I am more comfortable with the classes and subclasses, and the usefulness of them in designing programs such as a series of games.

These classes and subclasses help introduce new ways to think. Ways how to write generic code to help create a specific program, and later down the road, to improve upon the program. For instance, Assignment 1 had you create generic code for a game state, and specific code for a subtract square state. You might get some ideas on what you could do with the generic code. For example, in the Strategy class, we have a method to randomly pick a number from an user-defined range. You start to think about what else you could use that method with. For instance, could we use it to randomly shuffle a deck somehow? Could we use it for the children's card game, War? There are possibilities, both easy and difficult to implement.

Ultimately, the first three weeks were, in a way, a continuation of CSC108H picking off where it ended. However, the end of the third week started to hint about a topic called recursion. We'll talk more about that later.

Sunday, January 25, 2015

why geeks need to write


Why do geeks need to write? There are several possible answers to this question; some may disagree with the notion that geeks need to write. However, there are some intriguing reasons why geeks should write. One reason is to gain a better understanding of topics in Computer Science, and the other is to flesh out the design of a program.

In order to truly understand a specific topic in Computer Science, such as object-oriented programming or algorithms, you need to be able to relay your knowledge effectively to others, whether it be through spoken language or the written language. In this particular case, if you can't effectively explain a topic in your own written word, then how can you reasonably expect to convince others that you have a solid grasp of a concept? Being able to relay your knowledge of concepts to others effectively will translate to being able to break down the steps of your program and knowing what it will take to get a program to run. 

The translation of concepts into breaking down a program design leads to the next reason, which is fleshing out the design of a program. Being able to effectively break down the program into steps with basic English will go a long way in designing the program. It will also go a long way in explaining what your code does to others who may wish to read the program. There are many reasons to write as a Computer Scientists, no matter how simple it might be. Simple insertion of well-explained comments can go a long way in others, and even yourself, in understanding how the code works and what it does. Programming is a skill; part of your set of skills that include writing. It is not meant to replace a part of your skill set. It is meant to be used together. 

Wednesday, January 21, 2015

welcome to the blog

Hello,

welcome to sLOG musings where I may write about Computer Science on a regular basis. Maybe.

Stay tuned for more.