Tuesday, 28 January 2014

Brian's CSC148 SLOG # 2

Hello again! Thanks for reading my blog, really!

How is everyone's week so far? My keyboard broke over the weekend so I ordered one off of e-bay and for the first time took apart a laptop to get it fixed up. Turned out A-OK :]. My profs and friends had a good laugh at it though, especially since I was copy pasting the letter E to take notes.

So what's new in weeks 3-4? Well let's dive right in.

Overview
  • Diving deeper into recursion
  • Inheritance
  • Clarification on ADT's 
  • Testing code
  • MORE FRIGGIN AWESOME BUILT IN FUNCTIONS!
Recursion 

Cool stuff. Three visual examples of recursion were shown to us this week: Koch, Turtle and Sierpinski's triangle fractals. All of the functions work the same way, that is, they call themselves repeatedly until the code tells it to stop. If the code doesn't specify a stopping point, they will keep calling themselves recursively until your system runs out of memory! 

One really interesting thing that I noticed in the turtle recursive example was that each time it calls on itself, the function spawns 3 times the turtle of the previous function call. It starts with 3 turtles, one spawning at 0 degrees, another spawning at 120 degrees and the third spawning at 240 degrees. Then another 3 turtle spawns at each of the previous end points following the same degree order! So we have 3 turtles on the first call, 9 turtles on the second, 27 on the third... ETC. 
So we can conclude mathematically that it behaves as 3 to the n'th power where n is the number of specified (or unspecified!) calls.
Why is computer science so cool?

Inheritance

This is a really neat property in Python that allows us to combine other classes and how they work into a totally new class. Even though we can borrow from other classes, it has been mentioned that multiple inheritance should be totally avoided since things can get pretty confusing if multiple classes have the same functions. However, I wonder if it would be problematic at all if we combined classes that do not have multiples of the same functions. Hmmm..

ADT's

My previous post about ADT's was made totally clear to me by one of the professors in 148. So here it is: We have been using ADT's such as the built-in functions. These built-ins (isinstance, str, abs, ETC.) were programmed for our use without us having to worry how they were implemented. SUPER SIMPLE!

Testing Code

My weakness lies here. Out of habit, I test my own code without using doc tests or unit tests. During this week's lab, I was totally dependent on my partner for this part of the exercise. I actually know how to use doc testing but not unit tests. They are a bit harder to use as you have to write your own classes to perform them. They are however, much more reliable on finding bugs in the program than doc tests.

New Built-In Functions

raise allows us to call on our own defined Error Types! For example: If I were to follow coding design guidelines, I could raise a user-defined error if some programmer's code had lines longer than 80 characters *Cough, me, cough*.
exception allows our program to catch errors and tell it what to do if it encounters an error. Perhaps a client might use our program incorrectly, inputting a list of some sort instead of an int. If the program uses exception, we could easily add a little bit of code that will notify the client that the program will only take inputs of integers!
isnumeric checks if a string consists of an integer and returns True or False. I was lucky to learn of this built-in through a classmate in my lecture which helped me tons in the second exercise.

Check back next week for more awesome CSC stuff!
Comment and ask away if you please.

Ciao








Tuesday, 21 January 2014

Brian's CSC148 SLOG # 1

Introduction

Pleased to meet you!

I am a student studying at University of Toronto, doing my best to learn all I can in computer science. Currently, I have one huge goal that I have my heart set on: Creating a mind-blowing video game that will surpass all others that are currently available AND the ones to come in the future. If someone wishes to know the specifics of this video game, feel free to ask. :)

This courSe LOG is all about my perspective, experiences and knowledge of the material that is taught in CSC148. Hopefully this SLOG will be helpful to those who read it!

Introductions aside, let's move on to the interesting stuff taught during the first two weeks of 148.

Overview

Having taken CSC108 last semester, I was surprised from how much new content that was already taught in the first 2 weeks of class. 

