Cody Problem 33 | Leading answer is in Chinese ?!
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (2)
Walter Roberson
le 3 Déc 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
le 19 Août 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 commentaires
Voir également
Catégories
En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!