重新发现科迪
最近,我一直在Matlab Central上花费更多的时间,如果还没有,我想鼓励您尝试一些资源。
你听说过吗科迪? 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.)
例如,今天早上,这个科迪问题吸引了我的注意:
You are given a logical matrixBWof any dimension, and a dimension暗淡。您需要找到最后一个非零元素的位置BW在给定的维度。如果在元素位置不存在非零件,请在该位置返回0。
例如,给定:
bw = [0 0 0 0;1 1 1 1;0 1 1 0]
WhenDIM = 1,,,,you should returnans = [2 3 3 2],,,,because these are the row numbers of the last non-zeroes in each column.
WhenDIM = 2,,,,you should returnans = [0; 4; 3],因为这些是每行最后一个非零的列编号。
不要忘记输入不仅限于2D矩阵。
This problem caught my eye because any time I seeBW用作变量名称,尤其是对于0s和1的数组,它说图像处理大部头书。我也喜欢思考不同维度的多维阵列操作。
经过一点思考,我使用功能组合了三行解决方案翻动,,,,max,,,,尺寸,,,,andany。(我不会比这里更具体。尝试自己解决!)
创建解决方案之后,我花了几分钟查看其他人如何解决它。我尤其要研究具有比我的代码尺寸更小的解决方案。金宝搏官方网站在本练习中,我几乎总是学到一些有趣的东西,是一种应用熟悉的MATLAB功能的新方法。
使用新模式功能解决科迪问题
A few days ago, I was looking at this problem:
Problem 31. Remove all the words that end with "ain"
给定字符串S1,,,,return the stringS2删除目标字符。
例如,给定
S1= 'the main event'
您的代码将返回
S2 ='事件'
注意“主”和“事件”之间的两个空间仅删除了单词“ main”中的四个字母。
当这个问题最初写成时,我本来希望看到很多解决方案金宝搏官方网站regexp和正则表达式。但是MATLAB R2020B版本具有新的模式功能,该功能旨在使文本处理代码更容易编写和理解。我想尝试一下我的科迪解决方案。(看一眼Jiro Doke's 15-Oct-2020 postabout patterns.)
Here's what I came up with:
p = letterBoundary + asManyOfPattern(lettersPattern) +“ ain”+字母
p = pattern Matching: letterBoundary + asManyOfPattern(lettersPattern) + "ain" + letterBoundary
s =“西班牙的雨主要落在平原上”; replace(s,p,“”)
ans =“ inth falls主要在”
给科迪尝试 - 但不要尽快有其他事情!您是否希望与图像相关的东西开始?尝试问题181.更改图像中的特定颜色。Or search for科迪problems that are tagged "image processing".
如果您遇到了特别有趣的Cody问题(或解决方案),请在此处在这里告知我们。
Comments
要发表评论,请单击here登录您的数学帐户或创建一个新帐户。