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!