Thursday, February 26, 2015

Slog 5: recursion

At first time, I found the concept of recursion very confusing, but this example(from lecture) helped me to apprehend it.


def sum_list(L: list) -> float:    """    Return sum of the numbers in L.
    L --- a possibly nested list of numbers.  L, and all sublists, are          non-empty
    >>> sum_list([1, 2, 3])    6    >>> sum_list([1, [2, 3, [4]], 5])    15    """
    return sum([sum_list(x) if isinstance(x, list) else x for x in L])

and this code for the same function:

def sum_list(L: list) -> float: 
    total = 0
    for x in L:
        if isinstance(x,list):
            total = sum([total, sum_list(x)])
        else:
            total = sum([total,x])
    return total
 
The seemingly complicated problem of how to solve a list of lists is easily solved with recursion. 
To sum a list of lists, start with the initial list, if the first index of that list is a number, at it to the sum, if the next object at the next index is also a number, at it to the sum too, and so on. It is easy to write a function to do this, it's just getting the sum of numbers in a list. When a nested list is encountered, we still want to get the sum of the numbers in a list, however our object is a list instead of a digit. So, we just call our function on this list, then we get the sum of the nested list and it is added to our total sum. The big problem is solved by repeating the solution to a sub problem, in this case, summing lists.


The recursion handout, which is from the lecture, and the lab about recursion helped me out. Especially during the lab, the lab worksheet contains the way to tracing each steps of recursion function, so that I was able to understand step by step. Not only from the lecture and lab, but also from piazza, I learned some confusing concepts. For example, I was confused about writing each steps of inner recursive part, but viewing other classmates' comments and posts, I solved my difficulties.

Wednesday, February 11, 2015

Slog for Week6: summary of Object-Oriented Programming concepts

Before I begin to write summary, what is "Object-Oriented Programming concepts?"
"Object-Oriented Programming" is a programming paradigm that views a computer program as a combination of data structure.

So what we learned in csc148 about "Object-Oriented Programming concepts?"
We learned about formatting parent class with several methods such as __str__, __eq__, __init__, and etc, and formatting subclasses based on its super class.

Basically when we write the super class, sometimes several methods in super class are written as 'NotImplentedError'. This is because super class does not specify only one specific subclass, but it is a 'general class' that contains several subclasses' general  information at one time.

For the subclasses, now we write the code that what we need to implement. If the method is already existed in its super class, and it does not need to extend, then you can simply skip it. However when you feel it needs some extension, then you write the code, and sometimes a new method for only itself.

Designing class and subclasses is not hard;
First before you start to write the code, draw a big picture how you are going to write.
then think what code you will need, and the appropriate names for each method and classes.
If you can do these two things, then you will be safe in "object-oriented programming."

I also feel difficulties designing classes at first time, but the practice helped me a lot.
Assignment 1 really helped me, and first two labs too.

Wednesday, February 4, 2015

Slog3: impressions of tracing recursion from week 4

On week 4, we learned about tracing recursion, and while I reviewed for the midterm that I just had today, I realized that the importance of recursion is well-understanding of the one-line-coding, and finding out the pattern of the function, and use the pattern to the most complexed one. 

So... here is my recommendation of RECURSION myself:
1. read carefully of the one line coding or several lines of coding first
2. by tracing few simplest one, figuring out the pattern!
and one more important thing is
3. write carefully of parenthesis!!!! <- while I practiced tracing recursions during the lab, and lecture, I noticed that small difference in parenthesis cause totally different result!
and the most important thing is 
4. Do practice of tracing recursion many times until I become familiar of it

These are all I feel on week 4 and I recommend you and me that remember these things before you are on recursion.The recursion handout, which is from the lecture, and the lab about recursion helped me out. Especially during the lab, the lab worksheet contains the way to tracing each steps of recursion function, so that I was able to understand step by step. Also seeing posts and comments in piazza helped me too since I noticed many classmates have similar problems with me.