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 ago) by droeschl
Branches: MAIN
CVS tags: version_2_12_X, version_2_11_X, 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.

    1: <problem>
    2: <parameter name="externalurl" type="string" description="URL for external testing" />
    3: <script type="loncapa/perl">
    4: sub fact {
    5:    my $n=shift;
    6:    if ($n<=1) {
    7:       return 1;
    8:    } else {
    9:       return $n*&fact($n-1);
   10:    }
   11: }
   12: 
   13: $testvalues='0=1';
   14: foreach $i (1..3) {
   15:    $rand=&random(3,8,1);
   16:    $testvalues.=','.$rand.'='.&fact($rand);
   17: }
   18: 
   19: #
   20: # Set anything else you want to pass in this hash.
   21: # Name-value pairs will be passed as posted form parameters.
   22: #
   23: %args=('somecode' => (<<'ENDCODE')
   24: sub negative {
   25:    my ($number)=@_;
   26:    return $number<0;
   27: }
   28: ENDCODE
   29: );
   30: # Get a URL if it is externally set
   31: $externalurl=&parameter_setting('externalurl');
   32: #
   33: # Important: set your default URL here
   34: #
   35: unless ($externalurl=~/\w/) { $externalurl='http://localhost/cgi-bin/sampleexternal.pl'; }
   36: #
   37: </script>
   38: 
   39: 
   40: <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
   41: boolean function <tt>&amp;negative(number)</tt>, which returns <tt>true</tt> if the argument is negative.
   42: <instructorcomment>
   43: Sample code for an evaluation script can be found <a href="/res/adm/includes/templates/sampleexternal.pl">here.</a></instructorcomment>
   44: <endouttext />
   45: <externalresponse url="$externalurl" answer="$testvalues" form="%args" answerdisplay="The most elegant (and dangerous) solution is recursive.">
   46: <textfield></textfield>
   47: 
   48: </externalresponse>
   49: </problem>

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