Cody Problem 33 | Leading answer is in Chinese ?!

1 Ansicht (letzte 30 Tage)
Sadi Altamimi
Sadi Altamimi am 3 Dez. 2013
Beantwortet: Tom Gaudette am 19 Aug. 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.

Antworten (2)

Walter Roberson
Walter Roberson am 3 Dez. 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 am 19 Aug. 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';

Kategorien

Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by