{"id":4207,"date":"2020-10-20T11:58:14","date_gmt":"2020-10-20T15:58:14","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=4207"},"modified":"2020-10-20T11:58:14","modified_gmt":"2020-10-20T15:58:14","slug":"rediscovering-cody","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2020\/10\/20\/rediscovering-cody\/","title":{"rendered":"Rediscovering Cody"},"content":{"rendered":"<div class=\"content\"><p>Lately, I've been spending more time on MATLAB Central, and I'd like to encourage you to try out some of the resources there, if you haven't already.<\/p><p>Have you heard of <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/cody\">Cody<\/a>? It is an addictive MATLAB puzzle-solving activity. You can learn a lot about MATLAB and algorithm coding approaches by trying the problems there, and especially by looking at other people's solutions. There's a competitive aspect as well, as your puzzle solutions get scored and ranked against other solutions. (In the Cody world, smaller programs get the better scores.)<\/p><p>This morning, for example, this Cody problem attracted my attention:<\/p><p>\r\n<div style=\"margin-left: 5em; font-style: italic;\">\r\n<\/p><p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/cody\/problems\/340-find-last-non-zero-in-a-given-dimension\">Problem 340. Find the last non-zero in a given dimension<\/a><\/p><p>You are given a logical matrix <tt>BW<\/tt> of any dimension, and a dimension <tt>dim<\/tt> . You need to find the locations of the last non-zero element of <tt>BW<\/tt> in that given dimension. If no non-zeros exist at an element location, return a 0 at that location.<\/p><p>For example, given:<\/p><pre>BW = [0 0 0 0; 1 1 1 1; 0 1 1 0]<\/pre><p>When <tt>dim = 1<\/tt> , you should return <tt>ans = [2 3 3 2]<\/tt>, because these are the row numbers of the last non-zeroes in each column.<\/p><p>When <tt>dim = 2<\/tt> , you should return <tt>ans = [0; 4; 3]<\/tt>, because these are the column numbers of the last non-zeroes in each row.<\/p><p>Don't forget that the input isn't restricted to 2D matrices.<\/p><p>\r\n<\/div>\r\n<\/p><p>This problem caught my eye because any time I see <tt>BW<\/tt> used as a variable name, especially for an array of 0s and 1s, it says <b>image processing<\/b> to me. I also like thinking about multidimensional array operations in different dimensions.<\/p><p>After a little thought, I put together a three-line solution using the functions <tt>flip<\/tt>, <tt>max<\/tt>, <tt>size<\/tt>, and <tt>any<\/tt>. (I won't be more specific than that here. Try to solve it yourself!)<\/p><p>After I created my solution, I spent a few minutes looking at how other people solved it. I look especially at the solutions that have a smaller code size than mine. I almost always learn something interesting, such a new way to apply familiar MATLAB functions, in this exercise.<\/p><p><b>Solving a Cody problem using the new patterns feature<\/b><\/p><p>A few days ago, I was looking at this problem:<\/p><p>\r\n<div style=\"margin-left: 5em; font-style: italic;\">\r\n<\/p><p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/cody\/problems\/31\">Problem 31. Remove all the words that end with \"ain\"<\/a><\/p><p>Given the string <tt>s1<\/tt> , return the string <tt>s2<\/tt> with the target characters removed.<\/p><p>For example, given<\/p><pre>s1 = 'the main event'<\/pre><p>your code would return<\/p><pre>s2 = 'the  event'<\/pre><p>Note the 2 spaces between \"main\" and \"event\" Only the four letters in the word \"main\" were deleted.<\/p><p>\r\n<\/div>\r\n<\/p><p>When this problem was originally written, I would have expected to see a lot of solutions using <tt>regexp<\/tt> and regular expressions. But the MATLAB R2020b release has the new patterns feature, which aims to make text processing code a easier to write and understand. I wanted to give a try with my Cody solution. (Take a look at <a href=\"https:\/\/blogs.mathworks.com\/pick\/2020\/10\/15\/r2020b-pattern-new-way-to-regular-express\/\">Jiro Doke's 15-Oct-2020 post<\/a> about patterns.)<\/p><p>Here's what I came up with:<\/p><pre class=\"codeinput\">p = letterBoundary + asManyOfPattern(lettersPattern) + <span class=\"string\">\"ain\"<\/span> + letterBoundary\r\n<\/pre><pre class=\"codeoutput\">\r\np = \r\n\r\n  pattern\r\n\r\n  Matching:\r\n\r\n    letterBoundary + asManyOfPattern(lettersPattern) + \"ain\" + letterBoundary\r\n\r\n<\/pre><pre class=\"codeinput\">s = <span class=\"string\">\"The rain in Spain falls mainly on the plain\"<\/span>;\r\nreplace(s,p,<span class=\"string\">\"\"<\/span>)\r\n<\/pre><pre class=\"codeoutput\">\r\nans = \r\n\r\n    \"The  in  falls mainly on the \"\r\n\r\n<\/pre><p>Give <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/cody\/\">Cody<\/a> a try -- but not when you have something else due soon! Do you want something image-related to start with? Try <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/cody\/problems\/818\">Problem 181. Change a specific color in an image.<\/a> Or search for <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/cody\/problems?term=tag%3A%22image+processing%22\">Cody problems that are tagged \"image processing\".<\/a><\/p><p>If you come across Cody problem (or solution) that you find especially interesting, let us know here in the comments.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_2dbd89a2414f4acb9759b6732380bc32() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='2dbd89a2414f4acb9759b6732380bc32 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 2dbd89a2414f4acb9759b6732380bc32';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        copyright = 'Copyright 2020 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n\r\n        d.title = title + ' (MATLAB code)';\r\n        d.close();\r\n    }   \r\n     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_2dbd89a2414f4acb9759b6732380bc32()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2020b<br><\/p><\/div><!--\r\n2dbd89a2414f4acb9759b6732380bc32 ##### SOURCE BEGIN #####\r\n%%\r\n% Lately, I've been spending more time on MATLAB Central, and I'd like to\r\n% encourage you to try out some of the resources there, if\r\n% you haven't already.\r\n%\r\n% Have you heard of <https:\/\/www.mathworks.com\/matlabcentral\/cody Cody>? It\r\n% is an addictive MATLAB puzzle-solving activity. You can learn a lot about\r\n% MATLAB and algorithm coding approaches by trying the problems there, and\r\n% especially by looking at other people's solutions. There's a competitive\r\n% aspect as well, as your puzzle solutions get scored and ranked against\r\n% other solutions. (In the Cody world, smaller programs get the better\r\n% scores.)\r\n%\r\n% This morning, for example, this Cody problem attracted my attention:\r\n% \r\n% <html>\r\n% <div style=\"margin-left: 5em; font-style: italic;\">\r\n% <\/html>\r\n%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/cody\/problems\/340-find-last-non-zero-in-a-given-dimension \r\n% Problem 340. Find the last non-zero in a given dimension>\r\n%\r\n% You are given a logical matrix |BW| of any dimension, and a dimension |dim| .\r\n% You need to find the locations of the last non-zero element of |BW| in that\r\n% given dimension. If no non-zeros exist at an element location, return a 0\r\n% at that location.\r\n%\r\n% For example, given:\r\n%\r\n%  BW = [0 0 0 0; 1 1 1 1; 0 1 1 0]\r\n%\r\n% When |dim = 1| , you should return |ans = [2 3 3 2]|, because these are the\r\n% row numbers of the last non-zeroes in each column.\r\n%\r\n% When |dim = 2| , you should return |ans = [0; 4; 3]|, because these are the\r\n% column numbers of the last non-zeroes in each row.\r\n%\r\n% Don't forget that the input isn't restricted to 2D matrices.\r\n%\r\n% <html>\r\n% <\/div>\r\n% <\/html>\r\n%\r\n% This problem caught my eye because any time I see |BW| used as a variable\r\n% name, especially for an array of 0s and 1s, it says *image processing* to\r\n% me. I also like thinking about multidimensional array operations in\r\n% different dimensions.\r\n%\r\n% After a little thought, I put together a three-line solution using the\r\n% functions |flip|, |max|, |size|, and |any|. (I won't be more specific than that\r\n% here. Try to solve it yourself!)\r\n%\r\n% After I created my solution, I spent a few minutes looking at how other\r\n% people solved it. I look especially at the solutions that have a smaller\r\n% code size than mine. I almost always learn something interesting, such a\r\n% new way to apply familiar MATLAB functions, in this exercise.\r\n%\r\n% *Solving a Cody problem using the new patterns feature*\r\n%\r\n% A few days ago, I was looking at this problem:\r\n%\r\n% <html>\r\n% <div style=\"margin-left: 5em; font-style: italic;\">\r\n% <\/html>\r\n%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/cody\/problems\/31 \r\n% Problem 31. Remove all the words that end with \"ain\">\r\n%\r\n% Given the string |s1| , return the string |s2| with the target characters\r\n% removed.\r\n%\r\n% For example, given\r\n%\r\n%  s1 = 'the main event'\r\n%\r\n% your code would return\r\n%\r\n%  s2 = 'the  event'\r\n%\r\n% Note the 2 spaces between \"main\" and \"event\" Only the four letters in the\r\n% word \"main\" were deleted.\r\n%\r\n% <html>\r\n% <\/div>\r\n% <\/html>\r\n%\r\n% When this problem was originally written, I would have expected to see a\r\n% lot of solutions using |regexp| and regular expressions. But the MATLAB\r\n% R2020b release has the new patterns feature, which aims to make text\r\n% processing code a easier to write and understand. I wanted to give a try\r\n% with my Cody solution. (Take a look at <https:\/\/blogs.mathworks.com\/pick\/2020\/10\/15\/r2020b-pattern-new-way-to-regular-express\/ \r\n% Jiro Doke's 15-Oct-2020 post> about\r\n% patterns.)\r\n%\r\n% Here's what I came up with:\r\n\r\np = letterBoundary + asManyOfPattern(lettersPattern) + \"ain\" + letterBoundary\r\n\r\n%%\r\ns = \"The rain in Spain falls mainly on the plain\";\r\nreplace(s,p,\"\")\r\n\r\n%%\r\n% Give <https:\/\/www.mathworks.com\/matlabcentral\/cody\/ Cody> a try REPLACE_WITH_DASH_DASH but\r\n% not when you have something else due soon! Do you want something\r\n% image-related to start with? Try\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/cody\/problems\/818 Problem 181.\r\n% Change a specific color in an image.> Or search for <https:\/\/www.mathworks.com\/matlabcentral\/cody\/problems?term=tag%3A%22image+processing%22 \r\n% Cody problems that\r\n% are tagged \"image processing\".>\r\n%\r\n% If you come across Cody problem (or solution) that you find especially\r\n% interesting, let us know here in the comments.\r\n##### SOURCE END ##### 2dbd89a2414f4acb9759b6732380bc32\r\n-->","protected":false},"excerpt":{"rendered":"<p>Lately, I've been spending more time on MATLAB Central, and I'd like to encourage you to try out some of the resources there, if you haven't already.Have you heard of Cody? It is an addictive MATLAB... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2020\/10\/20\/rediscovering-cody\/\">read more >><\/a><\/p>","protected":false},"author":42,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/4207"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/users\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/comments?post=4207"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/4207\/revisions"}],"predecessor-version":[{"id":4211,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/4207\/revisions\/4211"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=4207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=4207"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=4207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}