File:  [LON-CAPA] / loncom / homework / templates / sampleexternal.problem
Revision 1.7: download - view: text, annotated - select for diffs
Wed Jun 3 10:25:12 2015 UTC (9 years, 5 months ago) by droeschl
Branches: MAIN
CVS tags: version_2_12_X, version_2_11_X, version_2_11_5_msu, version_2_11_5, version_2_11_4_uiuc, version_2_11_4_msu, version_2_11_4, version_2_11_3_uiuc, version_2_11_3_msu, version_2_11_3, version_2_11_2_uiuc, version_2_11_2_msu, version_2_11_2_educog, version_2_11_2, HEAD
The first testcase (0=1) caused infinite recursion with the consequence
that the server runs out of memory. fact(n) now returns 1 for n <= 1.
However, this won't prevent deficient submissions to run into the same
consequences.

<problem>
<parameter name="externalurl" type="string" description="URL for external testing" />
<script type="loncapa/perl">
sub fact {
   my $n=shift;
   if ($n<=1) {
      return 1;
   } else {
      return $n*&fact($n-1);
   }
}

$testvalues='0=1';
foreach $i (1..3) {
   $rand=&random(3,8,1);
   $testvalues.=','.$rand.'='.&fact($rand);
}

#
# Set anything else you want to pass in this hash.
# Name-value pairs will be passed as posted form parameters.
#
%args=('somecode' => (<<'ENDCODE')
sub negative {
   my ($number)=@_;
   return $number<0;
}
ENDCODE
);
# Get a URL if it is externally set
$externalurl=&parameter_setting('externalurl');
#
# Important: set your default URL here
#
unless ($externalurl=~/\w/) { $externalurl='http://localhost/cgi-bin/sampleexternal.pl'; }
#
</script>


<startouttext />Write a Perl subroutine called <tt>factorial</tt>, which returns the factorial of its argument, e.g. <tt>&amp;factorial(17)=17!</tt>. You can use the
boolean function <tt>&amp;negative(number)</tt>, which returns <tt>true</tt> if the argument is negative.
<instructorcomment>
Sample code for an evaluation script can be found <a href="/res/adm/includes/templates/sampleexternal.pl">here.</a></instructorcomment>
<endouttext />
<externalresponse url="$externalurl" answer="$testvalues" form="%args" answerdisplay="The most elegant (and dangerous) solution is recursive.">
<textfield></textfield>

</externalresponse>
</problem>

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>