13 October, 2013

# The background of Cody solutions having extremely low size

• There are some solutions in MATLAB Cody having extremely low size, how are they done?
• It means, that there may be any way of cheating?

Do not get confused if you see an extremly low solution size in MATLAB Cody: this is achieved by using dynamic regular expressions having usually a code size of 10 or 11.

Let us see an example on Cody problem #17:

Given an input vector x, find all elements of x less than 0 or greater than 10 and replace them with NaN.

An example input and output pair:

x = [  5  17 -20  99  3.4  2  8  -6 ]
y = [  5 NaN NaN NaN  3.4  2  8 NaN ]


The general solution is having a code size of 19:

function x = clean_data(x)
x(x < 0 | x > 10) = NaN
end

But in a way of cheating a code size of 11 can be reached:

function x = clean_data(x)
regexp('', '(?@x(x < 0 | x > 10) = NaN)')
end


Here the regexp command is called with special parameters causing MATLAB to evaluate the code in the second parameter. Since it is a single string, the complexity of the code will remain 11 irrespectively of the length of the given strings.

To explain it a bit more according to the documentation: if a regular expression is called by a parameter of form (?@command), then MATLAB executes the code represented in command. This functionality was originally developed to help diagnosing regular expressions. As you see, in addition it can be used by cheaters to reduce the size of their code...

So do not get confused if you see an extremly low solution size. Keep submitting real solutions!

Tags: