Code Widget: Coding Exercises

In the context of learning, coding challenges are a structured way to practice and demonstrate expertise in coding and problem-solving. You can use the 'Code widget' to assess and improve proficiency in a particular programming language.

Add the code widget and select the "Exercise" checkbox. This is what it looks like:

Edit mode
Edit mode

This feature lets you evaluate the learner's attempt through pre-defined test cases.

What do I need to know?

  • Solution: You can provide the solution to the challenge in the "Solution" tab below the "Test" button. Select the "Solution" checkbox so the provided solution is visible to the learners.

  • Evaluate in Another Language: You can specify the programming language when defining your test cases. It can be different from the one learners will code in.

Can I hide some part of my code?

You can hide part of your code. You can prepend the code (from the main testing file) in the "Prepend" tab, e.g., the commands to include various packages, etc.

Can I give hints?

You can add hints in the "Hints" tab. See the slides below for reference.

Open the "Hints" tab
Open the "Hints" tab
1 of 4

Types of Testing

The "Evaluation" tab lets you choose the test methodology and design test cases accordingly. The "Create Stub for C++" button generates the default test code for C++. This option will show up for any language that you choose.

There are two basic evaluation metrics: test cases and console. You can select one of them from the drop-down. For "Test Results", the code should be tested against the returned value/values. And, for "Console", the code is tested against the output printed on the console.

Consider a testing scenario, where we want learners to calculate the square of an integer correctly.

Can I hide some test cases?

It is possible to hide test cases at the learner's end. Look out for the following statement in the evaluation stub: std::vector<bool> hide_test = {false, false, false, false};. To hide a test case, you can set the value to true. This is how your test cases can be divided into private and public test cases.

Observe the difference in the output below.

#include <iostream>
using namespace std;
int square(int n) {
// your code goes here
return 0;
}

Support of functionless evaluation

You can write an evaluation according to your design as long as you create a TestResult/TestDetail object for each test case and add it to the results list. The fields of each TestResult must be filled appropriately. For example, you can make a functionless evaluation as well where you are checking the value of a variable or object. Let’s say you want to test the value of a variable called score in Python. Here’s what the evaluation would look like:

class TestResult():
def __init__(self):
self.succeeded = False; # Whether this test succeeded or not
self.reason = "" # What's the reason (specially if failed)
self.input = "" # What was the input?
self.expected_output = "" # What was the expected output?
self.actual_output = "" # What was the output when this test ran
self.hidden = False
def executeTests():
results = []
result = TestResult()
result.input = 'No input needed'
result.expected_output = str(50) # The value we want the variable to have
result.actual_output = str(score) # score is being taken from the user's code
if result.actual_output == result.expected_output: # Compare the two values
result.succeeded = True
result.reason = "Succeeded"
else:
result.succeeded = False
result.reason = "Incorrect Output"
results.append(result)
return results