Sunday, March 29, 2015

Week 11 Visit to an earlier SLOG

Week 11
Visit to an earlier SLOG poster of mine



It seems to me that “Introduction to Computer Science” has recursion as its main focus of study. We are close to the end of the semester, and I have realized that we spent two thirds of the course discussing this topic. For this reason, I would like to revisit my earlier post on recursion in order to get a fresh insight and to master the concept.

The post I am revisiting has as title: “Week 7 Recursion/ Math connection”. It deals with recursion and its use in lists and trees, as well as the connection with mathematical notions. Back then I wrote “It is interesting to see that basically the same concept can be applied, no matter which object we are working with.” Having work with recursion, for around 5 months by now, I can confirm this statement. Indeed, recursion is in every process that aims to repeat a task a considerable number of times. Thus, this concept is not limited to lists, and trees.

Moreover, although I did not emphasize it in the post, I now notice that recursions dealing with lists and trees share the same logic. We can actually think about lists in terms of nodes. For instance, imagine we have a list (node) which contains some elements (children nodes). Once this notion is clear we can follow exactly the same logic, and creatively apply the recursive function to the elements of the lists, which are no other thing that the children nodes of the tree. The connection may be more noticeable when looking at the following code:

Recursive step for a list
return sum([sum_list(x) for x in L]) 

Recursive step for a tree
return sum([leaf_count(child) for child in t.children])

In the code above, I have purposely coloured the parts that seem to be different and left the parts that are equal in black. As you may have noticed, the corresponding coloured parts have the same functionality. Therefore, we can conclude that the process of recursion indeed follows the same logic.

It is also important to notice that the process of using a case and a recursive step also remains intact for both lists and trees, and in fact for any other function using recursion. Therefore, it is always a good idea to carefully look at the following “if else framework” before the exam:

if (condition): #Base Case
else: #Recursive Step

Peers reactions about the topic

By looking at other people’s slogs, I noticed that some people are having problems with understanding recursion. For instance, Dottori states: “I have decided to revisit last week's (week 10's) post, as it describes the continuing struggle I have faced with grasping and mastering recursion.”

As I told this person, mastering recursion is about realizing that we are just making our children nodes do our work:
http://dottoripy.blogspot.ca/2015/03/week-11-revisiting-previous-post.html?showComment=1427680678374#c4600756507989807541

However, there are other people who have been able to wrap their head around the topic. For instance, Rod claims that practice has made him become more confident about recursion:
http://rodcsc148.blogspot.ca/

Finally, since I know that evaluating peer interaction is difficult, I would like to include some of the links of the blogs and discussions I have taken part in, here:

·         http://hayleylincsc148experience.blogspot.ca/2015/03/week-5-term-test-and-recursion-continued.html?showComment=1425821803323#c2948885543747290257
·         http://leopharandcsc.blogspot.ca/2015/02/week-4-lol-lots-of-learning.html?showComment=1425823356912#c9171090264996370596
·         http://slogwhygeeksshouldwrite.blogspot.ca/2015/02/week-6.html?showComment=1425825049576#c3125285132158934534
·         http://tonieewtbeageek.blogspot.ca/2015/01/why-should-geeks-write.html?showComment=1425825836153#c8369804375529975109
·         http://thisishomeofrui.blogspot.ca/2015/01/csc148week3.html?showComment=1425826429711#c2034789993238378983
·         http://wajw.blogspot.ca/2015/01/why-geeks-should-write.html?showComment=1425826736289#c1558642776147235726
·         http://csc148slog2015.blogspot.ca/2015/03/week-8-slog.html?showComment=1426275226075#c4220407772723293115
·         http://kirincao.blogspot.ca/2015/03/normal-0-10-pt-0-2-false-false-false-en.html?showComment=1426276256796#c5202222069714805659
·         http://darkfoxnewslog.blogspot.ca/2015/03/slog-for-week-9.html?showComment=1426904314181#c8008980281488216210
·         http://148choi.blogspot.ca/2015/03/week-9-my-impressions-of-week-8.html
·         http://dottoripy.blogspot.ca/2015/03/week-11-revisiting-previous-post.html
·         http://dottoripy.blogspot.ca/2015/03/week-11-revisiting-previous-post.html?
howComment=1427680678374#c4600756507989807541
·         http://rodcsc148.blogspot.ca/

