Sunday, 22 March 2015

SLOG: Week 10 REVISIT

This week we need to revisit one of our earlier SLOG posts.

http://myanacondadontpythonista.blogspot.ca/2015/02/slog-week-5.html

In this post, I talk about the importance of recursion how powerful it is. Even now as I look back at my post, I know exactly what I mean when I say "programmers use it on a daily basis" because of how much I've used it to make the functions in my assignments properly accomplish their tasks. Without recursion this would have never been possible. A prime example is the Minimax function in assignment two where the function checks every possible game state and then makes a move based off of which game state leads to a win. Though it may have been possible to make this function without recursion, it would have been very tedious and inefficient. Recursion simplifies the task as well as the amount of code we need to write and most importantly, it gets the job done.

In Ivan's blog: http://ivanslog148.blogspot.ca/ I found that he had the same view as me in the importance of recursion. It's really nice to know that I was not the only one having trouble with recursion back then and it's very inspiring how he had no experience with recursion but still managed to solve a question that had taken me an hour or two to complete.

I still agree on the fact that coming up with an algorithm for recursively calling functions is a bit tricky however, after weeks of practice and experience with recursion, I am nearly 10 times more confident in my ability than back then and I now feel as though I can find a recursive algorithm for anything given enough time.

All that is required for recursion is to simply find base cases that end the recursive process and what to do when the base cases are not satisfied aka the general statements.

At the end of the post, I also talked about how I should have prepared more for test one. This is something I definitely agree on for tests in general. Like I said, both studying and sleeping is very important which is something I will definitely do for the exam considering I only took half my advice for test two.

Sunday, 15 March 2015

SLOG: Week 9

This week was pretty short since we had a term test. I found the test to be pretty easy however, I feel as if I did not prepare enough. I did all the labs from scratch and even made a cheat sheet-something in which I didn't do on the previous test- hoping to get perfect however I made one fatal mistake. I'm sure you're wondering what this mistake was and I'll tell you, it was pretty dumb. I did not read the instructions given before the midterm that highlighted what concepts we needed to know. In particular, due to my mistake, I did not study any of the definitions nor the types of ordering in which nodes of a tree could be visited.

After Reading Albert's blog: http://www.99bugsbutaglitchaintone.blogspot.ca/

I found that I could clearly relate to his view of the test since I pretty much had the same troubles as him. All in all, like him, on the final exam I will not repeat my mistake and I will make sure to cover all the material even if it's not mentioned in the outline.

Also, since we had so much spare time this week with the strike happening and only one class due to the test, I was able to read Celina's blog: http://celinajiangslog148.blogspot.ca/

After reading her summary on abstract data types, I felt as if my understanding of that topic increased exponentially. Previously, I had only a general understanding of abstract data types but I never had the chance to actually apply my understanding other than in labs. Her diagrams along with the explanations that accompany them are extremely good and I even learned how they can be applied in real life applications. Lastly, I realised that Tree's were abstract data types which I probably should have known before but it's better late than never!

Sunday, 8 March 2015

SLOG: Week 8

This week, we finished off learning about binary trees which are basically trees but with a limit of two children per node. This may sound as if it were limiting the power of trees but in actuality, it provides us with some useful tools such as being able to check whether a value is contained in the tree without having to visit every node/leaf.

The idea for binary trees is that the left child of the parent node has a value less than the parent node itself whereas the right child of the parent node has a value greater than the parent node itself. Thus in the scenario:

Parent node value: 15
Left child value: 10
Right child value: 20

if we were looking to see if 17 was in the tree, we already know it isn't contained in the left child since 17 > 15 which implies it is in the right child tree if it exists. This effectively eliminates a lot of extra work for the computer and reduces the runtime of the program. In the scenario where the value we are looking for is in fact 15:

1) Does this node have a value?: YES
2) Is 15 < value of the current node?: NO
3) Is 15 > value of the current node?: NO
4) Thus, it implies that the value of the node it is on is in fact 15.

However, accomplishing this task requires quite a bit of mastery in recursion since we must recursively call the tree in order to travel through the nodes.

We also started talking about linked lists. One of the key concepts I found to be important would be how much the order of the code matters. For example when we append an element to a linked list, we need to change:

- last node
- former last node
- back
- size
- possibly front

If we change any of these in the wrong order then a syntax or maybe even a logic error would occur.
For example, when we switch the values of two variables we can't simply assign one to the other.

a =  5
b = 10

If I wanted to interchange the values of "a" and "b" I can't simply say a = b and b = a since the following would occur:

>>> a = b
>>> b = a
>>> print(a)
>>> 10
>>> print(b)
>>> 10

The value of "a" was changed so saying b = a does not give "b" the value 5 anymore. This situation is quite relatable to linked lists.

Thus, when working with linked lists, we must always keep in mind what we need to change first and how one line of code will affect the other

Sunday, 1 March 2015

SLOG: Week 7 Recursion Summary

After doing so many labs and exercises on recursion, I think I almost at the point of mastering this technique. As a reminder, recursion is basically when a function calls upon itself which can effectively break down a seemingly big complicated problem into many simpler and smaller cases such that when we combine these cases together, we can form a solution to the problem. This is much like how we solve optimization problems in calculus where we pick out key details, form equations, take the derivative, check where the derivative is equal to zero, and then substitute our results back into the original equation. On another note, I sort of like to think of recursion as a machine where everything operates together to accomplish a task; in the case of recursion, that task would be finding a solution.

One of the fundamental aspects for recursion to be successful is the base case. The base case is what signifies the end of the recursive call and without it, the function would keep calling upon itself infinitely to no end which would certainly get us nowhere closer to the solution we desire. In addtion, quite often there are more than one base cases as shown in assignment two where the minimax function has the following base cases:

if game is over and current player has won:
    return a score of 1
elif game is over and current player has lost:
    return a score of -1
elif game is over:
    return a score of 0


The key to finding the base cases of a problem is to break the problem into the simplest form possible. These are the cases where no absolutely no recursion is required which is, as you can see, why the recursive call would end.

The next part of recursion is what to do if the conditions of the base cases are not fulfilled. This is usually where the recursive call on your function will occur. To illustrate this concept more clearly, we can use the Fibonacci Sequence. The Fibonacci Sequence is a sequence in which the first two terms are 0 and 1 and every term after that is determined by adding the previous two terms together.

For example the first 8 terms of the sequence are: 0, 1, 1, 2, 3, 5, 8, 13

We can find the n'th term in the sequence where n is the first term using recursion. Right off the bat, we can easily identify the base cases which require no recursion at all. This is the case where we are asked to identify the first or second term.



def fibonacci(n):

    if n == 0 or n == 1:
        return n



Now comes what happens if the base cases are not fulfilled. In this case it is when the term we are finding is neither the first or the second term in the sequence. We know that the n'th term is determined by adding the two terms before it and notice:


To get the third term: we add 0 + 1 = 1
In other words, fibonacci(0) + fibonacci(1) = 3rd term in the sequence

SO MAYBE..... fibonacci(2) = fibonacci(2-2) + fibonacci(2-1) and since n = 2

                      -> fibonacci(n) = fibonacci(n-2) + fibonacci(n-1)



Thus we come to the conclusion that our function to determine the n'th term in the fibonacci sequence should be:


def fibonacci(n):

    if n == 0 or n == 1:
        return n
 
    else:
        return fibonacci(n-2) + fibonacci(n-1)



Let's test our function:

For the 5th term: (n=4)

None of the base cases are fulfilled so we call fibonacci(2) + fibonacci(3)

fibonacci(2) does not fulfil base cases so it calls fibonacci(0) and fibonacci(1)
These two fulfill the base cases and return 0 + 1 which gives fibonacci(2) a value of 1

Next, fibonacci(3) does not fulfil base cases so it calls fibonacci(1) and fibonacci(2)
fibonacci(1) fulfills the base case and gets a value of 1 and we already know fibonacci(2) gives a value of 1. Thus fibonacci(3) has a value of 2

Thus the 5th term = fibonacci(2) + fibonacci(3) = 1 + 2 = 3

This is indeed the 5th term in the sequence which proves that our function is right!


As illustrated, recursion can be a very powerful tool for solving problems. Had we been finding the 9000'th term in the sequence instead of just the 5th, it would most likely tire our brains out whereas the computer could compute it in seconds.

In Summary:

The key to recursion is to.....

1) Find the base cases in which absolutely no recursion is needed and a value can be returned immediately. This should be the condition of your "if" statement.

2) Find what you need to do to make your function work towards the base cases when it is called upon itself, as demonstrated by the Fibonacci sequence

3) Check to see whether your recursive call is not missing anything!

Sunday, 15 February 2015

SLOG: Week 6 Object Oriented Programming.