Stuff we learned:
  • Abstract Datatypes (ADT's)
  • Stacks and Stack ADT's
  • Object Oriented Programming (OOP)
  • Ternary
  • Comprehensions
  • Recursion
  • New Built-in Functions
Abstract Datatypes (ADT's)

This was super confusing for me at first but I think I've got it down. It's called abstract since it allows the programmer (the provider) to create methods or programs without having to worry about how others (the clients) will use it. It's something that will save time for the programmer if the client intends on using it without worrying about the code, while also maintaining the code's consistency without having to go into the specifics of each and every method. I can see how this would be useful in certain work related projects where there are time constraints and also in certain methods where it isn't necessary to explain how it works.

Stacks and Stack ADT's

Stack was somewhat touched on in 108. In 108, tracing our Python programs was the method used for learning about call stacks. However, in 148, we learned that stacks can also be in the form of lists and sets. These stacks in particular follow a structure known as LIFO, (Last In, First Out). LIFO can be recalled in real life situations! Picture this: Imagine taking a newspaper from a newspaper stand. They are usually stacked face down by placing one on top of another. Then when you take a newspaper, you take the one that was last placed (the one on top!) and with or without knowing it, you just had a LIFO moment! Easy Peasy!

However, we can also change the algorithm of LIFO and for example, take the item at the bottom of the stack out rather than the top. There are many ways to program this to get different desired results and the programmer can choose to make the algorithm abstract (without worrying about how clients will use the code). If this is the case, we have a Stack Abstract Data Type.

Object Oriented Programming

This is simply a paradigm where focus is emphasized on creating objects by means of classes as opposed to another paradigm called Procedural Programming which is the creation of methods and how they manipulate data. The latter (Procedural Programming) was mostly what was taught in 108, which I think is the appropriate order to learn programming in for people who are new to computer science.

Ternary

In this case, ternary isn't about the ternary numbering system, but rather an expression that has three arguments. Here is a simple example:

if The time is 9:00 A.M. <-- 1st argument
    I need coffee <-- 2nd argument
else
    I don't need coffee <-- 3rd argument

Comprehensions

From what I know, comprehensions are simply reducing a code from being multiple lines long, into a shorter version of code that is no less concise than the prior. Taking my example from above, let's see how the comprehension form of it looks!

(I need coffee) if (The time is 9:00 A.M.) else (I don't need coffee.)

BAM! Simple as that and something I hope to get used to since my code from l08 was horrendously long.

Recursion

Alright, so this is very new to me and probably everyone else but I will try my best to explain this! In order to deal with a complicated problem, rather than writing incredibly long code, perhaps this problem can be tackled down in smaller sizes and every time you tackle a small problem, the solution becomes easier to obtain. Recursion gets its name from implying repetition in solving a larger problem at a smaller scale.
Picture this: You're the captain of a pirate ship (YARR!!) and you encounter a Kraken (basically a gigantic octopus). It's looking for a fight! If you try to smack it in the face with your cannon, it will block it and instantly crush your treasured ship. That's a BIG issue so let's deal with the smaller problem first: The arms. Smack the arms with your cannons first, then when they're all dealt with, go for the prize!
And that's my story version of recursion.

New Built-in Functions

Many, many new things built-in functions were taught in the first two weeks. These functions were perhaps the most interesting things I've learned so far. They are of interest to me since I now have a larger arsenal of weaponry to handle the tests, assignments and exam (heheheh).
So what's new?

set : Sets are another type that are different from lists as they do not permit the same two items in one set. so if you typed in python: x = set((1, 1)), x would equal {1}. You can also do subtraction with other sets which removes objects and convert them to lists as well using the built-in function list().
isinstance : This checks if an object is a certain type. So for example: isinstace(1, int) would return True. Lots of potential here when going through loops which we'll probably encounter in many programming problems.
intersection : intersection takes two sets and compares them, returning objects that are in both sets. Although we haven't done much with sets yet, I can't wait to see the usefulness of this in particular.
re.split : Remember the split method? This is its brother, and he's stronger! Keep in mind to use method re, you have to import it first, so include import re in the code before you call method re, or there'll be an error. It basically allows you to add an operator '^' without the quotations which will negate a set such as [0-9] or [A-z]. From experimenting, I learned that the lowercase and uppercase matters.
Here's an example I used while playing around with it.

>>> import re
>>> a = 'I am Robot #52'
>>> re.split("([^A-z])", a)
['I', ' ', 'am', ' ', 'Robot', ' ', '', '#', '', '5', '', '2', '']
As you can see, there are some empty strings but the '#52' string has been totally split apart and the 'I am Robot' is split nicely. There are many desirable things one could obtain with this!

Anyways I would like to end my first SLOG here. So a few final words.
Please discuss with me if you like, I would love to.
I have an AMAZING lab partner, here's his SLOG:
http://mycsc148experience.blogspot.ca/

Also I really enjoyed reading this student's SLOG, he/she was very clear in explaining the new material and even I learned from this person's real life examples which I thought were hilarious! Check it out if you'd like:
http://ddc-csc148slog.blogspot.ca/

Hope you enjoyed,
Ciao for now - Brian.