Tuesday 31 March 2015

WEEK 11 - March 23th - March 26th 

Revisting an earlier slog

This week's topic is revisiting an earlier slog and comparing our thoughts now and then. I would like to revisit my slog on summary of recursions.  Recursion is something I enjoy and comes more naturally to me than before. I feel more comfortable with recursions. A3 option B was a good choice to make as it gave  me a lot of practice on recursions. There were many functions for which recursions happened in the helpers and helpers were called in the main function. I agree with my thoughts that recursive code is simpler and more elegant than iterative code. Recursion is something I would like to learn more about. I have a long break for my CSC148 exam so I would practice a lot of recursive code during that time so that I feel more confident with the topic. Talking to other students I think everyone is now developing recursive thinking and everyone feels that more practice on the topic makes us sharp on the topic. I would also like to visit my very first slog Why Geeks Should Write. It is  now the 11th week I am writing slog and I realize seeing all my slogs aka posts that how I have summarized my journey of CSC148 in these slogs. I  wrote about what I learned, how I learned, what problems I faced and what I did to solve these problems. I think other benefit of writing is that you can share your thoughts and ideas with the public and receive feedback on your thoughts. I particularly liked what these slogs had to say about the topic Why Geeks Should Write 


While writing is beneficial reading is also rewarding. Reading other slogs helped me relate to different perspectives of the same topic. 


Sunday 22 March 2015

WEEK 10 March 16th - March 20th

This week I started working on A3 with my partners. We decided to work on option B as we didn't do quite well on minimax. I will do option A myself later on to get practice for the final exam. We started with the function grow and are still struggling with it. We are very very to close in getting it. We almost got it but when we wrote the function node_count we realized we didn't actually what we wanted to accomplish. I am sure we will be able to get it. This week I need to work more hard so as to complete A3 on time. In lectures this week we looked back on A2 and performed unittest on minimax and learned how to choose test cases. I  am still confused how to decide about choosing test cases but I think lab09 would give me that practice. I didn't get time to complete lab09 though I have started. I will post my solutions when I am done. We also performed exercises in class to learn about assertion, what I learned of it was we need to assert something that would be true if our function did its right job. 

def list_between(node, start, end):
    ''' (BTNode, object, object) -> list

    Return a Python list of all values in the binary search tree
    rooted at node that are between start and end (inclusive).

    >>> b = BTNode(8)
    >>> b = insert(b, 4)
    >>> b = insert(b, 2)
    >>> b = insert(b, 6)
    >>> b = insert(b, 12)
    >>> b = insert(b, 14)
    >>> b = insert(b, 10)
    >>> list_between(None, 0, 15)
    []
    >>> list_between(b, 2, 3)
    [2]
    >>> L = list_between(b, 3, 11)
    >>> L.sort()
    >>> L
    [4, 6, 8, 10]
    '''
    lst = []
    if node is None:
        return []
    else:
        if node.data > start:
            lst += list_between(node.left, start, end)
        if node.data < end:
            lst += list_between(node.right, start, end)
        if start <= node.data <= end:
            lst += [node.data]
        return lst

In the function above we can assert node = BTNode which I got using the docstring. The syntax for assert statement is as follows
assert <<condition>>, <<error string>>
We have the AssertionError(<<error string>>) if the assertion is not true. I hope to learn more about unittest and implement it when doctest testing is not sufficient. I would like to say a little about doctest and unittest before I end. Doctest are really good for testing but we got more accurate and extensive testing with unittest. There are also conditions where doctest is not possible so unittest are of use in those conditions. Hope next week has more interesting stuff to reflect upon.

Sunday 15 March 2015

WEEK 9 - March 9th - March 13th

This week we continued with linked lists. We wrote insert and delete functions. The idea of breaking down into cases is essential and must for recursive functions. We started with discussing all the possible cases and then packed up many cases together. Insert function was easier for me to understand than delete function as there was no problem of making a new root in insert which was there in delete function. We proposed an algorithm for making a new root if the value to be deleted was the root and did the required substitutions which seemed not quite natural to me. I got that after drawing pictures and talking to my friends. This week's lab was not easy for me because I did not have any tutorials to clear my doubts with TA's or lab partners. I was not sure what I was supposed to do in this week's lab so I would confirm it my professors next week. We also had term test 2 this week. I think I did well on it but can't predict before results are out. I am looking forward to learn more interesting and challenging stuff in the last week of classes


Sunday 8 March 2015

 WEEK 8 - Impressions Of Week 7 March 2nd - March 6th

Week 7 was all about Trees specifically Binary Trees and Binary Search Trees(BST). I really enjoyed learning about Binary Search Trees because the fashion in which they are designed made searching simpler and better than linear searching. I was initially confused why is the height to be searched in a BST proportional to log2(N) where N is the number of the nodes but I was clear with it when I drew pictures for trees of different heights and searched for elements at different levels. 

This Tree diagram represents a Binary Tree where each node has two children. This resemblance with family relations helps me understand the concept of trees better.


This tree is a BST where all the left children are less than the root and right children are greater than the root. This is a special property of BST which distinguishes them from Binary Trees.

Writing Binary tree functions we had to take special care of None which was to be one of the base cases. There was no None in n-dimensional trees because the children were stored in a list so they either had something or were empty. So dealing with None data type was one of the things which I  became confident this week and it was mainly with practising lab exercises which were pretty challenging. Taking inspiration from other slogger's blog I thought of sharing my solutions to the lab's which can be found here. I am doing this to help people struggling on labs and also to get feedback from other sloggers about my solutions.

I also misunderstood Minimax where I thought I had to built Trees but later realized that we didn't have to use Trees at all instead had to use Recursion which recursively built Trees.



Sunday 1 March 2015

WEEK 7 - SUMMARY OF RECURSION- Topic to be graded

I chose this topics from the list of topics to be graded because this is a new topic which I am learning out of the listed topics so I have more to share on this topic than others. Recursion is calling a function within itself. For example to calculate a sum within a nested list the best way would be recursive calls of the function to the inner lists
def sum_list(L):
    """
    (int or arbitrarily nested list of int) -> int
    
    Return the sum of all ints in L.
    
    >>> sum_list([1, [2, 3], [4, 5, [6, 7], 8]])
    36
    """
    
    if isinstance(L, list):
        return sum([sum_list(x) for x in L])
    
    else:
        return L
A recursive solution is concise and elegant, an iterative solution is long, complex, and inelegant. For writing recursive code, thinking informally helped me a lot that is thinking if I had a bigger problem version of problem, then calling helpers on smaller versions of same problem and then using the result of helpers to solve the larger problem.
Recursion involves three steps
  1. Choosing a base case which would be our if statement
  2. Thinking of smaller instances of the same problem, tracing calls to smaller instances of the same problem
  3. Gathering the result from all the helpers (smaller instances) to solve the larger instant of the problem and return that in the else statement
Recursion was way easier following these steps. The part where I was usually stuck was second part. Thinking of smaller instants requiring recursion was a tough call. But practicing made me comfortable with second step. Most of the tree methods require recursive calls. It is easier for the node to ask it's children the required information than calculating itself. For example in the height function gathering height of all the children and finding the maximum out of them was better then using iterations for me. Recursion is making my coding life simple and easy.
The above code shows a same problem with recursion, for loop and while loop. The code for the recursion is shorter, simpler and elegant while in the case of loops it is longer and less elegant.
So overall recursive code made me understand the importance of beauty and elegancy in an programming language.

I would also like to share my recursive code exercises solutions from the lab here