Magic Comments

Magic comments are used to parse the setup code that is added while creating a problem. Each @cood: annotation serves specific functions, helping maintain versioning, tracking user modifications, and defining behavior for various blocks in the code.

These are the following magic comment supported currently -

  1. @cood:version;type=v1; - Specifies the version of the code. Used for client- and backend-side validation during save or edit actions.

    • type:v1 required
  2. @cood:user-code;block=start|end; - Marks the start or end of the user-defined code block. Optionally, specify the language (e.g., lang=javascript or lang=python).

    • lang=javascript|python; optional
  3. @cood:comparator - Optional. Used to define how outputs/results are compared. Currently options include type=array-compare or type=custom. In future there will be multiple other type of comparison for eg. dictionary or object, lenght based comparison, or tree matching comparison etc, but since right now these are not present we will have to use mostly custom type and define our own implementation for comparison.

    • type=array-compare;

      or

    • type=custom;identifier=<function-name>

  4. @cood:transformer; - Defines input/output transformations for test cases, allowing different behaviors (for=input, for=output). Just like comparator here we can use either inbuilt functions or we can define custom logic as well.

    • for=input;type=<function-name>;ref=copy|same; // testcase input
    • for=output;type=<function-name>; // testcase output
    • type=custom;identifier<function-name>;
  5. @cood:response;type=error;identifier=<function-name> - Specifies error message handlers based on function identifiers.

  6. @cood:response;type=success;identifier=<function-name> - Specifies success message handlers based on function identifiers.

  7. @cood:utils;block=start|end; - Designates utility functions section.

  8. @cood:imports;block=start|end; - Marks imports section.

  9. @cood:runner;block=start|end; - Identifies runnable code

eg. 1 python

# @cood:version;type=v1;lang=py;
# @cood:comparator;type=generic-type-based;
# @cood:response;type=success;identifier=generate_success_msg;
# @cood:response;type=error;identifier=generate_failure_msg;
 
# @cood:user-code;block=start;identifier=find_pair_indices;
def find_pair_indices(arr, target):
    """
    Do not alter this instead write your code here
    """
# @cood:user-code;block=end;
 
# @cood:utils;block=start;
def generate_text(inp):
    if len(inp[0]) <= 10:
        return f"{inp[0]}, {inp[1]}"
 
    prefix_array = inp[0][:10]
    input_suffix = f"...{len(inp[0]) - 10} more"
 
    return f"{prefix_array} {input_suffix}, {inp[1]}"
 
def generate_success_msg(testcase):
    txt = f"Test case - {generate_text(testcase['input'])}, ran successfully"
 
def generate_failure_msg(testcase):
    txt = f"Test case - {generate_text(testcase['input'])} failed, expected {testcase['output']}"
# @cood:utils;block=end;
 

eg. 2 javascript

// @cood:version;type=v1;lang=py;
// @cood:comparator;type=generic-type-based;
// @cood:response;type=success;identifier=generateSuccessMessage;
// @cood:response;type=error;identifier=generateErrorMessage;
 
// @cood:user-code;block=start;identifier=findPairIndices;
function findPairIndices(arr, target) {
    // Do not alter this instead write your code here
}
// @cood:user-code;block=end;
 
// @cood:utils;block=start;
function generateText() {
    if (input[0].length < 10) {
        return `${JSON.stringify(input[0])}, ${input[1]}`;
    }
 
    const prefixArray = input[0].slice(0, 10);
    const inputSuffix = `...${input[0].length - 10} more`;
 
    return `${JSON.stringify(prefixArray)} ${inputSuffix}, ${input[1]}`;
}
 
function generateSuccessMessage(testcase) {
    return `Test case - ${generateText(testcase.input)}, ran successfully`;
}
 
function generateErrorMessage(testcase) {
    return `Test case - ${generateText(testcase.input)} failed, expected ${testcase.output}`;
}
// @cood:utils;block=end;