WeBWorK Problems

CheckboxList with variable correct_choices

CheckboxList with variable correct_choices

by Joseph Pedersen -
Number of replies: 10

I would like to have the correct choices vary, so instead of:

[1,3,5]

I would like to use:

@ans_arr

Is this possible?

I also tried:

(grep { $correct_ans[$_] } (0..5))

but everything that I try throws the error:

A CheckboxList's second argument should be a list of correct choices.

In reply to Joseph Pedersen

Re: CheckboxList with variable correct_choices

by Alex Jordan -

Can you ask this again with a small example .pg problem that demonstrates what you would like to do? I know that it won't be functional, but something that shows your intent. I'm not understanding what you would like to do yet.

In reply to Alex Jordan

Re: CheckboxList with variable correct_choices

by Joseph Pedersen -

In this example, I would like there to be either one or two randomly chosen correct answers, by using @whichAreCorrect for the correct answers, but I get the error 

A CheckboxList's second argument should be a list of correct choices. at line 1 of (eval 4504)

DOCUMENT();


loadMacros('PGstandard.pl', 'PGML.pl', 'parserCheckboxList.pl', 'PGcourse.pl');


# have 1 or 2 incorrect answers

$numberOfIncorrect = random(1,2);


# randomly choose which are incorrect

@whichAreIncorrect = random_subset($numberOfIncorrect, (0,1,2));


# make the array of which are CORRECT, which is so much more complicated in Perl than it is in Python

@whichAreCorrect = (0,1,2);

for $remove (@whichAreIncorrect) {

    @whichAreCorrect = grep { $_ != $remove } @whichAreCorrect;

    }


# the correct answers

@answers = (3,5,8);


# make these answers incorrect by adding one to them

for (@whichAreIncorrect) { $answers[$_] += 1; }


$checks1 = CheckboxList(

    [

        "\(1+2=$answers[0]\)",

        "\(2+3=$answers[1]\)",

        "\(4+4=$answers[2]\)",

        'None of the above'

    ],

    @whichAreCorrect

);

BEGIN_PGML

Choose which equations are correct:

[_]{$checks1}

END_PGML

ENDDOCUMENT();

In reply to Joseph Pedersen

Re: CheckboxList with variable correct_choices

by Glenn Rice -
You need to reference @whichAreCorrect so that it is used as a single array reference argument, instead of the list being passed as multiple arguments.  So it should be

$checks1 = CheckboxList(
    [
        "\(1+2=$answers[0]\)",
        "\(2+3=$answers[1]\)",
        "\(4+4=$answers[2]\)",
        'None of the above'
    ],
    \@whichAreCorrect
);
In reply to Glenn Rice

Re: CheckboxList with variable correct_choices

by Joseph Pedersen -

Thank you for your reply.  I don't really understand the \@ syntax, but I did see that online and tried it.  Even after adding that one character, I still get an error message:

Translator errors

ERRORS from evaluating PG file: A CheckboxList's second argument should be a list of correct choices. at line 1 of (eval 5951) from within Value::Error called at line 236 of [PG]/macros/parsers/parserCheckboxList.pl from within parser::CheckboxList::new called at line 1 of (eval 5951) from within main::CheckboxList called at line 36 of setSE387_AT251_HW5/multi_select.pg

I'm using:

Page generated August 31, 2024, 12:46:37 PM EDT
WeBWorK © 1996-2024 | theme: math4 | ww_version: 2.19 | pg_version 2.19
The WeBWorK Project
In reply to Joseph Pedersen

Re: CheckboxList with variable correct_choices

by Alex Jordan -
In Glenn's code in an actual PG file, it needs to be

~~@whichAreCorrect

@whichAreCorrect is an array. The way you used it, it would expand to a list. It's the same as if you typed out all the entries.

\@whichAreCorrect is a single entity. It is like an address in memory for where the list is stored. This is what must be used for the syntax of this tool.

But PG preprocesses the problem file and temporarily turns \ into \\ for unrelated reasons. At the same time, it turns ~~ into \ so you have a way to get a single backslash when you really need one. So you have to do that here.
In reply to Alex Jordan

