Test cases which allow multiple types of Numpy arrays

Re: Test cases which allow multiple types of Numpy arrays

de Richard Lobb -
Número de respuestas: 1
All our in-house question types are available on github here. However, I doubt they're what you want right now, as they're extremely complex with minimal documentation.

I attach an xml export of an implemention of your even_array question. It looks like this (displaying the integers as floats):

Screenshot of even_array question.

Behind the scenes what's happening here is:

  1. The template has been changed to a useful general-purpose form that runs the contents of the globalextra field before running each test. Further each test runs the contents of its TEST.extra field before each test.
    {{ STUDENT_ANSWER }}
    
            SEPARATOR = "#<ab@17943918#@>#"
            {{ QUESTION.globalextra }}
            {% for TEST in TESTCASES %}
            {{ TEST.extra }}
            {{ TEST.testcode }}
            {% if not loop.last %}
            print(SEPARATOR)
            {% endif %}
            {% endfor %}
            
  2. I've set the globalextra field to Python code that redefines the print function to convert any parameters that are numpy arrays to floats. It's best to use globalextra for this rather than TEST.extra to avoid repeating it for every test.
    import numpy as __np__
    __saved_print__ = print
    def print(*args, **kwargs):
        newargs = [arg.astype(float) if isinstance(arg, __np__.ndarray) else arg for arg in args]
        __saved_print__(*newargs, **kwargs)
      

However, with this approach, be prepared to field questions from students asking why the coderunner output displays all the numbers with a decimal point after them, whereas when they test their own code there are no decimal points.

En respuesta a Richard Lobb

Re: Test cases which allow multiple types of Numpy arrays

de Peter Bratby -
Thanks, Richard. This makes perfect sense now.

Based on this discussion, and using the 'keep it simple for now' principle, I think I will go with your first suggestion (i.e. only accepting a specific array type). I'll understand better once real students start using it!