- Parses ordered lists of letters like "B > A = C > D"

From WeBWorK

Jump to: navigation

NAME - Parses ordered lists of letters like "B > A = C > D"


This context provides a structured way to parse and check answers that are ordered lists of letters, where the letters are separated by greater-than signs or equal signs. The only operators allowed are > and =, and the only letters allowed are the ones you specify explicitly.

To access the context, you must include


at the top of your problem file, and then specify the Ordering context:


There are two main ways to use the Ordering context. The first is to use the Ordering() command to generate your ordering. This command creates a context in which the proper letters are defined, and returns a MathObject that represents the ordering you have provided. For example,

        $ans = Ordering("B > A > C");


        $ans = Ordering(A => 2, B => 2.5, C => 1);

would both produce the same ordering. The first form gives the ordering as the student must type it, and the second gives the ordering by specifying numeric values for the various letters that induce the resulting order. Note that equality is determined using the default tolerances for the Ordering context. You can change these using commands like the following:

        Context()->flags->set(tolerance => .01, tolType => 'absolute');

If you want to allow lists of orderings, use the Ordering-List context:

        $ans = Ordering("A > B , B = C");

Note that each Ordering() call uses its own copy of the current context. If you need to modify the actual context used, then use the context() method of the resulting object.

The second method of generating orderings is to declare the letters you wish to use explicitly, and then build the Ordering objects using the standard Compute() method:

        $a = Compute("A > B = C");
        $b = Compute("C > D");

Note that in this case, D is still a valid letter that students can enter in response to an answer checker for $a, and similarly for A and B with $b. Note also that both $a and $b use the same context, unlike orderings produced by calls to the Ordering() function. Changes to the current context WILL affect $a and $b.

If the ordering contains duplicate letters (e.g., "A > B > A"), then a warning message will be issued. If not all the letters are used by the student, then that also produces a warning message. The latter can be controlled by the showMissingLetterHints flag to the cmp() method. For example:

        ANS(Ordering("A > B > C")->cmp(showMissingLetterHints => 0));

would prevent the message from being issued if the student submitted just "A > B".