# Rediscovering Cody

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 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.)

This morning, for example, this Cody problem attracted my attention:

Problem 340. Find the last non-zero in a given dimension

You are given a logical matrix BW of any dimension, and a dimension dim . You need to find the locations of the last non-zero element of BW in that given dimension. If no non-zeros exist at an element location, return a 0 at that location.

For example, given:

BW = [0 0 0 0; 1 1 1 1; 0 1 1 0]

When dim = 1 , you should return ans = [2 3 3 2], because these are the row numbers of the last non-zeroes in each column.

When dim = 2 , you should return ans = [0; 4; 3], because these are the column numbers of the last non-zeroes in each row.

Don't forget that the input isn't restricted to 2D matrices.

This problem caught my eye because any time I see BW used as a variable name, especially for an array of 0s and 1s, it says image processing to me. I also like thinking about multidimensional array operations in different dimensions.

After a little thought, I put together a three-line solution using the functions flip, max, size, and any. (I won't be more specific than that here. Try to solve it yourself!)

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.

Solving a Cody problem using the new patterns feature

A few days ago, I was looking at this problem:

Problem 31. Remove all the words that end with "ain"

Given the string s1 , return the string s2 with the target characters removed.

For example, given

s1 = 'the main event'

s2 = 'the  event'

Note the 2 spaces between "main" and "event" Only the four letters in the word "main" were deleted.

When this problem was originally written, I would have expected to see a lot of solutions using regexp 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 Jiro Doke's 15-Oct-2020 post about patterns.)

Here's what I came up with:

p = letterBoundary + asManyOfPattern(lettersPattern) + "ain" + letterBoundary

p =

pattern

Matching:

letterBoundary + asManyOfPattern(lettersPattern) + "ain" + letterBoundary


s = "The rain in Spain falls mainly on the plain";
replace(s,p,"")

ans =

"The  in  falls mainly on the "



Give Cody a try -- but not when you have something else due soon! Do you want something image-related to start with? Try Problem 181. Change a specific color in an image. Or search for Cody problems that are tagged "image processing".

If you come across Cody problem (or solution) that you find especially interesting, let us know here in the comments.

Published with MATLAB® R2020b

|