Cody Problem 33 | Leading answer is in Chinese ?!
1 view (last 30 days)
Show older comments
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.
0 Comments
Answers (2)
Walter Roberson
on 3 Dec 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
on 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';
0 Comments
See Also
Categories
Find more on Interactive Control and Callbacks in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!