Lecture
So this week
was just a bit more on recursion. We learnt a bit more on how recursion works
and how to trace through recursion functions step by step. Tracing isn’t one of
my strongest points so it took a few examples before I got the hang of tracing
through recursion code, but it was definitely challenging in a good way and an
interesting learning process!
One of our
tracing examples was:
def rec_max(L):
"""
Return the maximum number in possibly nested
list of numbers.
>>> rec_max([17, 21, 0])
21
>>> rec_max([17, [21, 24], 0])
24
>>> rec_max([17, [21, 24], [18, 37,
16], 0])
37
"""
return max([rec_max(x) if isinstance(x, list)
else x for x in L])
The point I
started understanding how the tracing for recursion worked was when I figured
out when exactly I had to start using the base case and then work backwards
from it. It was frustrating at first, but it worked out in the end! Another
useful technique was learning when I needed to just plug in values so I wasn’t
tracing too much.
We also got
a few examples with the Python visualizer, which definitely helped when it came
to tracing through the recursion and understanding the step-by-step process of
such functions. We even got a bit of help on the move_cheeses function for our
Tower of Hanoi assignment which definitely helped as both a recursion example,
and understanding how to make the code in our assignment more efficient.
Lastly, we
also went through a few unittest examples so we had a better understanding on
how it worked and how to use it.
Lab 4
I probably
learnt the most in this lab than I have in any other lab, since this time we
actually got to practice using recursion for the first time! Our first two
exercises were tracing through recursion code with pencil and paper, which is pretty
useful since our exam will be the same way. Also we had to make sure we were
following one of the programming rules: being lazy! While tracing through code,
we had to try and plug in values we’d worked out earlier to avoid tracing too
much, a pretty useful trick in the Python programming world!
The rest of
our exercises focused more on actually writing our own recursive functions,
which was a bit daunting at first since I’d never done it before. Luckily,
after figuring out what our base-case was, my partner and I found putting the
rest of our function in a lot easier than we thought it would be. So I
definitely learnt a lot from this week’s lab!
Assignment 1
Now for an
assignment update! My partner and I are making pretty good progress with it so
far! We’re almost completely done with step 4 of our Towers of Hanoi
assignment. We’ve finished our TOAHModel.py file and it works great with
GUIController (It’s actually pretty fun to play). The TOAHModel file seemed
pretty challenging to finish at first, but as we went through it and the code
got more familiar then it gradually got easier. Hopefully ConsoleController isn’t
much harder, but since we’ve got most of our code in, it shouldn’t be too
difficult.
This assignment is definitely a lot more interesting than 108
assignments since it involves trying to work with different files and making
sure they work together, and we have to write our own functions instead of them
being half-done for us. Since it’s a game, it’s also a lot more fun!
That’s all
for this week!
No comments:
Post a Comment