Sunday, March 29, 2015

Getting close: A look back

The term and school year is almost over as we approach the final week of winter term 2015. Looking back at all the slog posts I made, I see some of the slow but steady improvements and some of the problems that, to this day, I still haven't properly solved yet.

For one, I felt like I actually learnt and was able to comprehend most of the new concepts that were introduced in the course. Things like linked lists, ADTs and class design, all seemed to come natural to me as it either took little to no time at all to learn and understand. On the other hand, ever since the beginning, I still continue to have problems grasping recursion. It is not like I don't understand what it is or how it works, in general, it is when it comes to complicated examples or certain situations, I have a hard time imagining how recursion will work, or I would be unable to come up with the proper coding/algorithm to make my idea work. Another concept that is tied to recursion is Trees. Although, again, I understand the general concept of trees, some functions I have a hard time of understanding, which is reflected by my struggles during A2, in strategy minimax.

My slogs would reflect the struggles I've had with these concepts and I just can't get it. It may be because my programming style is different or I don't know the full capabilities of python yet but surely, I will find a way to master it, and I believe it has to do with looking at more examples of recursive functions.

As for what I've seen from other fellow computer scientists, other people seem to have a tighter grasps on recursion compared to me but other than that, I feel like I'm learning at the same pace as everyone else which is more than enough, considering my background in computer science.

Sunday, March 22, 2015

Chopping down a Tree with tests

This week we looked at a function that could delete a node from a binary tree. It turns out, a lot more complicated than what I had initially imagined. There are many considerations that have to go into your code for it to not break the tree, such as checking for any children that are attached to the node. The condition that I was confused about is that once you chop off a node, you have to reattach the children to the tree. It would seem like by the way binary trees work, any nodes that are deleted, unless they are leaves, tend to force the the tree into a particular shape, mainly left sided. 

As well, the idea of floating data, such as when we delete a node, what happens to that data? It is an interesting concept since not many people know what happens to data that has been deleted, and the idea of floating data can raise the question of, if we repeat a task multiple times that results in floating data, does that affect how the program runs?

Lastly, we had our second test. Overall, the test was fair and I believed I did well, except with less confidence as the first test considering the strike has made it harder to keep up with the topics, with the lack of labs. There was one question that was a modification of a function that we learnt in lecture that was about linked lists and I'm uncertain of the coding for that question. I'm also unsure if I ended up reassigning the back or not in my code. Again, I believe that I did well although unsure of that belief. Hopefully the strike will end soon so I will know for sure. 

Saturday, March 14, 2015

A certain link is missing. STRIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIKE.

Assignment 2 is finally done and over with. I am still worrisome about how I did due to the fact that the algorithm I came up with for checking tippies seems very inefficient. As well, my algorithm for Minimax is also atrocious. It involved creating a list of scores of different moves, going down all the possibilities until it reached a state where a move only had a straight path of following moves to go down, and would return that score. My troubles with recursion showed here because the resultant list I got back every time was a massive, massive list with many many inner lists. In the end, the algorithm required to iterate over said list two times which resulted in very very long processing time for values over 50-ish for Subtract Square and especially for 4 by 4 + in Tippy. Other than that, I was pretty satisfied that I managed to complete it. 

As for what we learnt in lectures this week, it was mostly about linked lists. I find this to be one of the easier concepts out of these past few weeks since linked lists involves iterations to traverse it and only has 1 path (linear). No recursion, even though some methods would need recursion, i still had an easier time understanding it.

Lastly, the strike. I never thought I would require my TA as badly as I do now. I found that in 108, having to constantly be required to do work that is related to the week's teachings makes it a lot easier to keep up and understand. With the strike going on and a lack of tutorials, I find that I'm constantly behind in topics, and this is especially bad since the topics that I am missing all have to do with recursion, which I have been struggling with over the past few weeks. It's hard for me to learn concepts without a final explanation or solutions to go with what I did, and the lack of labs gives me an incentive to not work, which will definitely bite me later on.   

Sunday, March 8, 2015

Binary search processing...

    Week 7 of the winter term and the 'questionable decisions' are rolling in. First off, I regret, greatly, not spending more of my time during the reading week working on A2 since I realized, figuring out a proper algorithm for checking for tippies, a lot harder than one would imagine. Don't even get me started on minimax. Only time will tell me how well I do on this assignment as that is what I need most right now.

    On the other side of the coin, binary search. The concept of binary search seems like it could be a very powerful tool in many situations, not just to combine elements of mathematical expressions. I would imagine the features that are tied into folders, that we take for granted, are tied into binary search. This weeks lab didn't go as smooth as I would normally expect. I assume it was due to the fact that my previous intuition of recursion is still lacking in some parts. One of the questions asked us to create a method called 'list_longest_path'. I had a hard time figuring out how the algorithm works since I repeatedly questioned how I would search two branches at the same time. and figure out which branch held the longest path. In the end, after asking the TA, I slowly applied an algorithm I assumed would work which lead to a common problem that I have with computer science, which is: 'My code works, but I have no idea why.' The methods afterwards were also challenging as there were many possible outcomes that had to be taken into account, which, again, took me a long time to figure out but in the end, the method only worked for the given example, and not all possibilities. I should probably revisit this lab specifically, again, on a later date. 

 

