## Question Authors' Forum

### Multilingual questions

Multilingual questions

Hi!

I want to give my students an opportunity to use different languages for answer.
For example use Java or C++ for an answer  a question.

What is the right way to organize that?

I can use Python with finding keywords in the solution and launching the right compiler
(or to require from my students to write special comments with the name of the language they use).
But may be there is an easy standart way to do that?

Re: Multilingual questions

It's a good question to raise, Ilyin. I've vaguely wondered in the past if anyone wanted such a capability, though I've never wanted it myself. It's mostly only applicable to "write a program" questions, of course, since otherwise (e.g. "write a function ...") the spec itself becomes language dependent.

There's no "nice" way to do it at present, although the two ways you suggest are both workable. Another possibility: you could inject HTML at the end of the question (using an editor in HTML mode) to display a drop-down Language Select menu. You'd also need a script to insert the menu state into the question answer somehow when the answer is submitted. I haven't thought through how do-able that is. You might have to switch off Ace to make it work, but Ace is a bit redundant if you don't know the language. All options at present are a bit hacky, though.

I'll put this on the list of things to think about in my development burst at the end of this year. As a related aside, I have a student working on a UI plug-in that provides a graph-drawing program for input of the answer rather than an Ace editor panel. It's looking pretty promising. More generally we're exploring  the idea of having question authors specify the UI seen by the student, serialising the state into the answer box. Having a language select drop-down would fit within that model as an easy extension to a simple Ace-only UI.

Richard

Re: Multilingual questions

> You'd also need a script to insert the menu state into the question answer somehow when the answer is submitted.

What  is state?
I see the button "Check" does not send any information about language.
Just the plain text of answer.

I was going to add another language Multi and then run special task for the choosen language.

May be there is a way to do that easy (to change task's language on the fly).

By the way, I can just modify Python task.  I am not sure about running tasks - and am going to copy
What is the right way to call one task from another, f.e. cpp from python3?

Re: Multilingual questions

Are you sure this is worth the effort, Ilyin? Why not just have a separate version of the question for each language you're offering to the student? I agree it's not very elegant but it's way easier than all the alternatives.

By the "state" of the drop-down language menu (an HTML select element) I meant whatever value is currently selected. Getting that state value into the question requires a script within the question text, which would need to (say) insert a line at the start of the student answer, preferably in a language-specific comment, such as

// LANGUAGE = C

or

# LANGUAGE = Python

I managed to get a script like that going in preview mode but getting it working in a general would be much harder and I really don't think it's an idea worth pursuing. Far better to tell students to insert a line like that themselves at the start of their program.

I've added a section to the documentation explaining how to add new languages by scripting in Python. See http://coderunner.org.nz/mod/book/view.php?id=193&chapterid=749

You can extend the idea explained in that section of the documentation to implement a multi-language questions. For example, here's a per-test template to run a submission in either Python or C depending on the comment line at the start of the submitted program:

""" The template for a question type that accept either C or Python answers, requiring
that the first line of the program is a # or // comment for Python or C respectively.
"""

import subprocess
language = 'python'
filename = 'prog.py'
language = 'c'
filename = 'prog.c'
else:

# Write the student code to a file

with open(filename, "w") as src:

# Compile if it's C
if language == 'c':
cflags = "-std=c99 -Wall -Werror"
return_code = subprocess.call("gcc {0} -o prog prog.c".format(cflags).split())
if return_code != 0:
raise Exception("** Compilation failed. Testing aborted **")
exec_command = ["./prog"]
else: # Python doesn't need a compile phase
exec_command = ["python3", "./prog.py"]

# Now run the code. Since this is a per-test template,
# stdin is already set up for the stdin text specified in the test case,
# so we can run the compiled program directly.

try:
output = subprocess.check_output(exec_command, universal_newlines=True)
print(output)
except subprocess.CalledProcessError as e:
if e.returncode > 0:
# Ignore non-zero positive return codes
if e.output:
print(e.output)
else:
# But negative return codes are signals - abort
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)


Please realise though that that's just a prototype that's had minimal testing. It will undoubtedly need refining. I've never actually used multilingual questions myself.

Richard

Re: Multilingual questions

> Why not just have a separate version of the question for each language you're offering to the student?

Basically Algorithms are independent of language.
So we can (and we really do) teach algorithms, and let students to Implement them on their favorite language.

Thank you for the solution! It works!

I think it is great idea to add these comments by drop-down list in a question by JavaScript.