Saturday 29 March 2014

SORTING AND EFFICIENCY

Sorting is a way to arrange a set of data in a specific order. There are different ways by which we could sort data. They all do the same thing but with different approaches. We have also been able to compare these different kinds by timing them in order to obtain their efficiency and to know which one to use regarding the length of data we have to sort. There are different ways by which we could data. Some of the methods we were introduced to are:
1.) Bubble Sort: This method goes over every item in the list and swaps them until the list is in the right order.
2.) Selection Sort: This method continuously selects the smallest remaining item in the list and places it where it belongs.
3.) Insertion Sort: This method divides the list into two part, the other part an empty list. It goes through every item in the list and places them in their respective position in the sorted part.
4.) Merge Sort: This method works by splitting the list into two part and sorting them separately then merging them.
5.) Quick Sort: This method works recursively by sorting the list according to the first item in the list. It checks every item and divides the list into two part, one part being the items that are less than the first item; it repeatedly calls itself for the other parts of the list.
6.) list.sort: This is Python's built-in function for sorting data and is usually the most efficient in most cases.

Some particular methods are more efficient in some cases while some are not. It all depends on the size of the list and the format(sorted, unsorted, reversed). Generally, the built-in function proves to be the efficient in most cases and quick sort is less efficient because it's recursive and as the length of the list increases, the function reaches its maximum recursive.


Sunday 9 February 2014

RECURSIONS

The last lab and also the past few weeks were all about getting to know how recursion works. Recursion is a way of calling a particular function continuously in itself. It works for situations where you need to keep checking the same thing or the same conditions over and over again.

For example if you need to go through every element in a nested list or search list. It is basically a way to avoid repetition of code and so you just call the function in itself. This recursion method was useful in assignment one where we needed to call the move functions repeatedly.

 In the previous lab, we had to create a function that finds a particular number in a search list. This meant we had to check different conditions for each list in the search list and recursion was a very useful method to use. We were also taught how to use recursion in relation to binary numbers. I actually never thought it could be possible to call a function within itself. It took be quite a while and a lot of tracing to fully grab the concept.

Monday 27 January 2014

LABS AND EXCEPTIONS



The first lab was really fun, we got partnered up with random people according to our birthday dates and it was really cool working together with someone. It also refreshed my memories of programming with python and got me into the programming spirit once more.

In the second lab, we were required to write classes Stack and Queue which turned out to be very useful because it helped me understand better how they work and it will be useful for assignment one (I haven't even started yet).

The previous classes and exercise two were based on Exceptions. Exceptions are basically ways in which we could handle errors in order to make our program keep running instead of crashing. You could also put in short messages to go with the exception which could be pretty useful in certain situations. So, exercise two was basically to familiarize us with the idea of exceptions and how they work.

Tuesday 21 January 2014

OBJECT ORIENTED PROGRAMMING

         In the past two weeks, we've have been looking at programming with Python from a different angle. We were introduced to something called Object Oriented Programming. This (OOP) is a method of programming that is done by creating objects that relate to the real world.

CLASS
Classes are used to create objects and also methods. Classes have this structure:

def NameOfClass:
     def __init__(self, a, b, c):

I don't fully understand all the concepts here but from what I've learned every class needs to be initialized with the 'init' method above. Also, the first parameter should always be self; self is used to make reference to the new object that was created. The letters a, b and c above are called attributes. There are also method which are functions created under the class that also relates to the way the class functions in the real world.

We also talked about Stacks. I find this concept very interesting. We can add items to the top of the stack and can only remove the last item that was added. It was a little vague at first until I attended the lab this week. Things got a little clearer and I also had fun brainstorming and I began to really understand how a stack works.

This has really gotten me excited for the next few weeks of class. Programming to me is a little technical but fun and definitely worth it in the end.