Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!

Problem 1770. singularity 2.0 (hard)

This problem is the continuation of problem Singularity 2.0 (easier)

If you have been in Cody long enough you have probably run across some badly constructed problems and test suites.

This problem, I believe, represents an apparently impossible scenario. Yet, it is possible to solve it. Do you know how?


This is this problem's testsuite:

 [a,b]=1; % oops...

As you may notice, the second line is not a proper assignment, so the testsuite will break at that point, and will return an error message:

??? Too many output arguments.

Typical hacks work by overloading some function in the evaluation code (e.g. assert hack). Unfortunately, as far as I know, assignment operators cannot be overloaded in Matlab. Is there something myfunction could do to still solve this problem?


You may use a similar trick as before but now targeting a vulnerability in the function verifyCode instead of in the testsuite. For simplicity, here is the relevant portion of the verifyCode.m file (the function that evaluates and scores Cody problems):

 function testSuiteResults = verifyCode( sourceFile, testFile )
    %VERIFYCODE Evaluates and verifies MATLAB Code against provided test suite.
    %   TESTFILE and returns the results.
    %   Copyright 1984-2012 The MathWorks, Inc.
    % Score it
    score = calculateSize(sourceFile);
    % Load the test file
    code = fileread(testFile);
    % Split it into cells
    code2 = regexprep(code,'\n%%','\nxxx-cellbreak-xxx%%');
    cellList = regexp(code2,'xxx-cellbreak-xxx','split');
    % Test each cell
    % Each testpoint structure
    % testPoint = struct(...
    %    'pass', false, ...
    %    'code', '', ...
    %    'output', '');
    testSuite = struct([]);
    [containsIllegalFcnFlag, illegalFcnMessage] = containsIllegalFcn(sourceFile);
    for countVariable = 1:length(cellList)
        cellCode = cellList{countVariable};
        cleanCellCode = cleanCode(cellCode);
        % Test is guilty until proven successful
        pass = false;
        % Run it
            output = evaluateCode(cleanCellCode);
            pass = true;
        catch me
            output = ['Error: ' me.message];
        if containsIllegalFcnFlag
            pass = false;
            output = illegalFcnMessage;
        cellOutput = cleanCode(output);
        testSuite(countVariable).code = cleanCellCode;
        testSuite(countVariable).output = cellOutput;
        testSuite(countVariable).pass = pass;
    % Set up data structure to return
    testSuiteResults = struct(...
        'pass', false, ...
        'score',   0, ...
        'functions', [], ...
        'testPoints', struct([]));
    % Populate the result struct
    % If any test point is failed, sets the overall pass status to false.
    testSuiteResults.pass = all([testSuite.pass]);
    % sets the score
    testSuiteResults.score = sprintf('%d',score);
    % Set test points
    testSuiteResults.testPoints = testSuite;
    % Set functions points
    testSuiteResults.functions = findFcns(sourceFile,'file');

Previous problem in this series: Singularity 2.0 (easier)

Next problem in this series: Singularity 2.0 (really hard)


Problem Group

Solution Statistics

20 correct solutions 35 incorrect solutions
Last solution submitted on Oct 14, 2015

Problem Comments

Solution Comments