Cody Problem 33 | Leading answer is in Chinese ?!

2 visualizaciones (últimos 30 días)
Sadi Altamimi
Sadi Altamimi el 3 de Dic. de 2013
Respondida: Tom Gaudette el 19 de Ag. de 2015
Salam,
While I was playing with Cody, I found one interesting question that asked to write MATLAB function to create times-table (can be found here: http://bit.ly/1cWZGGM ). The question itself was easy, but I had problems to figure out how could someone solve this problem with compact code of size 10 !!
After I've checked the leading answer, it seems to be written in Chinese not in MATLAB :)
Here is the answer:
function m = timestables(n)
regexp '' '(?@A=repmat([1:n]'',1,n);m=A.*A'';)'
end
Could anyone translate this to me so that I can use it in my future attempts ? :P
Regards.

Respuestas (2)

Walter Roberson
Walter Roberson el 3 de Dic. de 2013
It is a spiritual cheat. For more about how it works see the recent thread http://www.mathworks.co.uk/matlabcentral/answers/106546-how-are-ninja-solutions-even-possible-in-cody

Tom Gaudette
Tom Gaudette el 19 de Ag. de 2015
This is not very readable by all, but I will try to parse this for you.
1) The command actually being executed is regexp(str, pattern), but it is doing this by knowing that MATLAB takes text after a function as a string. So you could rewrite this by saying:
regexp('','(?@A=repmat([1:n]'',1,n);m=A.*A'';)');
2) regexp takes the (?@cmd) with cmd being MATLAB code. You can see this in the doc here. regexp doc
So they are really just running the code:
A=repmat([1:n]',1,n);
m=A.*A';

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by