Hello all, and welcome to my post about recursion. Now for those of you who aren't in the know, recursion is simply put, the definition of something by itself. In other words, its to when something uses its own self to define itself. Now although that concept may seem pretty vague and strange, but when it is applied to programming, it leads to some of the most interesting and really effective code you'll be writing (at my current level of programming). When recursion is applied in programming, it basically means that a method or function can call itself during its own execution. Take that in for a minute and really absorb the insanity behind that. You can basically call a function on itself to get to the results you want in a more user friendly way. For example, lets say you want to count the number of occurrences of a number within a list, now that list also contains sub lists, and even those sub lists contain sub lists of sub lists. Now that's a lot of sub lists, you could run the function multiple times on each individual list of sub lists and then add it up, or using recursion, you can write yourself up a function that will not only do the counting of occurrences for you, but also do the count inside of each sub list as well. Now in that specific case, let's say you make your accumulator for the occurrences of n, now for each item within the list L, if item is the number you're looking for, we add to the accumulator and we leave happy, otherwise, if L is an instance of list, then what you do is you call the function or method you are defining on the item and add it to your accumulator, that way it will make its own accumulator, check each item within, if the item is n make its own count, if its item is a list it will go again even deeper. Now like this, you'll be able to go into how ever many lists and sub lists you want, of course you will also need to make sure the function returns the accumulator so that when its called recursively, it has a value to give to its superior's own accumulator. So that is the simple definition of recursion, as well as a simple example of when and how you would implement it. Recursion can also be found at various different levels, including the case of Trees in CS, a tree is a recursion in its own rights, each node branches to two other nodes, that branch till their own nodes, with the exception of the leafs. Writing a Tree, or to display a Tree in python would absolutely require recursion, that way you could check each nodes value, and their children's value until you can't go any lower, and then it would move on to the next node.
So that is the quick rant of the week, about the Inception like recursion, this is something that can be confusing at times but is really effective when you start thinking recursively. As always this is merely my definition and if it is at all wrong I apologize. Thank you for reading.
-Just a CS student
Thursday, 27 February 2014
Tuesday, 4 February 2014
Exceptions
So this post is a little bit late... I had intended on posting it last week, but life gets in the way and other assignments take up more of my time. Anyway, last week we had to do an assignment as well as a lab in which we utilized a wonderful class known as exceptions. Now for those of you reading and who don't know what an exception class is, to put it simply, its errors. An example of an exception class that I think is the easiest to picture, would be the "ZeroDivisionError". This is an exception class that is flagged whenever the function for division encounters a denominator that is equal to zero. Now the really interesting and cool things about the exception class, is that like any other class, you can make your own! Now it probably doesn't seem that cool at first, but, through making your own unique exceptions, you can put in a few new levels of depth to any of the functions that you write. For example, lets say that you make a function that prints a "happy x birthday!" string. Naturally you would just have the user input an x and concatenate it to the string. Let's say somebody tries to mess around and inputs a negative number. Now it is impossible to have a negative age, so you can create your own exception, let's call it "NegativeAgeError", and raise it if x<=0. Now should someone try to input a negative number, not only will it not be accepted, but the user will be given information, in the form of an error (with a message if you so choose to add one, because you can), as to what they did in order to trigger an error. One last point about the exception class that I absolutely love, is that you can make your function work around the errors so that your program doesn't crash. Lets go back to the "ZeroDivisionError" just because its easy and I wish we could divide by zero. Anyway, let's say your function takes in user inputs for a denominator, and applies it to a specific numerator, and let's say the user chooses 0 as his denominator. Normally, this would cause the error to be raised and exit the program, however, if you so please, you can utilise the except command with the "ZeroDivisionError" as its argument in order to make it so that if that error should be triggered, your function ignores it and acts like nothing happened. Personally I love the idea of just pushing problems right under the rug.
Well that's it for what was supposed to be my rant last week on the exception class, which is by far now my favourite class(so far).
- Just a first year CS student.
Well that's it for what was supposed to be my rant last week on the exception class, which is by far now my favourite class(so far).
- Just a first year CS student.
Subscribe to:
Posts (Atom)