WeBWorK Problems

Changing the student answer to upper case when using MultiAnswer

Changing the student answer to upper case when using MultiAnswer

by Cindy Loten -
Number of replies: 2

For the sake of answer checking, I used the command uc() perl command to convert the student answer array to upper case letters.  However, if lower case letters are entered, lower case letters are shown the preview table, see below


I would like to change the formatting of the student answer so that even if they enter lower case letters, upper case letters are show in the Answer Preview column (Entered column also would be nice). 

With the parserMultiAnswer.pl , I know can feed a sprintf-style string to format => string and tex_format => string, but I can't find command for sprintf that affects the case of the string (I'm new at this).  I've pasted my code below.

##DESCRIPTION

## Give an example of a derangement.

##ENDDESCRIPTION

##KEYWORDS('counting', 'inclusion exclusion', 'derangements')

## DBsubject('Combinatorics')

## DBchapter('Counting')

## DBsection('Inclusion/exclusion')

## Level('2')

## Date('01/30/2023')

## Author('Cynthia Loten')

## Institution('University of the Fraser Valley')

## TitleText1('Discrete and Combinatorial Mathematics')

## EditionText1('5')

## AuthorText1('Grimaldi')

## Section1('8.3')

########################################################################

DOCUMENT();

loadMacros(

"PGstandard.pl", # Standard macros for PG language

"MathObjects.pl",

"PGML.pl",

"PGchoicemacros.pl", # NchooseK defined here

"parserMultiAnswer.pl",

"PGcourse.pl", # Customization file for the course

);

# Print problem number and point value (weight) for the problem

TEXT(beginproblem());

# Show which answers are correct and which ones are incorrect, set to 0 for no information

$showPartialCorrectAnswers = 1;

##############################################################

#

# Setup

#

#

Context("Numeric");

@letters = ('A'..'Z');

$num_letters = 6; #randomize later??

@slice = NchooseK($#letters,$num_letters);

@my_letters = @letters[@slice];

$my_letters_string = join "", @my_letters;

foreach my $k (0..$num_letters-1) {

Context()->strings->add($my_letters[$k] =>{});

};

#################### question 1

@slice_der = NchooseK($num_letters,$num_letters);

@derangement = @my_letters[@slice_der];

foreach my $i (0..$num_letters-2) {

if ($derangement[$i] eq $my_letters[$i] ) { #swap with letter on the right

my $temp = $derangement[$i];

$derangement[$i] =$derangement[$i+1] ;

$derangement[$i+1] = $temp;

};

};

if ($derangement[-1] eq $my_letters[-1]) {

my $temp = $derangement[-1];

$derangement[-1] =$derangement[0] ;

$derangement[0] = $temp;

};

$der_string = join "", @derangement;

$multi_ans = MultiAnswer (@derangement) -> with (

singleResult => 1, ## entered answers on the same line in view table

separator => ' ' , tex_separator => '\hbox { }', ## text between two answers in view table

checker => sub{

my $correct= shift; my $student = shift;

@cor_der = @{$correct};

@stu_der = @{$student};

foreach my $i (0..$num_letters-1) {

$stu_der[$i] = uc($stu_der[$i]); # convert student answer to upper case if not

}

$stu_der_string = join "",@stu_der;

Value::Error("Repeated letters detected") if ($stu_der_string =~ /([a-zA-Z])~~1/ );

foreach my $i (0..$num_letters-1) {

if ($my_letters[$i] eq $stu_der[$i]) {

$multi_ans -> setMessage($i+1, "$stu_der[$i] in original spot");

return 0;

};

};

return 1;

},

);

##############################################################

#

# Text

#

#

BEGIN_PGML

All questions are about the following ordering of letters:[:quad :] [$my_letters_string]

a. Give a derangement of the original ordering. Enter one letter in each of the blanks.

[_]{$multi_ans} [_]{$multi_ans} [_]{$multi_ans} [_]{$multi_ans} [_]{$multi_ans} [_]{$multi_ans}

a. Removed

a. Removed

END_PGML

##############################################################

#

# Hints

#

#

$showHint = 2; # default is 2?

BEGIN_PGML

If you don't get this in [$showHint] tries, you can get a hint.

END_PGML

BEGIN_PGML_HINT

A derangement is a particular type of arrangement (permutation). Check your definitions!

END_PGML_HINT

##############################################################

#

# Solutions

#

#

BEGIN_PGML_SOLUTION

A derangement is a permutation where no object is in its original spot.

There is more than one correct answer for all 3 questions.

END_PGML_SOLUTION

ENDDOCUMENT();



 



In reply to Cindy Loten

Re: Changing the student answer to upper case when using MultiAnswer

by Glenn Rice -
With the problem as you have it if the letters that are given are entered they are upper case in the answer. Both in the "Entered" answer and in the "Answer Preview". You have made all of the given letters strings in the context, and strings are displayed upper case by default.

Are you wanting letters that are not part of the original letters in the problem to also appear in upper case? One way of obtaining that is to make all upper case letters strings in the context, not just the letters randomly chosen to be part of the answer. Then you need to also check that the letters the student enters are among the correct letters as those will be passed into the checker routine. I have attached a version of your problem that does this.

Note that my attached version also does not use NchooseK, and so does not need the PGchoicemacros.pl macro.  I also removed your conversion of the student answers to upper case.  Since the answers are MathObject strings, they are already converted to upper case. There are also a few other tweaks.

In reply to Glenn Rice

Re: Changing the student answer to upper case when using MultiAnswer

by Cindy Loten -

I was trying to make the question so that students could enter their answers in lower case or upper case, but still have the preview show upper case -- more to learn how to do this than anything.  As shown in the screenshot, when I entered lower case letters, lower case letters were shown in the Entered and Answer Preview columns (we have webwork 2.15 if it matters??).  I noticed in some graph theory questions that students could use lower case or upper case letters when entering vertices or edges for their answers, but that everything displayed as upper case in the the preview table.  MultiAnswer wasn't used for these questions.  The authors made their own custom answer checkers.


I noticed that in your code the student gets an error message for using lower case letters -- I think I like this approach better.  Thanks for sharing your code with the improvements, especially the code that avoids using NchooseK for randomly selecting letters from an array.  I"ll get a lot of use out of that!