WeBWorK Problems

How to remove repeated entries from a list

How to remove repeated entries from a list

by Tim Alderson -
Number of replies: 3
In the following code, the correct answer will always be listed twice within the multiple choice options. (I am sure student's wouldn't mind this:-)

Is there a way of including the "set" of all (distinct) elements in the list $L3 as options for the multiple choice, without repeats? 

Any help appreciated.
Thank you.

DOCUMENT();
loadMacros(
"PGstandard.pl",
"PGchoicemacros.pl",
 "MathObjects.pl",
"contextFraction.pl",
);

TEXT(beginproblem());

Context("Numeric");
$a = random(0,5,1);

Context("Fraction-NoDecimals");
Context()->flags->set(
     reduceConstants => 0,
     reduceConstantFunctions => 0,
   );
$a1 = Formula("(1+sqrt(3))/2");
$a2 = Formula("sqrt(2)");
$a3 = $a1;
$a4 = Formula("(sqrt(3)-1)/2");
$a5 = Formula("0");
$a6 = Formula("(1-sqrt(3))/2");

@L1 = (30,45,60,120, 135, 150,);
@L2 = (150,135,120,60, 45, 30);
@L3 = ($a1,$a2,$a3,$a4, $a5, $a6);


Context()->texStrings;
$radio=new_multiple_choice();
$radio->qa("\( \displaystyle \cos($L1[$a]^\circ)+ \sin($L2[$a]^\circ) =\) $BR ", "\(\displaystyle  $L3[$a] \)$BR");
$radio->extra("\(\displaystyle $L3[0]\) $BR", "\(\displaystyle $L3[1] \)$BR","\(\displaystyle $L3[3]\)$BR","\(\displaystyle $L3[4]\)$BR","\(\displaystyle $L3[5]\)$BR");
$radio->makeLast("None of the above.");

BEGIN_TEXT
$BR
\{$radio->print_q()\}
\{$radio->print_a()\}
END_TEXT
Context()->normalStrings;
ANS(radio_cmp($radio->correct_ans()));

BEGIN_SOLUTION
$PAR Solution $PAR
END_SOLUTION

COMMENT('MathObject version');
ENDDOCUMENT();
In reply to Tim Alderson

Re: How to remove repeated entries from a list

by Christopher Heckman -
(I am sure student's wouldn't mind thissmile

I am actually of the opposite mind; I think they will get annoyed. If the question is

1.  2+2 =
a. 4
b. 3
c. 4
d. 5

what if they select (a) and are told their answer is wrong, because the correct answer is (c)? I know I'd be upset.

It's also bad design because a might not be 2.

You can redesign your problem along the following lines:

$a = random (0,4,1);
$d = random ([30, 60]);

Context("Fraction-NoDecimals");
Context()->flags->set(
     reduceConstants => 0,
     reduceConstantFunctions => 0,
   );
$a1 = Formula("(1+sqrt(3))/2");
$a2 = Formula("sqrt(2)");
$a4 = Formula("(sqrt(3)-1)/2");
$a5 = Formula("0");
$a6 = Formula("(1-sqrt(3))/2");

@L1 = ($d, 45, 120, 135, 150);
@L2 = (180 - $d,135,120,60, 45, 30);
@L3 = ($a1, $a2, $a4, $a5, $a6);

@wrong = ("") x 5;
for ($i = 0; $i < 5; $i++) { $wrong[$i] = "\(\displaystyle $L3[$i]\)$BR"; }
splice (@wrong, $a);
$radio->extra (@wrong);

In reply to Christopher Heckman

Re: How to remove repeated entries from a list

by Tim Alderson -
Thank you for your prompt response.

I will try your suggested approach tomorrow. I certainly would not have come up with that solution myself.  Thank you very much. 

(BTW My comment regarding students not minding was intended to be facetious.)  
In reply to Tim Alderson

Re: How to remove repeated entries from a list

by Tim Alderson -
Update: I also found ``grep" to be useful:

$radio=new_multiple_choice();
$radio->qa(" ", "\( $L3[$a] \)$BR");

#the following removes the correct answer from the array L3
@L3 = grep { $_ != $L3[$a] } @L3; 

# Now L3 has only 4 entries. These are the remaining choices.
$radio->extra ("\(\displaystyle $L3[0]\) $BR", "\(\displaystyle $L3[1] \)$BR", "\(\displaystyle $L3[2] \)$BR","\(\displaystyle $L3[3]\)$BR");

#And for good measure...
$radio->makeLast("None of the above.");