Saturday, 8 February 2014

Week 5

Throughout this week, we have been dealing with deeper recursion problems as well as learning about the hierarchy of name and method searches. I also began working on Assignment 1 in the beginning of the week, and finally finished it a few days ago.

Assignment 1 deals a lot with class definitions and methods, as well as recursion. When I first looked at the code for GUIController.py, ConsoleController.py and TOAHModel.py, I was startled. I did not understand most of the code in GUIController.py and was really confused as to how to start the assignment. However, after following the first two steps in the assignment handout, I created the method header and code for each required method, and was pleasantly surprised when I ran GUIController.py to play the game manually and it worked! On the other hand, I thought ConsoleController.py was particularly straightforward and finished it quite quickly.

The challenge, however, lied in Tour.py, where we were expected to write the recursive function that would move a tower of cheeses from the first stool to the last stool with the highest possible efficiency. I found this particular step in the assignment especially hard because I could not find a way to optimize the choice of i. After a lot of brainstorming, writing equations on pieces of paper and searching to understand the problem, I believe I derived to right function for finding i, which seemed to work for all numbers. From then on, I finished the assignment fairly efficiently and was extremely excited when everything worked!

The lab this week dealt even more with recursion problems. We were expected to write tons of recursive functions, and then modify them further until they were very efficient. I was actually surprised to find out that I understand recursion pretty well. By tracing each step on paper first, writing the code seemed particularly straightforward afterwards. My partner and I were able to finish all functions and we were even able to work on the bonus exercises.

During lecture, we learned about the hierarchy of searching for names and methods in Python. When a Pythonista refers to a name on Python, the program first looks in the innermost scope, called the local scope; this scope usually consists of the function body. If it does not find such a name, it goes on to look at the enclosing scopes, called nonlocal; these usually occur when the referral to the name occurs in a function within other functions. Next, Python looks for the name in the current module __main__, referred to as global. Finally, if it does not find the name in any of the previous places, it goes on to see if there are any built-in names with the same name. If it does not find the name in any of these locations, an error is generated.

When a method is called, the process is similar to that of searching for names. First, Python looks in the nearest subclass and then it works its way upwards until it finds a method with such a name. Similarly, if Python fails to find this method, it generates an error.

No comments:

Post a Comment