Friday, March 20, 2015

Week 10 Impressions of Week 9

Week 10
Impression of week 9

Week 9 was mainly about having a look back at assignment 2 and obtaining some insight on how to ensure good coding. The use of assert calls and the tracing of code were also covered.

Assignment 2

Two things were highlighted when talking about assignment 2:
1) Minimax Strategy and
2) Apply Move

Minimax Strategy

Special emphasis was given to explaining how minimax works because it will play an essential role in assignment three. Minimax was one of the most difficult methods to implement, but after having a look at both my group’s approach and the sample solution, it is clear to me how it works. In a nutshell, minimax is playing the game for us and telling us which move can guarantee us a win, a tie or a lost, in that order.  

Moreover, the running time for the program on assignment 2 was definitely an issue, but assignment 3 will deal with that problem. As shown in class, avoiding repeated states may be a good way to deal with this.

Implementing classes and methods in assignment 2 really helped me understand how to use inheritance and how to write recursive code. Also, I consider the use of classes and inheritance to be a really good way to structure understandable code and I am planning on using this approach in my projects.

Apply move

One of the most common mistakes was misreading the docstring and changing the state instead of returning a new one. Changing the state caused minimax to misbehave and therefore our program to fail; therefore, returning a new state was the best approach to take.

Assert Calls

It seems to me that a proper use of assert calls is going to make it easier to find bugs on time. Assert calls are a powerful tool because not only it allows us to find our mistakes, but they tell us what exactly failed. Without a doubt, this means spending less time on fixing code and more time on developing new.
However, the success of this tool lies on its correct use. Having a good grasp of how the code works is needed in the first place.

Tracing calls


The focus of this topic was on recognizing and understanding the difference between local and global variables. It is important to notice that when talking about naming, the more local is the reference, the more right has a variable over a name. Let´s consider the following case:




MAX = 4        # these variables are global. They are also constants
MIN = 2          # constants are written in upper case letters by convention.

def  use_my_min(MIN): # The moment this function is called the value that MIN refers to
                                        # changes and points to the value of the argument passed
            return 1 + MIN

def use_global_min(num): # We do not make any change to  MIN, therefore we use the value
# assigned in the first place.
            return 1+  MIN

def  use_my_max(MAX)
            return 2 + MAX

def use_global_max(num):
            return 2+  MAX

Executing this code and coding on the shell

>>> use_my_min(3)
4
>>> use_global_max (3)
6
>>>  (use_my_min(3) + use_global_max (3)) * (use_my_max(3) - use_global_min(3))
20

Course Interaction:

I was looking at other people’s slogs and I found out that some students are going to tutorials, even though there is no tutorials anymore. I am planning on doing the same this week.

http://darkfoxnewslog.blogspot.ca/2015/03/slog-for-week-9.html?showComment=1426904314181#c8008980281488216210

I would also like to use this space to thank Ji Yong Choi for his encouraging words. It is nice to see that people are actually looking at my slog and that I can be helpful in one or another way.


http://148choi.blogspot.ca/2015/03/week-9-my-impressions-of-week-8.html

Friday, March 13, 2015

Week 9 Impressions of week 8

Week 9
Impressions of week 8




Week 8 dealt with the concept of Linked lists and Linked List nodes. Linked lists is the topic I like the most so far. It may be due to the use of drawings that helps me understand the code I need to write. There is a couple of things that made me understand this topic better and I would like to share them here:

When talking about linked lists, the three variables that should come to mind are the following: 

self.front: Initially refers to None, but as we add Linked list nodes it will refer to the first linked list node in the linked list.
self.back: Initially refers to None, but as we add Linked list nodes it will refer to the last linked list node in the linked list.
self.size = Initially refers to 0, but it will change, increasing when adding nodes, but decreasing when deleting them.

In terms of Linked list nodes, two variables are of main importance:

self.value: It refers to the data contained in the node
self.nxt: It refers to the next node in the linked list. This variable is crucial because it allows the linking.

Everything that has to do with linked lists involves walking through the list and it can be achieved as follows:
current_node = self.front # self.front refers to the first node in the linked list
While condition: # This condition will make the loop stop, therefore we should include  
# while node: as a condition to avoid getting errors
            current_node = current_node.next # After executing this statement we do not 
# longer remember the first node 
# in the linked list

