WeBWorK Problems

ordering of inner lists in lists of lists

ordering of inner lists in lists of lists

by Gavin LaRose -
Number of replies: 1

I'm working with the following problem (much simplified).

DOCUMENT()
loadMacros(
"PGstandard.pl",
"MathObjects.pl",
)
Context("Numeric");
$a1 = List( (1,2) );  $a2 = List( (2,3) );
$ans = List( $a1,$a2 );

TEXT(beginproblem());
Context()->texStrings;
BEGIN_TEXT
ans = \{ ans_rule(15) \}
END_TEXT
Context()->normalStrings;

ANS( $ans->cmp() );

ENDDOCUMENT();

In this, the checker allows the lists (1,2) and (2,3) to be entered in either order, but requires that the inner lists (1,2) and (2,3) be entered in the order given.

I'm sure there is an obvious reason why this is, and thus an obvious fix. But I'm not seeing it. How do I allow the inner lists to be entered in any order?

Thanks,
Gavin

In reply to Gavin LaRose

Re: ordering of inner lists in lists of lists

by Davide Cervone -
This problem is occurring because the list answer checker is more complicated than just a == between the two lists. In order to get messages about which entries are correct, and how many more you might need, and such, the list checker handles the unordered lists, whereas == between two lists is a comparison of ordered lists. This is a design decision the has had several undesirable consequences, I'm sorry to say, and so probably is a design flaw.

In any case, in order to do what you are wanting to do, you need to provide your own custom checker. Here is one that gets the job done in your case but may not be completely general (if you were using decimal values, for example this might not be the best approach, but it should be fine for integers).

Note that I also use the entry_type to make a better wording for the entries in error messages ('pair' is better than 'list of numbers' in this case).

ANS($ans->cmp(
  entry_type => 'pair',
  checker => sub {
    my ($correct, $student, $ansh) = @_;
    my $c = List(lex_sort($correct->value));
    my $s = List(lex_sort($student->value));
    return $c == $s;
  }
));