Sunday, March 1, 2015

"To understand recursion, you must first understand recursion!"

As I mentioned in my previous blog post, if one is to understand recursion, they must first understand recursion, but what does that mean? If you were to be literal, it means exactly what it says, to repeat something over and over again. The real challenge that I see people suffer through when it comes to recursion is that: How can a function call itself, inside of itself, over and over again?  Lets go through that shall we?

Lets say, for example, we were supposed to find the sum of all of the elements of a list.

1) [1, 2 ,3] (How would you solve it?)
- By intuition (and probably habit), we would probably use a for loop and accumulate each individual value. If you were able to remember what the python function 'sum' does then you could also use that.

Now lets say your friend comes in and messes with your examples and data (because that's what friends do right?)

2) [[1, 2], [3, 4]] (How about this?)
- Well, why not use nested loops? One for the outer list, and one for the smaller lists? (or again, call 'sum' for the inner lists)

3) [1, [2, 3]] (But what happens with something like this?)
- We can't use the nested loop method so easily with an example like this since we can't iterate over     1. For this to work, we'd need extra code to check if the element in the list is a list or not.

4) [1, [2, [3, 4], 5], 6, [7, 8]] (At this point, you should reconsider who your friends are.)
- With so many lists within lists, and stand-alone numbers, how are you supposed to code this? How many loops will you need?

This is where recursion comes in. Lets break down this list first. What would be the first step in solving this problem?
(Step 1) First, we inspect each element of the outer list to see if it is a list or not.
(Step 2) If an element is not a list, just return it, but if it is a list, we want to go into THAT list to see                 if there are any more lists or non-lists. If there are even MORE lists in THAT list, then we do               it again.
(Step ?) After you stress-fully gone through every single list, you probably want to break those lists                 to make it easier for you. To do that, why not use function 'sum' to get a number that we can               use?
(Step ? + 1) After you broke that list, you're still stuck in a list with no final answer, so, why not call                       sum on this list then? But hey, I'm still in a list, so, call it again!

(But wait...)  Do you see where the 'recursion' of this is now? We're just doing the same thing over and over and over again until we finally get an answer. What you're trying to achieve is:

sum([1, sum([2, sum([3, 4]), 5]), 6, sum([7, 8])]) => sum([1. sum([2, 7, 5]), 6, 15]) 
=> sum([1, 14, 6, 15]) => 36

I know what you're thinking of right now: "But wait, person who writes this blog, I can just do this with nested loops!" Yes, you could but what if you had no idea what the data looked like or your friend comes back and changes it even more, then you would need to adjust the coding for each situation, and lets be frank, us programmers, we don't want to rewrite code over and over again. It's tiring!

Here is a recursive function that can do everything your nested loops can do:

def total(L):
    """ -insert proper docstring here- """
    if isinstance(L, list):  # if it is a list....
        return sum(total(i) for i in L)   # call sum on the entire list, while calling total(i) on every
                                                         # item in L, then calling total(i) again on any more inner lists
                                                         # and so on....
    else: # if L is not a list, return the value
        return L

This here is a recursive function. As you can see, this function does everything! It checks if elements are lists, and if they are, call sum on them, then do the same for all inner lists. If they aren't, just return the value. If you have any doubts, just try it yourself.

This is the power of recursion, but only one example to express it. Recursion can do a lot more, for example: A function that returns the depth of a list (How many layers of inner lists do you have to go through) (or, a quantifiable number to show how badly your friend wants to mess with you). a function to return the longest list, and lots more. It even covers things like ADTs which I won't get into until a later date.

I hope you've learned something today about recursion, and remember:

To understand recursion, you must first understand recursion!





Sunday, February 15, 2015

A month and a half, OOP.

It's been a month and a half since this term started and, really, university life moves fast. The past week we've been talking about the green good stuff. Yes, I'm talking about the ADT Trees. At this moment, I still can't wrap my ahead around how the tree looks like in actual data(lists), instead of the visualization of a tree, with nodes and leafs. I hope I will figure this out during lecture.

