What is OOP(Object-Oriented Programming), my partner and I met this question during the first lab of this course. Now half time of this course has gone, and we should have better understood the meaning of OOP and how to use it, but the first lab really gave me and my partner deep impression about OOP.
The first lab, our main task is to count the number of occurrences of words in a piece of text, and store the words with their frequency, find the "top n" words, and compare one collection to another. We thought it was not difficult, after a short discussion, we started to code. We knew how to open a file, read file, split, strip, and get a list of words. We use "for loop" to check every word in the list, find their frequency, and store them in dictionary, words are the keys and frequencies are the value. Every thing went smooth well. After we finished coding and implementation, we shew our work to TA. To our big surprise, TA told us that what we were required was Object-Oriented Programming, and what we did was just a procedure, though it was program, but not what this lab asked. Then we read the lab handout again, this time we found something we missed: "Perform an object-oriented analysis of the specifications in specs.txt". We just did discuss how to make the procedure and how to make it work on Python, but ignored the key word "object-oriented analysis, or we simply did not understand what it meant. We had to redo it: find the major noun and write the header for an appropriate class definition, along with a short docstring for the class; then find the main verb and write the header for an appropriate method, along with a short docstring for the method, and so on...
This first lab did give me a deep impression on OPP.
Sunday, 23 February 2014
Thursday, 6 February 2014
Recursion -- The Power of Recursion Insight
This week's lab, we were requested to write some code using recursion which we learned in lectures. The first question is to write a function to check if some number is in a possibly nested list. The header and description as below:
after a short discussion with my partners, we quickly formed a solution. The idea is: "for item in SL", check each item in the list SL, if the item is a number, then check if item == n; if the item is a list, then we check its items one by one, using for item in list. If we find any item equals to n, then return True and stop; if have checked all the items but no one equal to n, return False.
We were happy to have a solution. While my partner was typing the code in Pyphon, suddenly I remembered that the function the instructor showed in class has only one line -- return line for function body while our code has 8 someting lines. I realized what we had done is what we learned in CSC108, not CSC148. Then I asked my partner stop typing and told him that we need to write a one-line body function. Surprised by my words, my partner didn't believe we can do the job by one line. Then we checked the examples teacher used in class.
We checked the example "sum_list(L: list)", it uses only one line to sum up the numbers in a possibly nested list of numbers. We studied the function carefully, and wrote our one-line body function. save, run, and doctect, call check, everything was ok. Then we called TA to check our job, "GOOD!" was his answer.
Using this idea, we quickly completed the second question, and then did some extra problems.
The key is using recursive code; and the power is recursion insight.
def find_num(SL: 'SearchList', n: int) -> bool:
"""
Return True if n is in SL, False otherwise
after a short discussion with my partners, we quickly formed a solution. The idea is: "for item in SL", check each item in the list SL, if the item is a number, then check if item == n; if the item is a list, then we check its items one by one, using for item in list. If we find any item equals to n, then return True and stop; if have checked all the items but no one equal to n, return False.
We were happy to have a solution. While my partner was typing the code in Pyphon, suddenly I remembered that the function the instructor showed in class has only one line -- return line for function body while our code has 8 someting lines. I realized what we had done is what we learned in CSC108, not CSC148. Then I asked my partner stop typing and told him that we need to write a one-line body function. Surprised by my words, my partner didn't believe we can do the job by one line. Then we checked the examples teacher used in class.
We checked the example "sum_list(L: list)", it uses only one line to sum up the numbers in a possibly nested list of numbers. We studied the function carefully, and wrote our one-line body function. save, run, and doctect, call check, everything was ok. Then we called TA to check our job, "GOOD!" was his answer.
Using this idea, we quickly completed the second question, and then did some extra problems.
The key is using recursive code; and the power is recursion insight.
Subscribe to:
Posts (Atom)