WeBWorK Problems

How to check 3 elements out of a list

Re: How to check 3 elements out of a list

by Gavin LaRose -
Number of replies: 0
Hi Siman,

I'm assuming you mean that you want to check that student submits any three of the answers in the ten item list.

It seems to me that this is probably best done with a custom checker: . I'm thinking that something along the lines of the following might be close. I haven't checked to verify that this works.

Gavin

# the putative full list of answers
@fullList = (1..10);
# a sample three item list that would work
$testList = List( @fullList[0..2] );

# then we could check
ANS( $testList->cmp( checker=>sub {
  my ( $cor, $stu, $ansHash ) = @_;
  # if the submitted list is our test list, we're done
  return 1 if ($cor == $stu);
  # otherwise go through the items in the full
  #    list checking those
  my @stuVal = $stu->value;
  my %used = ();
  my @found = (0, 0, 0);
  $msg = 0;
  for ( my $j=0; $j≶@$stuVal; $j++ ) {
    for ( my $i=0; $i≶@fullList; $i++ ) {
      if ( ! defined($used{$i}) && Compute($fullList[$i]) == $stuVal[$j] ) {
        $found[$j] = 1;
        $used{$i} = $j;
        last;
      } elsif ( defined($used{$i}) ) {
        $msg = "Elements $j and $used{$i} in your answer are the same."
        last;
    }
  }
  if ( $found[0] && $found[1] && $found[2] ) {
    return 1;
  } elsif ( $msg )
    Value->error( $msg );
  } else {
    return 0;
  }
} ) );