To start the week off Prof. Heap helped us out on Assignment 1 by going over the recursive elements of the Towers of Hanoi exercise which involves moving a stack of cheeses from on stool to another, without ever placing a larger cheese on top of a smaller cheese. It was not difficult to understand as the function to perform this have an if and else statement where if the size of the stack of cheese was 1 it would simply move the cheese to the targeted stool, and if it the stack was larger than 1 it would employ the function recursively on the the top cheese and subsequent cheeses under it, by implementing the function with different parameters entered to fulfill an algorithm to reduce the amount of moves used to move all the cheeses. This was used for a Tower of Hanoi problem with 3 stools which is simple when compared to one with 4 stools, which is the type of problem we are supposed to address in Assignment 1, so this was used to help us get a good understanding of recursion for the Tower of Hanoi but in no way did he solve the problem for us.
Next we covered names and namespaces in class, more specifically which order Python looks for them when executing. First and foremost Python looks at the innermost local scope, which is usually in the function body. If you assign a value to a name within a function that has another function within it that changes the values and you run the parent function, python will not change the value of the name because the original value is the local value. If you use the nonlocal statement before assigning a new value in a child function and then run the parent function it will change the value of the name to the one stated after the nonlocal statement as it creates a new local statement within that function. After local and nonlocal names are evaluated global names are evaluated. Even if you use the global statement within a child function like you did with the nonlocal statement it wont change the value of the name until you finish running the parent function as it creates a global or modular name change outside the function. Last but not least built in names that python initializes when a python interpreter is run, such as the isalpha function, and these names are looked for last by python.
Last, we were offered tips on tracing through recursive functions and their doctest examples. We were told to trace the easiest of the doctests and then plug in know results into the next most complex case, and repeat until done. I however prefer to just use the python visualizer provided to us in the software page of the CSC148 homepage as it takes you through every step of the function's execution.
Other than the class work I got started on Assignment 1 this week and am hoping on slowly progressing through it until it is done.
Until next week,
Maxim Isakov
No comments:
Post a Comment