When this course started, I
noticed that this course is called “Introduction to Computer Science”, while
CSC108 is called “Introduction to Computer Programming”. Looks similar but a
whole lot different. In CSC108, we mainly spent our time learning a programming
language. However, in this course, I think we are learning about program design
concepts, like object-oriented programming.
In OOP, the focus is on the
creation of objects, which contain both data and functionality together. In the
first class, we learned how to create a user-defined class: the Point. Although
we could just use tuple to store the coordinate of a point but I don’t think
that’s a good way, because a point has many properties that a tuple doesn’t
have, so why don’t we just create our own class? I find the design process to
be easy because we already learned about designing class in CSC108.
Nevertheless, it must be noted that I had some problems. Firstly, style. I used
to write the type contract below the def
line not in the parentheses. Secondly, when designing the __eq__ method, I didn’t know we need to write (isinstance(other, Point) in order to compare whether the two
objects that we are going to compare are both Point objects. I then realized
that this is necessary and very important. The reason is that Point objects are
list-like objects, so if we compare [3,4]
with Point([3.0,4.0]) without
checking if they are all Point objects, the result will be True. Moreover, I
In Lab01, we designed a class to
count the times of appearances of each word in a document. The problem my
partner and I had was that how to sort a dictionary by its value when designing
top_n method. We tried sorted() and a couple of other methods
but it doesn’t work. Then with the help of TA and some python guides, I found
that we missed the lambda function,
just write sorted(d.items(), key=lambda d:d[1],
reverse=True), we can easily sort the dict by
its values(d:d[1]) or its keys(d:d[0]) reversely. Here is my top_n:
>>> b=[]
>>> for i in range(n):
b.append(sorted(a.items(), key=lambda
d:d[1], reverse=True)[i])
return b
It works
perfectly.
I feel a little
frustrated because it cost me more time than I had thought. I think I need more
practice.
In the second lecture, we designed the
stack class, which I found to be easy. But the recursion function confused me,
I didn’t understand that we can call a function within its definition. Then I
found this answer on Piazza which helped me a lot:
“Well first of
all, it doesn't matter that you call a function within its function definition.
My understanding of it is that at the beginning of runtime, the code finds all
the def statements and makes the function names point to "run the code
from line x to line y". So you're not technically calling it in the
function definition, because it was already defined and can be called whenever
you want now.”
Now I understand how to write a
recursion function:
def sum_list(L)
result = []
for x in L:
if isinstance(x,list):
result.append(sum_list(x))
else:
result.append(x)
return result(array)
But I still have some problem writing
code like this:
def
sum_list(x):
return
sum([sum_list(x) if isinstance(x,list) else x for x in L])
This piece of code is very succinct and
works just perfectly but it looks kind of ambiguous to me. I may have to write
this code several times before I get it right. I think some more practice will
help.
In lab02, our task is to using class
Stack and Queue. The tasks are pretty simple. And the part about the efficiency
is really interesting. The two classes Stack and Queue are similar but their
running time are so different. The problem is that in Queue, you pop the first
element while in Stack, you pop the last one. To fix this problem, you must not
remove the element because it costs too much time.
In week3, we will learn about
inheritance, which is an important concept in object oriented programming. I
guess we will learn about polymorphisms in this courses also.
In my spare time, I am
learning Objective-C. Python and OC are all object-oriented programming
language. In my opinion, I think Python’s programming language is more
straightforward, easy to read and write. Programming in python is just like
writing in English.
For
example:
In
OC, it takes 20 lines to finish a piece of code. While in Python, we just need
to write 10 lines. But fortunately, Xcode is very powerful and it’s free!