Last week we learned about the built-in python function property. I didn't really know how to use this function until I asked for advice on Piazza. But even after figuring out I wasn't really sure what the point of this function was because up until now we've been assigning values as self.x = x as soon as we called our class. But I soon realized that you can add if statements to prevent future users of your code to input invalid values. The example Danny used was:
symbol = property(get_symbol, set_symbol, None, None)
def set_symbol(self: 'RegexTreeNode', s: str) -> None:
"""set private symbol"""
if not s in '01e|.*':
raise Exception('Invalid symbol: {}'.format(s))
else:
self._symbol = s
The highlighted part in set_symbol made it so that if the someone was to pass a value other than 01e|.* as the parameter then an error would be raised. This could be one of the pitfalls in A2, even though it was not specified that we had to take such cases into consideration.
After reading the slogs of my classmates, it seems like others are also having trouble understanding how to use the property function and what it should be used for.
I look forward to using this function in my future code.
Monday, 25 November 2013
Wednesday, 13 November 2013
More sorting adventures
This past week we continue to learn about various types of sorting. Reading over the slog entries of classmates, it seems that most people have grasped this concept, especially those taking csc165 this semester, as we are also learning this topic in that class. I feel that the algorithms that have an average run-time of O(n^2) are generally easier to understand. I found that in most cases O(n^2) algorithms tend to use for loops inside for loops, as in selection sort. The algorithms that have an average run-time of O(n log n) are a little harder to understand. Some examples are merge sort and quick sort. The general idea of theses O(n log n) algorithms are to divide the array into smaller arrays and those arrays into even smaller arrays.These types of algorithms make use of recursion to sort the array.
One problem I have is understanding how a recursive algorithm would have a run-time of O(n log n). Another question I have is why sorting recursively would result in a shorter run-time compared to something like O(n^2) run-time. If any classmates reading this could explain I’d appreciate it!
One problem I have is understanding how a recursive algorithm would have a run-time of O(n log n). Another question I have is why sorting recursively would result in a shorter run-time compared to something like O(n^2) run-time. If any classmates reading this could explain I’d appreciate it!
Sunday, 3 November 2013
Binary Search Trees and Running Time Analysis
One topic we covered this week were the binary search trees. This is a tree where a number designated as the main root would be placed as the starting point of the tree. Then you can insert additional numbers into the tree, and the code would sort it so that numbers less then the parent would be put into the left sub tree and numbers greater would be put into the right sub tree. I enjoy working with binary search trees, especially when there is a __str__() method where we can print a visual representation of the tree.
The other topic we were briefly introduced to was running time analysis. This is the analysis of how fast or slow your function would run. Depending on how you implement the algorithm in your function, the running time may increase or decrease. It may increase/decrease constantly, logarithmically, linearly, quadratically, exponentially, etc. This is a very interesting topic. Thus far our code would run and return a value immediately but this may not be the case for a larger amount of code. Also, we as students may not care as much about the running time of our programs but it would be very important to programmers of commercial software.
After reading the slogs of my classmates, it seems that most people have a grasp on these concepts.
The other topic we were briefly introduced to was running time analysis. This is the analysis of how fast or slow your function would run. Depending on how you implement the algorithm in your function, the running time may increase or decrease. It may increase/decrease constantly, logarithmically, linearly, quadratically, exponentially, etc. This is a very interesting topic. Thus far our code would run and return a value immediately but this may not be the case for a larger amount of code. Also, we as students may not care as much about the running time of our programs but it would be very important to programmers of commercial software.
After reading the slogs of my classmates, it seems that most people have a grasp on these concepts.
Subscribe to:
Posts (Atom)