Thursday, 27 February 2014

Week of Feb 27th:

Learned alot about OOP this week in studying for the midterm and starting a2:

I now understand inheritance much better, and I see that the __init__ and __eq__ methods are just that: methods: I had a hard time as I thought they were somehow different from any other methods, but after working through the boundedStack Subclass and IntStack subclass exercises I understood how their inheritance actually works as I had to call it form my superclass Stack:

I became much better at one statement recursions after going through the nested_depth and max_depth examples and now have no problem using them on the fly. My issue was that I was not comfortable not seeing it a usual loop structure (my TA presented it to us like this) and hence I could not easily identify the base case when seeing it in one line, but after trying it out a few times it finally clicked.

For linked lists this week, my partner was away (for which I am grateful), and I got to do the whole lab by myself, so I felt like I learned twice as much. I had no problems reimplementing the queue as a Linked List but spent an hour trying to make sure it decapitates like a queue and not a stack; I'm still not clear on this so I will have to ask my TA next week.

I also realized why I was having such a hard time with init before: my shell doesn't allow me to define the class types for self and other input variables in the init statement, so all the programs I wrote before would never run in my shell and I didn't get why; I also can't use the ) -> bool/object/None,etc: to end off my inits, but once I figured this out everything has been much easier.

I did almost all of a2.1 today; in fact I had alot of fun. My regexes work exactly like trees; for me the biggest challenge was getting the correct __repr__ for say, regex1, but then removing the quotation marks when regex1 was used as an input in some other regex2. I solved this by assigning a self.value within each regex's init that holds its actual character and then using the repr to just add outside ' 's to the self.value; I've been able to successfully duplicate every compound regex on the assignment sheet so far so I assume it works!

One error I made today was to write if regex == s statements for each regex's __eq__ method, but I see now that for more complex and longer regexes this might not work. Instead, I need to redefine them recursively using the input regex __eq__ methods, so that

regex==s becomes regex1.__eq__(s) etc.This will link it back to the base case, which is the single length regex eq method.

Also sorry this entry is late, I completely got carried away with a2!

Thursday, 13 February 2014

Feb 13: Filter, Zip, and A1

This week in lab I learned about the zip function. This function takes at least 2 lists, and creates a new one with tuple entries corresponding to the first items in both. So, for example, if I had a = [1, 2, 3] and b = [a, b, c], zip(a,b) would give me [(1,a), (2,b), (3,c)]. This is like a matrix concatenation of sorts and was also exemplified in the lab, where it was used for  some basic linear algebra functions.

The lab was great. We got to strip down and rewrite dot and cross product functions without using list comprehensions; we built them up form scratch. For example, the filter list comprehension is both a loop and an if statement in one line, and is a much more efficient way of 'filtering' whether to do something or not, but once we  rewrote it as the original loop structure, I could better understand what was actually happening inside.

We finished assignment 1. Actually using the things we have learned over the last 6 weeks in such a dynamic setting greatly enhanced my understanding of OOP. Inheritance, subclass, method headers, the init, I now understand all of these. I was impressed with how streamlined and efficient the OOP code allows this game to be. As we programmed it, we 'built' up from the ground and got to see the various functions pass objects back and forth between each other. So it made sense that console controller wouldn't work without TOAHModel, and etc.

I also finally understand the if name == main; all somebody had to tell me was that whatever code you write underneath will be executed when the program runs. I also zeroed in on instantiation as I had a huge problem where my console controller was trying to run a general model (i.e TOAHModel()), but now I understand that to create an instance of the game I had to take one particular case that inherits from the class, so for example game1 = consolecontroller(4) which was then passed onto TOAHModel and instantiated as one instance of the TOAHModel class. The TA really helped with this.

Lastly, on a1 recursion. Originally I thought I had to establish a base case for the Tour min moves function M(n), but a friend suggested a better approach (I thought). 

Instead of working backwards to the base case, we built 2 loops that accumulated the i value giving us the min moves value, and a list of the corresponding Ms. So this was really like a reverse recursion where we built the list going forward, meaning that to solve n = 6 you need to know all the previous ones, but they had been built up for you. This really interested me as I think in this instance a simple loop structure accomplished the same task but in a way that seemed way more intuitive to me. In terms of OOP, I mean I was surprised to see that there may be more than one approach and that everything I've learned in class is really a tool, to be used depending on what the situation calls for.

Finally, I've found a much quicker way to deal with raising assertions, using 1 or 2 lines and an assert condition with a comma and the message following after.

Thursday, 6 February 2014

Feb 6: a1 and recursion.

This week was very productive as my partner and I really got into a1. We've written the raw functions and the whole thing looks functional; now comes the debugging. Overall I really liked this assignment for how it made us check the relationships between different classes. Throughout this I think I really saw the value of OOP, and the whole 'system' really appealed to my intuition. 

The lab was even better, and I really liked that they gave us super simple examples. Now I'm very curious to see how a more complex recursion would look, in the sense that in terms of efficiency this really appeals to me. My partner and I did the extra exercises at the end of the lab, and even started the string reverser recursion (which fascinated me the most). 

Ive gotten better at syntax, defining classes and functions, and all the stuff that was troubling me at first. I'm worried for the midterm as I haven't written a paper programming test since 2010 and I still find myself having to resort to google when implementing things. On the other hand, I've been really surprised at how helpful all the TAs in this department are (meaning, in my major, this never happens), so I think it would be stupid on my part to not use them as a resource. Namely, I'm going to ask someone about __init__ because I am still struggling with it. I get what it does, what instantiation is, etc, etc, but I need to understand, algorithmically, what the implementation looks like in my mind. (For example, in a1, it says that my use of init for TOAHModel is a syntax error and I have no idea how to debug it as it looks no different than  the way they used init for cheese.).

In any case, I really liked the methodology of this assignment, because while it was a simple task, working backwards from the fully functional code was a really cool way to learn it; in some sense, you can't do this without a thorough understanding of what's going on . Anyway, I'm most stoked for the recursion part, so we'll see !