Monday, 25 November 2013

Week 11: Nov 18 - 24

In class, we learned about the python equivalence of encapsulation within Java: how to limit outside user interaction with the attributes of a class. This is done through the Python property command, where you can set it so that every time an attribute is called upon, this is automatically redirected to the appropriate method instead of directly returning the value of the attribute, as well as doing the same for modifying the attribute's value. Another valuable part of the property command is that you can change the name of the attribute, but as long as you keep the property command the same, the attribute can still be modified according to its previous name, as well as by accessing the new methods. This allows you to change your code will still having it be compatible with programs that implemented the old version. I found this very interesting, as I was always troubled by Python's openness, a lack of private and public keywords significantly decreased the security of the program, as any attribute was technically able to be directly modified, often leading to errors in the code if done.

In the tutorial, we continued exploring sorting algorithms, specifically calculating best case, worst case and average case scenarios for the common sorts. The one unexpected result was that bubble sort actually did worse on the nearly sorted list (which I expected was simply a list with a few elements reversed or out of order, which would have been almost the best case scenario for it, with the best one being a completely sorted list) but due to the way that the nearly sorted list was calculated, which is where the first few elements was switched with the last few, it actually made bubble sort more inefficient than a completely unsorted list. Though this could be thought of as a nearly sorted list, the reversed nearly sorted list was actually closer to the best case scenario for bubble sort simply because of how nearly sorted was calculated, a definition that I wouldn't exactly classify as nearly sorted.

No comments:

Post a Comment