Now, time for some more good stuff, Object Orientated Programming. Back in CSC108 and even back in high school, Most of the stuff I did that was mostly with lines of code to make a 'program'. For example, functions or having an entire program in the __main__. The one time I had some experience with OOP was when we used visual basic, since literally, everything was an 'object'. As well, in 108, most of the course was around building functions with the final 2 - 3 weeks being about classes and methods. As I said in one of my earlier posts, I said that we never went that in-depth into classes in 108, then suddenly in 148, CLASSES, SUBCLASS. It was definitely a good thing since having a whole program in __main__ would make debugging and understanding a nightmare. We've learn to initialize and create a class properly and in the most wrong-est way possible. We learnt what a method is, in general, a function that is within a class. Some methods are a bit hard to create depending on the class (ex. repr and str). We learnt about inheritance with classes, as in subclasses, then there's subclasses of subclasses. I mean we did have an entire assignment based on subclasses. Other than that, it's been ADTs throughout the weeks that have past.

Sunday, February 8, 2015

To understand recursion, you must understand recursion.

Recursion has so far been fairly understandable. The whole concept of constantly recalling the same method on every element of a list is not something that is 'that' hard to grasp on, what can get confusing is when the information we are trying to get is either non-accumulative in terms of the elements of the list or requires comparing different elements of the list. For example, during the tutorial, the question asked us to write a method that would compare the lengths of each list and inner list and return the longest list. I had a hard time figuring out how to retrieve the lens of the strings without overwriting the number in the next iteration. I think I still have a ways to go. 

On a side note, this week's midterm went pretty well. I felt like I did pretty well, while the only problem I had was remembering what the type contract was when in __eq__, what to assign to 'other'. As for the length of the midterm itself, it took quite a while, mostly because I spent a lot of time rewriting the name of classes when called but i wouldn't consider it too long. Others said they were pressed for time. 

As for this week, we did more ADT in tutorial. It didn't take long to understand even though we only had a fraction of a lecture to do it. Interestingly enough, queues are basically recursion. What I did question was that once you got to an inner list, and was told to break apart that list and re-add the elements of said list back into the queue, the definition of the puts those elements at the back of the queue, rather than at the front. Is this correct or do we need to change it so that it re-adds at the front? 

Sunday, February 1, 2015

So far down in __main__

Having come from CSC108, I was fairly confident in my programming skills yet I was smacked in the face with classes as soon as CSC148 started. It was a bit puzzling yet I'm not completely surprised as the course description clearly said to take 148 only if you had good programming experience before hand. The first few weeks went by relatively smoothly, mostly being reviewing all the stuff I've forgotten from 108 while building on with sub-classes. We never did 'that' much about classes in 108 so I thought this was useful. Tutorials were helpful as well, just like how we had our weekly Prepares, etc, back in 108. Being forced to constantly work on material helps me keep up with the class because I am in no way considers 'ahead' of anyone else.

Then suddenly at the end of the first week, the first assignment was posted. 'I guess this is university.' The first assignment was a nightmare. I could say that it was partially 108's fault because every assignment was basically: Here are all the methods we want you to make. Here are examples. If you want to add anything, go ahead, now go. The spoon-feeding made it hard to understand what the assignment required of us. What made it worse was that the assignment was left 'general' for us, so we could express our own coding style yet the irony was that there was still specific requirements such as having specific classes and sub-classes and how they 'should' work with one another. It took me more time figuring out what classes we needed and what they were supposed to do. Our in-class Q&As was some-what helpful but it took until the day before it was due to finally receive a proper rundown of what we needed but still, to the end, I could not implement game_view class properly.

Lastly we started recursion which was completely new to me. So far I'm understanding most of the basic examples with the more complex examples being more of a 50/50. I did had a bit of a hard time with the tutorial question since I couldn't figure out how to keep the length of a list without overwriting it with the next iteration.

Saturday, January 24, 2015

'Geeks' needs to learn how to write.

     The difference between 'your average Joe' and a 'geek' is the dedication, the motivation to learning the ins and outs of a certain things. That is also the reason why it is hard for 'your average Joe' to understand 'geeks', because when asked for a explanation, geeks tend to go overboard, explaining all there is to know, when generally, they were looking for a basic answer. I had a experience where I was asked to explain why one of the new Samsung phones were so good. I gave them a rundown on upgrades from the previous generation and new features but once I was finished, they looked at me blankly and responded: 'So, what does that mean?'. It was like they asked for ' 1 + 1 = 2' but instead I gave them the proof of 'Why 1 + 1 = 2'.

     This is why geeks need to learn how to write. We have to be able to communicate all that we know into language other people can understand and read. If we are to convert everyone into being 'one of us' or at least convince them to trust us and think that we aren't weird, we have to come to terms, give up a few things that makes us who we are. We must resist the urge to nerd out, to show off and just, speak normally, be able to express our passion in normal sentences, on paper, for the world to read and see, just how capable we are.

Tuesday, January 20, 2015

Week 3. 01/20/15: Why?

Assignment 1 has started not so long ago and I am still trying to decode these paragraphs. I have general ideas of small bits of different parts of the program but can't seem to bring it all together. Suddenly, I get an email in my mailbox regarding this 'SLOG' and questioning why was this not emphasized more in class. It could be because class is tomorrow. Now I'm wondering why we need to do this. I guess I'll find out tomorrow.