We're nearly halfway done the summer term and I feel like I have learned many important concepts, mainly something computer scientists call object oriented programming. This is single-handedly one of the most important and powerful tools in all of computer science that allows to do what we can do in modern programs today. To put it simply, it is the most fundamental aspect in coding and without it, you can't really accomplish much; similarly to how you can't do much with butter without the bread.

As it's name describes, the concept revolves around the idea of objects and that of course means it has certain attributes and behaviours. For example if we have an blue bird object, it could have attributes such as colour and behaviours such as flying or eating which we can translate into methods. This is essentially the basic idea behind it and despite being so simple, it can accomplish many tasks. When we create a representation of an object, we call it a class and by doing this, we can instantiate an instance of a class (create the object) as many times as we want and if one of the created objects were modified, only the one specifically modified would be affected. This allows, for example in a zombie shooter game, there could be hundreds of zombies instantiated from the same zombie class but if the player were to shoot one zombie, only that zombie would be damaged while the others would remain unchanged.

However what truly makes object oriented programming shine is the process of inheritance which brings object oriented programming to its full potential. Similarly to how you inherit genetics from your parents, classes can do so too except that they inherit everything from their parents not just specific features. This allows us to group objects with similar features up, creating a hierarchy and preventing a lot of repeated and unnecessary code from appearing.

For Example, if we were creating some hostile enemies in a game, our classes would look like:

Enemy (Parent):
Attributes: Health, Damage, Speed
Behaviour: Walk, Attack

Zombie (Subclass of Enemy):
Behaviour: Attack (Override), BadBreath

In the above example, we have a general parent class and then a more specific subclass. Our subclass which is called zombie, despite not having any new attributes attached to it not only inherited all the attributes of Enemy but its behaviour as well. As demonstrated, code is used more efficiently since we don't need to recopy any of our attributes that any Enemy should have into hostile creature classes that we make. Also we can even override methods from the parent class to make it fit the subclass more. For example an enemy could have an Attack method which simply damages targets at a range but clearly a zombie can't attack at a range (assuming he can only use his hands) and thus we can modify the Attack method and make it damage targets that are very close. Furthermore we could even add our own methods to our subclass such as BadBreath which can't be accessed by our parent class since in relation to children and parents, parents do not inherit genes from the children.

This makes object oriented programming one of the most useful tools in programming and when put together with inheritance, anything can be accomplished which is why I will be sure to try my best in honing my skills in this area.

Sunday, 8 February 2015

SLOG: Week 5

Last week we started learning about recursion and I found the concept to be relatively easy. The idea is that a function continuously calls upon itself which is a very powerful tool we can use in solving complex problems that we could normally not accomplish through writing simple python code. For example it could be used to count the total number of elements in a list or even a list of lists. Recursion is so powerful that many programmers today use it on a daily basis and I believe that if I can get a solid grasp on this topic, I will be very successful in my future programming career. Despite understanding the concept of recursion, I always have trouble coming up with the correct algorithm in which I can translate into python code for complicated recursive functions because it's pretty hard to think of what specific things I need the function to do each time it is called. To improve upon this I'm planning to practice tracing more recursive functions to the point where I can clearly visualize what each call will do without having to write it out.





On another note, we also had our first midterm this week which I felt I did a pretty good job on. However, I feel like I could have done better had I coded a bit more since I completely forgot what to put in methods that were meant to be abstract and overridden by subclasses. I now know that I should have wrote an exception or perhaps put pass instead of writing something that had no correlation to the question whatsoever. Another thing I should probably do next time is study a bit more since I ended up sleeping all night due to the fact that I was dead tired from staying up the previous night studying for a psychology midterm. 

Sunday, 1 February 2015

SLOG: Week 4

These first few weeks in CSC148 have been pretty hectic. Since the very first day of class, I noticed that the difficulty level of this course was exponentially greater than its predecessor CSC108 and through each lecture, the difficulty only gradually increased. In addition, the transition from having classes only three days a week to classes everyday only made it that much more difficult to adjust to the level of difficulty that this course had to offer. As a result I tried to review my notes everyday in order to understand the key concepts that were covered in lecture and to see how to apply them in future assignments/programs that I do.






Despite understanding the concepts, one of the main things I'm disappointed about are the quizzes. Although they were generally easy questions, I ended up doing pretty badly on them since my lab occurred at 9 am and I usually don't get enough sleep. As a result, I didn't read over the questions carefully enough and ended up writing a completely wrong answer. To improve upon this, I'm going to try to make a schedule to better manage my time and also to practice writing out code more so that I have an ample amount of time to finish the quizzes and check over my answers.