Question Authors' Forum

Files and random numbers.

Files and random numbers.

by Al Spay -
Number of replies: 5

Dear developers,

First of all, thanks for your plugin.

We use it to teach programming at school in C ++, Pascal, Python.

There are still a lot of questions and we need help.

Perhaps these questions arise not only for us, but also for many other teachers. It would be great to add detailed descriptions to the documentation.

2 questions:

1. It is necessary to check the operation of the program on several files (read numbers or lines from the file and process them). We have learned how to check the program on one file.

2. Random input of numbers into the program and checking its work.

In reply to Al Spay

Re: Files and random numbers.

by Вася Пупкин -
Did you manage to use multiple files?
In reply to Вася Пупкин

Re: Files and random numbers.

by Richard Lobb -

The easiest ways to test a program with multiple different input files are:

  1. Have the program read from standard input and paste the different test data sets into the Input fields of the test cases in the author editing form. When CodeRunner runs questions that have non-empty input, it creates a data file for each of the input data sets and runs the program multiple times with standard input redirected to each of the data files in turn (equivalent to prog < data.txt at the command line)
  2. Get the students to read the name of the file to be processed from standard input, upload a set of data files to the Support Files, and in the various test cases set the Input for the test to be the name of one of the data files.
  3. Get the students to instead write a function that takes the name of the test file as a parameter.
If you've asked students to read from a filename like data.txt that is hard-coded into their program, you need to write your own template to set up the test data for each test case. For example, you might have support files test1.txt, test2.txt etc, and set the Extra field of each test case to the name of the file you wish to test with. The template code (which now needs to be a per-test-case template, not a combinator template) could then copy the required support file to data.txt, copy the students answer to a file prog.py (or whatever) and then compile (if necessary) and run the program with that data file. But that's far more complicated.
In reply to Вася Пупкин

Re: Files and random numbers.

by Al Spay -

Added to the template:

if {{TEST.testcode}} == 1:

    shutil.copy(r'input1.txt', r'input.txt')

elif {{TEST.testcode}} == 2:

    shutil.copy(r'input2.txt', r'input.txt')

elif {{TEST.testcode}} == 3:

    shutil.copy(r'input3.txt', r'input.txt')

elif {{TEST.testcode}} == 4:

    shutil.copy(r'input4.txt', r'input.txt')

elif {{TEST.testcode}} == 5:

    shutil.copy(r'input5.txt', r'input.txt')

    and added the necessary files. Students in their program open only one file - "input.txt"

I do not know whether I did it right or not, but it works

In reply to Al Spay

Re: Files and random numbers.

by Al Spay -

But I still haven't figured out how to set random values and check the correctness of the answer.


For example, the task:

Fill the array with random numbers from A to B. Find the sum of the array elements.


In tests, set different values for A and B. But here's how to check the correctness of the answer ...

In reply to Al Spay

Re: Files and random numbers.

by Richard Lobb -

Well done on your input file switcheroo - very cunning.

I don't think I really understand what you're after with this random question. How does the student code get hold of the numbers A and B? If it's a pure 'write a program' question, they'd have to read them from a file or standard input.

I attach a question that seems to fulfil the above spec, but I'm at all sure if it's what you're after. Did you want each student to see different random numbers in their tests? That's possible, but it does complicate things and doesn't make the question any more robust against cheating - if one student copies another's code, it will still work in their variant.

I built the question without filling in the 'Expected' fields of the tests, ran the sample answer with Validate On Save on, and just clicked the buttons to copy the expected answers in.

I doubt it's what you want, but it's the starting point for a discussion.