WeBWorK Problems

Setting up a chemistry problem

Re: Setting up a chemistry problem

by Eric Stroyan -
Number of replies: 0
Works nicely!
I've not played around with it any more than to set up a problem that randomly accesses reactions from a macro file.
As the week progresses I'll try some other things.
I've included the sample macro file and the pg file in-line below.
You certainly have saved me quite a bit of work.
Thanks again!

macro (rexn_rand_chemicalReaction.pl):
%reactions=(
1=>{equation=>"N_{2} + 3H_{2} \longrightarrow 2NH_{3}",
class=>"combination",
reactants=>"N_{2} + 3H_{2}",
products=>"2NH_{3}"},
2=>{equation=>"4P + 5O_{2} \longrightarrow 2P_{2}O_{5}",
class=>"combination",
reactants=>"4P + 5O_{2}",
products=>"2P_{2}O_{5}"},
3=>{equation=>"2C_{2}H_{6} + 7O_{2} \longrightarrow 4CO_{2} + 6H_{2}O",
class=>"combustion",
reactants=>"2C_{2}H_{6} + 7O_{2}",
products=>"4CO_{2} + 6H_{2}O"},
4=>{equation=>"2KClO_{3} \longrightarrow 2KCl + 3O_{2}",
class=>"decomposition",
reactants=>"2KClO_{3}",
products=>"2KCl + 3O_{2}"},
5=>{equation=>"NaHCO_{3} + HCl \longrightarrow NaCl + H_{2}O + CO_{2}",
class=>"neutralization",
reactants=>"NaHCO_{3} + HCl",
products=>"NaCl + H_{2}O + CO_{2}"},
);

Problem.
DOCUMENT();

loadMacros(
"PG.pl",
"PGbasicmacros.pl",
"PGchoicemacros.pl",
"PGanswermacros.pl",
"PGauxiliaryFunctions.pl",
"MathObjects.pl",
"Parser.pl",
"PGstandard.pl",
"rexn_rand_chemicalReaction.pl",
"contextReaction.pl");
Context("Reaction");

$count=keys %reactions;
@items=(1..$count);
$S = list_random(@items);

$reactants = $reactions{$S}{reactants};
$products = $reactions{$S}{products};

$react = Formula($reactants);
$prod = Formula($products);

Context()->texStrings;
BEGIN_TEXT
Predict the products for the following chemical reaction$BR
\($react \longrightarrow\) \{ans_rule(30)\}.
END_TEXT
Context()->normalStrings;

ANS($prod->cmp);
ENDDOCUMENT();