WeBWorK Problems

How to check 3 elements out of a list

How to check 3 elements out of a list

by Siman Wong -
Number of replies: 1
Hi,

I'd like to ask students to enter THREE different answers from a list (of 10 or more integers). What is the best/simplest way to implement this?

I had previously asked a related question, about entering two different answers to an equation

http://webwork.maa.org/moodle/mod/forum/discuss.php?d=2508

but that does not seem to be applicable here.

THANKS!

In reply to Siman Wong

Re: How to check 3 elements out of a list

by Gavin LaRose -
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;
  }
} ) );