Cody

Problem 1937. Get input and output variable names

Solution 1982142

Submitted on 19 Oct 2019
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test Status Code Input and Output
1   Fail
s = 'function foo()'; inNames_correct = {}; outNames_correct = {}; [inNames, outNames] = get_variable_names(s); assert(isequal(inNames,inNames_correct)) assert(isequal(outNames,outNames_correct))

s = 'function foo()' ans = 0×0 empty char array

Index exceeds the number of array elements (0). Error in get_variable_names (line 6) out{1}{1} Error in Test1 (line 5) [inNames, outNames] = get_variable_names(s);

2   Pass
s = 'function a = foo(b)'; inNames_correct = {'b'}; outNames_correct = {'a'}; [inNames, outNames] = get_variable_names(s); assert(isequal(inNames,inNames_correct)) assert(isequal(outNames,outNames_correct))

s = 'function a = foo(b)' ans = 'b' ans = 'a ' inNames = 1×1 cell array {'b'} outNames = 1×1 cell array {'a'}

3   Pass
s = 'function [myOutput] = foo(b,c)'; inNames_correct = {'b';'c'}; outNames_correct = {'myOutput'}; [inNames, outNames] = get_variable_names(s); assert(isequal(inNames,inNames_correct)) assert(isequal(outNames,outNames_correct))

s = 'function [myOutput] = foo(b,c)' ans = 'b,c' ans = 'myOutput' inNames = 2×1 cell array {'b'} {'c'} outNames = 1×1 cell array {'myOutput'}

4   Pass
s = 'function [A,B] = foo(c,d,f)'; inNames_correct = {'c';'d';'f'}; outNames_correct = {'A';'B'}; [inNames, outNames] = get_variable_names(s); assert(isequal(inNames,inNames_correct)) assert(isequal(outNames,outNames_correct))

s = 'function [A,B] = foo(c,d,f)' ans = 'c,d,f' ans = 'A,B' inNames = 3×1 cell array {'c'} {'d'} {'f'} outNames = 2×1 cell array {'A'} {'B'}

5   Fail
s = sprintf('function %s=...\nmyfun %s...\n %s\n%% With comments[]()\nandCode = 4*[1 3 4/(1+2)];',... '[how,about , several,outputs]',... '(and , several, inputs ,',... 'split,over,lotsOf,lines)'); inNames_correct = {'and';'several';'inputs';'split';'over';'lotsOf';'lines'}; outNames_correct = {'how';'about';'several';'outputs'}; [inNames, outNames] = get_variable_names(s); assert(isequal(inNames,inNames_correct)) assert(isequal(outNames,outNames_correct))

s = 'function [how,about , several,outputs]=... myfun (and , several, inputs ,... split,over,lotsOf,lines) % With comments[]() andCode = 4*[1 3 4/(1+2)];' ans = 0×0 empty char array ans = 'how,about , several,outputs' inNames = 0×0 empty cell array outNames = 4×1 cell array {'how' } {'about' } {'several'} {'outputs'}

Assertion failed.