Using the regular expression grader

Using the regular expression grader

por Richard Lobb -
Número de respuestas: 1

A teacher asked me the following question:

I had this silly question in a tutorial for absolute beginners to get a feeling of the Pascal syntax:

Write a program to print a "visiting card" similar to this:

  25/D, Gamanarambha Veediya
  Kosmadalawa 12450
    • The output has four lines
    • Lines 1 and 4: any number of hyphens
    • Lines 2 and 3: any text, can't be empty
So the "answer" could be,
program printVisitingCard;
  WriteLn(' Gemunu Jayawarsiri');
  WriteLn('  24B, Talawatugoda Road');   
So the output can not be taken literally, only the pattern should match. What is the way of doing this?
My response
It's not a silly question at all. Teachers learning to use CodeRunner often stumble over this sort of thing, as they're used to asking questions in which the output requirements are rather vaguely specified.

You can't use the default 'exact match' grader on questions of this sort. You must either use a regular expression grader or write your own custom grader that intercepts the output and prints helpful messages like "I expected 4 lines but got only 3". Custom graders are a lot more work and are not usually worth the effort except when you are creating a new question type that you wish to use repeatedly.

To specify the expected answer with a regular expression you must click the Customise checkbox and then select Regular Expression from the Grading dropdown. The enter the regular expression you want into the test case's Expected field. The in-line help explains a bit about the required syntax.

In the example above, a possible regular expression is:


Here, \A matches the start of the output and \Z matches the end. Note that there is no newline at the end of the expected output because CodeRunner strips all trailing white space from the output, as well as any trailing white space on each line.

BUT: I prefer to avoid questions that require a regular expression grader, as the students don't understand the result table "Expected" column, and the Show Differences button isn't available. It's nearly always easier to ask students to write programs that generate precisely-specified output. That might seem overly fussy, but programming is like that. The compilers/interpreters are fussy about requiring precise syntax in the program, so why not be equally fussy about requiring exact output?

Hiding the regular expression

As a final comment, if you do wish to use regular expression grading but don't want the students to see the regular expression you're using, you can hide the Expected column from the result table by setting a custom value for the Result columns field in the (customised) question authoring form. That means you won't confuse the students by showing them the regular expression. Instead, they will be confused when they get marked wrong but don't know why!

However, with small classes, and a teacher on hand to help, that might be quite acceptable.

The default value of the Result columns field is

[["Test", "testcode"],["Input", "stdin"], ["Expected", "expected"], ["Got", "got"]]

To hide the Expected column, set the field to

[["Test", "testcode"],["Input", "stdin"], ["Got", "got"]]

Note that empty columns are not displayed in the result table, so in fact only the Got column would appear in the above question, which would look like this:


En respuesta a Richard Lobb

Re: Using the regular expression grader

por Visvanath Ratnaweera -
Hi Richard, appreciate your clear explanation. I'm glad that CodeRunner supports pattern matching of the output.

@others, if you wonder where these exotic names come from, the question arose in a webinar Richard gave the day before to the people at