edX 6.00x week 3 – recursion, objects, polynomials and hangman!

Ah – the joys of recursive code!

The first of this week’s lecture sequences covered this very well and in some detail. All of the old favourites were there – calculating powers, greatest common divisors, Fibonacci numbers and the Towers of Hanoi. The second lecture sequence was on objects, with one of the points being made being that Python functions are also first class objects, making it simple to call a function with a function as an argument.

Both are really useful things to be able to do in a programming language – even if the dialects of FORTRAN that I grew up with at PAFEC in the 1980s didn’t permit recursion. Indeed, an unintended piece of recursive FORTRAN hidden away in DOGS Mapping nearly got me “held hostage” at a customer site in Rotterdam at one point early on in my career – but that story would take too long to tell here!

This week’s problem set (PSET3) consisted of writing code to manipulate polynomial expressions and more excitingly (but only just), writing a simple ‘Hangman’ game. I found both exercises to be relatively straightforward, although there’s one part of my Hangman code which calculates game success or failure that I’m not happy with as it’s inelegant, even though the code works. Somehow, I think I should only need to make a call to my isWordGuessed function inside the while loop – rather than having to call it a final time outside the loop to print out the success/failure message…

(wait a minute … hack, hack, hack – a-ha!)

Simply writing that sentence ended up giving me insight – I was absolutely right and the grader confirms it! I should just be testing my ‘mistakesMade’ variable outside of the while loop to determine success or failure instead of making a second call to isWordGuessed. D’Oh.

One of the drawbacks of this course therefore seems to be the automated grader, which is simply checking and awarding points for code that works, rather than providing an incentive for ensuring that the code produced is elegant. Worse, the comments provided by the course team in the hangman code stub caused the grader to mark my completed version as incorrect, as for some reason it couldn’t determine if I’d used the raw_input function as demanded until I’d removed all of them. On the positive side however, the grader does provide very useful feedback against its test cases when you do mess up!

What's your opinion? Let me know by leaving a comment!