Skip to content

Parson’s Problems

Learning programming is tough. I teach on COMP1202, Programming 1 for computer science and we are always looking for ways to smooth out the learning experience. The course assumes absolutely no programming knowledge and is taught using lectures, practical classes (called labs) and optional tutorial sessions. Having designed the labs for several different introductory programming courses there is recurring problem. The problem of week 1. At this point in the course if you are lucky students will have had 2 lectures and much of the first one will have been a “house keeping” style introduction. This makes writing a lab hugely challenging. You could just skip a lab in week one but the course has a practical focus so we want students getting there hands dirty as quickly as possible. Something we will be exploring this year in lab 1 is introducing programming using Parson’s Problems.

A Parson’s Problem is essentially a code rearrangement task. You give students all the code they need to solve the problem but you jumble up the lines. The program can be fairly simple (does not even have to use iteration) but still reinforces the importance of syntactic structure of the programming language. It also forces students to think about the algorithmic steps of solving the problem without having to worry about getting every tiny bit of syntax correct. This makes the learning outcomes of the exercise quite high level which is hard to do in week one.

The problem I found is that there are very few examples of Parson’s Problems floating around on the web. I concluded that the best way to make one was to write a simple program which does what you want and then jumble it up. There is an added subtly that white space at the beginning of the line gives away what nesting level in the code the line should appear so, to make it a trickier, leading white space is removed. This job was now a big enough task that it was worth writing a program to solve it. I chose perl because its a string manipulation task but you could have written it in anything. The following can be run on any source code file to turn it into a Parsons problem. For teaching I recommend only do this with simple programs since it gets tricky very fast. I amused myself by testing the program by running it on itself to create a Parson’s problem which once solved creates Parson’s problems.

use List::Util qw(shuffle);

if(scalar @ARGV > 1)
    print "Usage: ";

@lines = <$fh>;

@output = shuffle(@lines);

foreach $line (@output)
    $line =~ s/^\s*//;
    if(length $line > 0)
        print $line, "\n";

Posted in Perl, Programming, Training.

4 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Peter Sefton says

    You’re not trying to teach the poor dears Perl are you? That might explain why it’s kind of hard.

    • Patrick McSweeney says

      Haha, no :-P, I am teaching Java but I reshuffle my Java programs in to Parson’s problems using perl. perl is the best language, its too good for students.

  2. Michel BIllaud says

    Well, a Unix one-liner
    sed -e ‘s/^[[:space:]]*//g’ -e ‘/^$/d’ | sort -R
    is worth 20 lines of Perl

    • Christopher Gutteridge says

      Well, if we’re playing that game…

      perl -e’use List::Util qw(shuffle);for(shuffle(<>)){s/^\s+//;print;}’

      If the line is empty then the white-space stripping regexp will remove the \n too.

Some HTML is OK

or, reply to this post via trackback.