<?xml version="1.0" encoding="UTF-8"?>
<quiz>
<!-- question: 0  -->
  <question type="category">
    <category>
        <text>$system$/Questions for export</text>

    </category>
  </question>

<!-- question: 18340  -->
  <question type="coderunner">
    <name>
      <text>PROTOTYPE_octave_via_python</text>
    </name>
    <questiontext format="html">
      <text><![CDATA[<p>An example of a simple Python script that runs an Octave program and collects its output for further analysis.</p>]]></text>
    </questiontext>
    <generalfeedback format="html">
      <text></text>
    </generalfeedback>
    <defaultgrade>1.0000000</defaultgrade>
    <penalty>0.0000000</penalty>
    <hidden>0</hidden>
    <coderunnertype>octave_via_python</coderunnertype>
    <prototypetype>2</prototypetype>
    <allornothing>1</allornothing>
    <penaltyregime>10, 20, ...</penaltyregime>
    <precheck>0</precheck>
    <showsource>0</showsource>
    <answerboxlines>18</answerboxlines>
    <answerboxcolumns>100</answerboxcolumns>
    <answerpreload></answerpreload>
    <useace>1</useace>
    <resultcolumns></resultcolumns>
    <template><![CDATA[""" A relatively simple per-test-case template for a question type that
    runs an Octave program via a Python script. Because the student submission
    is preceded by line 'clear;' in order to force script mode rather than
    function mode, Octave error message line numbers are all too high by 1.
    This could be fixed by post-processing the stderr output.
"""

import subprocess, sys

# This example constructs a script consisting of clear, the student answer, the
# TEST.testcode and the TEST.extra. 
student_answer = """{{ STUDENT_ANSWER | e('py') }}"""
test_code = """{{ TEST.testcode | e('py') }}"""
extra = """{{ TEST.extra | e('py') }}"""
with open('prog.m', 'w', encoding='utf8') as src:
    print('clear;', file=src)
    print(student_answer, file=src)
    print(test_code, file=src)
    print(extra, file=src)

# Octave is interpreted so we don't need to compile first. Just go straight
# into the execution phase. stdin is already set up.

failed = False
flags = '--silent --no-gui --no-history --no-window-system --norc'
command = 'octave {} prog.m'.format(flags).split()
output = ''
stderr = ''
try:
    with open('prog.stderr', 'w', encoding='utf8') as f_stderr:
        output = subprocess.check_output(
            command,
            stderr=f_stderr,
            universal_newlines=True
        )
    with open('prog.stderr', encoding='utf8') as f_stderr:
        stderr += f_stderr.read()
except subprocess.CalledProcessError as e:
    if e.returncode > 0:
        # Ignore non-zero positive return codes
        if e.output:
            output = e.output
    else:
        # But negative return codes are signals - abort
        failed = true
        if e.output:
            print(e.output, file=sys.stderr)
        if e.returncode < 0:
            print("Task failed with signal", -e.returncode, file=sys.stderr)
        print("** Further testing aborted **", file=sys.stderr)
            
if not failed:
    # Analyse the output + stderr from the Octave run in any suitable way. Print
    # appropriate feedback to student. 
    # This example just prints the actual output and stderr. 
    print(output)
    if stderr:
        print(stderr, file=sys.stderr)]]></template>
    <iscombinatortemplate>0</iscombinatortemplate>
    <allowmultiplestdins>0</allowmultiplestdins>
    <answer></answer>
    <validateonsave>0</validateonsave>
    <testsplitterre><![CDATA[|#<ab@17943918#@>#\n|ms]]></testsplitterre>
    <language>python3</language>
    <acelang>matlab</acelang>
    <sandbox></sandbox>
    <grader>NearEqualityGrader</grader>
    <cputimelimitsecs>10</cputimelimitsecs>
    <memlimitmb>800</memlimitmb>
    <sandboxparams></sandboxparams>
    <templateparams></templateparams>
    <uiplugin>ace</uiplugin>
    <testcases>
    </testcases>
  </question>

<!-- question: 18341  -->
  <question type="coderunner">
    <name>
      <text>Test simple function question of octave_via_python type</text>
    </name>
    <questiontext format="html">
      <text><![CDATA[<p>Write an Octave function sqr(n) that return n-squared.</p>]]></text>
    </questiontext>
    <generalfeedback format="html">
      <text></text>
    </generalfeedback>
    <defaultgrade>1.0000000</defaultgrade>
    <penalty>0.0000000</penalty>
    <hidden>0</hidden>
    <coderunnertype>octave_via_python</coderunnertype>
    <prototypetype>0</prototypetype>
    <allornothing>1</allornothing>
    <penaltyregime>10, 20, ...</penaltyregime>
    <precheck>0</precheck>
    <showsource>0</showsource>
    <answerboxlines>18</answerboxlines>
    <answerboxcolumns>100</answerboxcolumns>
    <answerpreload></answerpreload>
    <useace></useace>
    <resultcolumns></resultcolumns>
    <template></template>
    <iscombinatortemplate></iscombinatortemplate>
    <allowmultiplestdins></allowmultiplestdins>
    <answer>function square = sqr(n)
    square = n * n;
end</answer>
    <validateonsave>0</validateonsave>
    <testsplitterre></testsplitterre>
    <language></language>
    <acelang></acelang>
    <sandbox></sandbox>
    <grader></grader>
    <cputimelimitsecs></cputimelimitsecs>
    <memlimitmb></memlimitmb>
    <sandboxparams></sandboxparams>
    <templateparams></templateparams>
    <uiplugin></uiplugin>
    <testcases>
      <testcase testtype="0" useasexample="0" hiderestiffail="0" mark="1.0000000" >
      <testcode>
                <text>disp(sqr(-11))</text>
      </testcode>
      <stdin>
                <text></text>
      </stdin>
      <expected>
                <text> 121</text>
      </expected>
      <extra>
                <text></text>
      </extra>
      <display>
                <text>SHOW</text>
      </display>
    </testcase>
      <testcase testtype="0" useasexample="0" hiderestiffail="0" mark="1.0000000" >
      <testcode>
                <text>disp(sqr(9))</text>
      </testcode>
      <stdin>
                <text></text>
      </stdin>
      <expected>
                <text> 81</text>
      </expected>
      <extra>
                <text></text>
      </extra>
      <display>
                <text>SHOW</text>
      </display>
    </testcase>
      <testcase testtype="0" useasexample="0" hiderestiffail="0" mark="1.0000000" >
      <testcode>
                <text>disp(sqr(100))</text>
      </testcode>
      <stdin>
                <text></text>
      </stdin>
      <expected>
                <text> 10000</text>
      </expected>
      <extra>
                <text></text>
      </extra>
      <display>
                <text>SHOW</text>
      </display>
    </testcase>
    </testcases>
  </question>

</quiz>