Python is a powerful language that we can program toward objects. We have used objects since we started using python, such as list, dictionary, string. They all have built-in methods that were properly loaded inside the object and we can use them intuitively, for example, when we want to find the first item in a list l, we use l[0]. Also, we can print a list [1, 2, 3] and we can get [1, 2, 3] as we desired to see.
Moreover, we can build our own class. It is fairly intuitive because what we deal with all day could be abstracted into a class. For example, the roster, registry system, cashier system and even games! There are lot of built-in method we can use to make our own class be used in a way we used for other basic objects such as list, string. They are __str__, __repr__, __contains__, __eq__, etc.
We can also define our own methods to fit the class we are about to write. For point class, it can have method to calculate the distance from the origin. For roster, it can have method to report the people by some information we are interested in. For games, we can have methods to keep track of the process of this game and return some useful information or execute the move to lead us to the next state. It's all open ended.
Sunday, February 15, 2015
Friday, February 6, 2015
week 5 RPN calculator
This week we had a test. But before the test I get to do some additional exercise from lab, it was about RPN calculator. And I found out that this week's lab was also about stack ADT.
These types of ADT such as stack and queue sometimes are handy in dealing with real situations. So about RPN calculator algorithm, basically we deal with a list of symbols that has been well placed(or we need to put them in some order ourselves.) such that we construct a stack instance to operate this list in certain rule, in this case we do a binary operation whenever we encountered an operator, and we pop out the last two items, push in the result after the operation.
This type of calculation may seem not too intuitive to human but it is actually a great way of thinking about how computer works(kind of like when we deal with recursion, the functions we call were stored in a stack.) And this is a simple version of RPN calculator that can explain an idea how it works with the help of stack.
These types of ADT such as stack and queue sometimes are handy in dealing with real situations. So about RPN calculator algorithm, basically we deal with a list of symbols that has been well placed(or we need to put them in some order ourselves.) such that we construct a stack instance to operate this list in certain rule, in this case we do a binary operation whenever we encountered an operator, and we pop out the last two items, push in the result after the operation.
This type of calculation may seem not too intuitive to human but it is actually a great way of thinking about how computer works(kind of like when we deal with recursion, the functions we call were stored in a stack.) And this is a simple version of RPN calculator that can explain an idea how it works with the help of stack.
Subscribe to:
Posts (Atom)