The way in which we change things matter. For instance, if we change the reference for self.back before using the node that self.back refers to, we will lose that node forever.
I think I have covered all the tricky parts about Linked lists, but I look forward to learning more about them.

Course Interaction:

I was looking at other people’s slogs and it seems that some have not mastered this material yet. The problem seems to be understanding what to reference and what to change. I gave specific suggestions, but the general advice is that we should always make drawings.




On an aside note, it was very hard to find people actually doing their Slogs and commenting on Linked lists.

Sunday, March 8, 2015

Week 8 Impressions of week 7

Week 8
Impression of week 7




On week 7 we talked about binary trees and its nodes. When dealing with this kind of trees it is important to notice that the root will have two children, one to the left and one to the right. These children may or may not have other children, but they can only have two.

Arithmetic expression trees were a little bit more difficult to understand. The fact that some nodes have data (arithmetic expressions), and others do not made me got confused. However, after looking at them, it became clear that it all boils down to taking the same approach, a base case and a general behaviour case. For the base case, we just have to return the expression since we would have likely reach a leaf at that point. For the general behaviour case we need to evaluate the expression generated by combining right and left child with the corresponding data.

As for understanding how preorder, postorder and inorder work I would suggest the following useful online resource: https://www.youtube.com/watch?v=gm8DUJJhmY4

Speaking about order, binary trees are special in the sense that they require ordering. The value in the left subtree must be less than the node that contains it, and the value in the right subtree must be greater than the node that contains it.

Course Interaction:

This week I would like to share the following phrase written by Lisa in her Slog http://slogwhygeeksshouldwrite.blogspot.ca/

“I think the key concept is: don’t start [from] details but start from structure.”


This is definitely a good advice to take home. Structure is really important when programming. A clear structure can help not only the person coding, but people looking at that code. Also, having a plan before starting anything helps to clarify ideas and to have an easier time coming up with a solution.

Sunday, March 1, 2015

Week 7 Recursion/ Math connection

Week 7 Recursion

I spent some time on my Week 6 slog giving a complete summary of Object-Oriented Programming, recursion and Abstract Data Types; therefore, it would be really nice if that piece of work could be graded. However, since I am passionate about the course I would like to share some words about recursion and the connections I found between this course and MAT137.

Recursion

As the course goes on, recursion is becoming a powerful tool that helps us deal not only with lists but also with trees. It is interesting to see that basically the same concept can be applied no matter which object we are working with.
Talking about the use of recursion in trees makes us go back to taking base cases. The base case in a tree is usually the point at which we reach the leaf (node that has no children) of the tree. The base case when working with lists is usually the point at which we hit a value instead of a list. 

Another interesting fact about recursion is that we are executing a considerable number of operations by writing as few as four lines of code which could even become one line. Without a doubt, recursion is powerful because of its concise form and functionality.This can be clearly seen in the recursive examples below:

Recursion for a list
def sum_list(L):
    ''' (list of list of int) -> int
    Return the sum of all ints in L.
    >>> sum_list([1, [2, 3], [4, 5, [6, 7], 8]])  # Working with lists
    36
    '''
     if isinstance(L, list):    #BASE CASE
                return sum([sum_list(x) for x in L]) #Recursive part of the function
            else:
                      return L

Recursion for a tree
def leaf_count(t):
    ''' (Tree) -> int

    Return the number of leaves in Tree t.

    >>> t = Tree(7) # Working with trees
    >>> leaf_count(t)
    1
    >>> t = descendents_from_list(Tree(7), [0, 1, 3, 5, 7, 9, 11, 13], 3)
    >>> leaf_count(t)
    6
    '''
    if len(t.children) == 0: #BASE CASE
        return 1
    else:

        return sum([leaf_count(child) for child in t.children]) #Recursive part of  the function

Recursion in Mathematics


Now, let’s take into account the above recursive examples and look at a recursive sequence in mathematics.



Defined {Fn} by

F1 = F2 = 1
For any n ≥ 3, Fn = Fn-1 + Fn-2
A few terms:
F3 = F2 + F1 = 2,
F4 = F3 + F2 = 3,
F5 = F4 + F3 = 5,
F6 = F5 + F4 = 8


This is clearly a sequence that depends on a recurrence relation.
 (Fn = Fn-1 + Fn-2)
 Just as in the recursion examples above we are calling a function within another function which creates our sequence.