Re: CheckboxList with variable correct_choices

by Alex Jordan -

Another way to create an array reference is with brackets around a list. Like:

[ @whichAreCorrect ]

But in this case that is wasteful coding. That creates a new array whose entries are copies from the original. And the bracketed expression is a reference to that array. The new array is anonymous - there is no name for it like @whichAreCorrect. So that works but needlessly duplicates something into memory.

Mentioning this for completeness only, to help understand other things you will see in similar places in PG code examples.

In reply to Alex Jordan

Re: CheckboxList with variable correct_choices

by Joseph Pedersen -

That worked!  Thanks!

So if I understand:

1. I can use an array @some_array the same way that I can use the literal (1,2,3)

2. Using \@some_array is the same as using [1,2,3]

3. That's what I want to use, but PG changes \@some_array to \\some_array

4. So I should use ~~@some_array, which PG changes to \@some_array

Is there a reference where I can look up rules like that?  I've been searching https://webwork.maa.org/wiki/Problem_Authoring_Background_Information and I have found https://webwork.maa.org/mediawiki_new/images/f/f5/PGML-cheatsheet.pdf useful, but I haven't seen this rule about \ or ~~ anywhere.

Or would I just need to study the source code to learn something like that?

In reply to Joseph Pedersen

Re: CheckboxList with variable correct_choices

by Glenn Rice -

Uhh, oops.  Thanks Alex.  I forgot to take translation into account.

In reply to Joseph Pedersen

Re: CheckboxList with variable correct_choices

by Alex Jordan -

That's not 100% right, but close enough.

> 3. That's what I want to use, but PG changes \@some_array to \\some_array

PG changes the text of your PG file from \@some_array to \\@some_array, double backslash and at sign still present. Now it's an actual backslash character (as represented by \\) followed by the array. Which might be broken syntax or might be a string that is not what you intended, depending on the context.

> 2. Using \@some_array is the same as using [1,2,3]

There is a technical difference, although in most cases the effect is the same. \@some_array is a reference (think of that as an address in memory) to the actual array @some_array.  But [ @some_array ] is a reference to a different nameless array that has the same entries in it. And [1,2,3] is similarly a reference to an anonymous array.

So far, everything mentioned is just perl syntax. A PG-specific tutorial on perl would be nice, but it's hard to put in the work for that given there is a lot about perl in general on the internet.

Then the one thing that is specific to PG is translations. These are how PG modifies your problem file text before it actually gets processed. The two translations that matter here are \ to \\ and ~~ to \. I believe these are the only "gotcha" translations. They are happening at the bottom here:

https://github.com/openwebwork/pg/blob/d17c2beddf44d1bdcfabe7c1f6dff8f561dee752/lib/WeBWorK/PG/Translator.pm#L1334

Higher up in that list, you can see things like how "BEGIN_PGML" is translated into an actual perl construction for a multiline text string. Those translations are less mysterious.

The tilde issue is mentioned here:
https://webwork.maa.org/wiki/LaTeX_Tips_And_Traps
But that page is actually mostly information about writing PG in the era before PGML. Other things on that page do not apply if you are using PGML. If there were a fundamentals page about coding PG, it should have this thing about backslashes and tildes in it. Although, it's hard to talk about until the perl basics are understood first.

In wiki resources you were using, does any place seem natural to add an explanation about blackslashes and tildes? We could try to make edits there.

In reply to Alex Jordan

Re: CheckboxList with variable correct_choices

by Joseph Pedersen -

The place I would add them is in the example for multi-select problems.  After having the most basic example for that kind of problem, I would include an example just like the one that I posted here, where the correct answers are code dependent instead of hard coded.

I have only been using WeBWorK for a month now.  I really like it, but I have usually had to do a lot of experimenting to get the problems I've made to work correctly.  I would definitely like to contribute the questions that I've made, and a tutorial showing how to design those questions. I plan on making tutorial videos at some point and posting them on a YouTube channel, but if WeBWorK has any documentation that includes examples, which I'm allowed to contribute to, I will gladly do that.