[LON-CAPA-cvs] cvs: modules /gerd/texas THconvert.pl texas.library /gerd/texas/samples LonCapa01.txt LonCapa02.txt LonCapa03.txt

www lon-capa-cvs@mail.lon-capa.org
Wed, 15 Nov 2006 22:09:56 -0000


www		Wed Nov 15 17:09:56 2006 EDT

  Added files:                 
    /modules/gerd/texas	THconvert.pl texas.library 
    /modules/gerd/texas/samples	LonCapa01.txt LonCapa02.txt 
                               	LonCapa03.txt 
  Log:
  Texas Homework Service
  
  

Index: modules/gerd/texas/THconvert.pl
+++ modules/gerd/texas/THconvert.pl
use strict;
# read file from command line
my $filename=shift;
open(IN,$filename);
my $filecontent='';
while (my $line=<IN>) {
    $filecontent.=$line;
}
close(IN);
# deal with all kids of line endings
$filecontent=~s/[\n\r\l]+/\n/gs;
# read the file by sections
my $current='';
my $readsofar='';
my %content=();
my $multipart=0;
my $keywords='';
foreach my $line (split(/\n/,$filecontent)) {
    chomp($line);
    if ($line=~/^\s*quest\s+(\d+)/) {
	if ($current) { $content{$current}=$readsofar; }
        $readsofar='';
	$current='question_'.$1;
	if ($1>1) { $multipart=1; }
    } elsif ($line=~/^\s*expl\s+(\d+)/) {
	if ($current) { $content{$current}=$readsofar; }
        $readsofar='';
	$current='explanation_'.$1;
    } elsif ($line=~/^\s*void answer/) {
	if ($current) { $content{$current}=$readsofar; }
        $readsofar='';
	$current='script';
    } elsif ($line=~/^keywords\:(.*)$/) {
	$keywords=$1;
	$keywords=~s/\s*\%.*$//;
	$keywords=~s/^\s*//;
	$readsofar.=$line."\n";
    } elsif ($line!~/^\s*\*/) {
	$readsofar.=$line."\n";
    }
}
$content{$current}=$readsofar;
close(IN);
# Start problem output
print("<problem>\n<import>texas.library</import>\n");
print("<meta name='keywords' content='".$keywords."' />\n");
my %globals=&generate_script($content{'script'});
if ($multipart) {
} else {
    if ($content{'question_1'}=~/\\choice\{/s) {
# single part multiple choice
    } else {
# single part numerical
	&question($content{'question_1'});
	&numericalresponse(1,%globals);
    }
    &answer($content{'explanation_1'});
}
print("</problem>\n");


sub generate_script {
    my ($script)=@_;
    my %globals=();
    print "\n".'<script type="loncapa/perl">'."\n";
    $script=~s/\}\s*$//s;
    foreach my $line (split(/[\n\;]/,$script)) {
        unless ($line) { next; }
	if ($line=~/\s*\/\*\s*global\s* (?:double)*\s*(\w+)\s*u=\{*[^\}]*\}*\s/) {
           $line="&global('$1','$2')";
	   $globals{$1}=$2;
       } else {
	   $line=~s/\=\((\w+)\)(.+)$/\=\&$1($2)/;
	   $line=~s/random\(/\&random_default\(/;
	   foreach my $variable (keys %globals) {
	       $line=~s/\b(\Q$variable\E)\b/\$$1/g;
	   }
       }
	print $line.";\n";
    }
    print"</script>\n";
    return (%globals);
}

sub question {
    &print_text(@_);
}

sub numericalresponse {
    my ($number,%globals)=@_;
    print '<numericalresponse answer="$ans'.$number.'"';
    if ($globals{'ans'.$number}) {
	my $unit=$globals{'ans'.$number};
	print ' unit="'.$unit.'"';
    }
    print ">\n";
    print '<responseparam type="tolerance" default="5%" name="tol" description="Numerical Tolerance" />'."\n";
    print '<responseparam name="sig" type="int_range,0-16" default="0,15" description="Significant Figures" />'."\n";
    print "<textline />\n</numericalresponse>\n";
}

sub answer {
    print "\n<postanswerdate>\n";
    &print_text(@_);
    print "\n</postanswerdate>\n";
}

sub print_text {
    my ($text)=@_;
    print "\n<startouttext />\n<m eval='on'>";
    $text=~s/\@(\w+)/\&$1/gs;
    print $text;
    print "</m>\n<endouttext />\n";
}

Index: modules/gerd/texas/texas.library
+++ modules/gerd/texas/texas.library
<library>
<script type="loncapa/perl">
sub global {
   my ($var,$value,$unit)=@_;
   my $return='$'.$var.' '.$unit;
   eval("sub $var { return \"$return\"; }".'$'.$var."=$value;");
}

sub random_default {
   return &random(@_);
} 
</script>
</library>

Index: modules/gerd/texas/samples/LonCapa01.txt
+++ modules/gerd/texas/samples/LonCapa01.txt
/* CalC16b04n
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
quest 1
\medskip
What is the value of $@a + @b$?

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
expl 1

Adding, we see that
$$\fbox{$\phantom{u \over v} @a + @b \ = \ @c \ \,$}\,.$$



\bigskip %keywords
\hrule width 2.0in %keywords
\medskip \noindent  %keywords
keywords:  addition %keywords

*/
void answer(void) {
  /* global double ans1 u={} */
  /* global double a u={} */
  /* global double b u={} */
  /* global double c u={} */


    a=(int)random(2.0,5.9999,5.0,3.0);
    b=(int)random(6.0,9.9999,5.0,8.0);

    c=a+b;

  ans1=c;
}

Index: modules/gerd/texas/samples/LonCapa02.txt
+++ modules/gerd/texas/samples/LonCapa02.txt
/* hw9 format
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
quest 1
\medskip
What is the color of an orange?

\choice{}{1}{orange}

\choice{}{2}{lemon}

\choice{}{3}{lime-green}

\choice{}{4}{tangerine}

\choice{}{5}{sky-blue}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
expl 1

As its name indicates, an orange has color
$$\fbox{$\phantom{u \over v} \hbox{orange}\ \, $}\,.$$

\bigskip %keywords
\hrule width 2.0in %keywords
\medskip \noindent %keywords
keywords: fruit, color %keywords

*/

void answer(void) {
  /* global double ans1 u={} */


  ans1 = 1.0;
}

Index: modules/gerd/texas/samples/LonCapa03.txt
+++ modules/gerd/texas/samples/LonCapa03.txt
/* hw9 format
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
quest 1
\medskip
Simplify the expression
$$f(x)\ = \ (x+@a)^3 + (x-@a)^3  $$
as much as possible.

\choice{}{1}{$\ f(x) \ = \ x(x^2 + @a3) $}

\choice{}{2}{$\ f(x)\ = \ x(x^2 - @a2) $}

\choice{}{3}{$\ f(x) \ = \ 3x^2 + @a2 $}

\choice{}{4}{$\ f(x) \ = \ 2x(x^2 + @a3) $}

\choice{}{5}{$\ f(x) \ = \ 2x(x^2 - @a3) $}

\choice{}{6}{$\ f(x) \ = \ 3x^2 - @a2 $}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
expl 1

Using the standard factorization
$$a^3 + b^3\ =\ (a+b)(a^2 -ab +b^2)$$
with $a = x+@a$ and $b = x-@a$, we can rewrite
the expression as
$$f(x) =2x\{(x+@a)^2-(x+@a)(x-@a)+(x-@a)^2\}\,.$$
After simplification this becomes
$$\fbox{$\phantom{u \over v}f(x) \ = \ 2x(x^2+@a3)\ 
\,$}\,.$$


\bigskip %keywords
\hrule width 2.0in %keywords
\medskip \noindent  %keywords
keywords:  sum of cubes, simplify, %keywords
 %keywords

*/

void answer(void) {
  /* global double ans1 u={} */
  /* global double a  u={} */
  /* global double a2 u={} */
  /* global double a3 u={} */

   a=(int)random(1.0,3.9999,5.0,3.0);

   a2=a*a;
   a3=3*a*a;

   ans1 = 4;
}