Dear all,
I want to test some students answers with some tolerance (like 5% relative). Currently I have been just comparing exact answers but using less decimals, which is not ideal. I am pretty sure the grader combinator is the answer, but I have not been able to implement it successfully. I have read:
- https://coderunner.org.nz/mod/forum/discuss.php?d=618
- https://trampgeek.github.io/moodle-qtype_coderunner/#grading-with-templates
- https://coderunner.org.nz/mod/forum/discuss.php?d=365
and tried to implement it using the function at the end (is in python but as long as I understand it can work with c++ programs because it just captures the expected output and the students answer). But I must be sincere, I dont even know where to put the testing function, and It seems that llm are confusing old and new terms in code runner or just hallucinating fields where to put this so I am stuck.
If someone has a clue about a simple implementation for c++ questions ( i mention the language just in case it is important), or if is there an actual question example implementing this, I will be glad if you share it with me.
Thanks in advance.
import json import sys # Get the student's output and the expected output got = """{{ STUDENT_ANSWER | e('py') }}""" expected = """{{ TEST.expected | e('py') }}""" # Define the tolerance tolerance = 0.05 try: # Convert the outputs to floating-point numbers got_float = float(got) expected_float = float(expected) # Compare the numbers with tolerance if abs(got_float - expected_float) <= tolerance*expected_float: # If the answer is correct, print a JSON object with a fraction of 1 result = {"fraction": 1.0, "got": got} else: # If the answer is incorrect, print a JSON object with a fraction of 0 result = {"fraction": 0.0, "got": got} except ValueError: # If the output cannot be converted to a float, mark it as incorrect result = {"fraction": 0.0, "got": got} # Print the JSON object to be processed by CodeRunner print(json.dumps(result))