终于上20天了,今天刷一道题目,之后打算把matlab GUI编程给记录一下,所以我之后这几天暂时不刷cody了。
(1)Cell joiner
You are given a cell array of strings and a string delimiter. You need to produce one string which is composed of each string from the cell array separated by the delimiter.
For example, this input
in_cell = {'Lorem', 'ipsum', 'dolor', 'sit', 'amet', 'consectetur'};
delim = ' ';
should produce this output:
out_str = 'Lorem ipsum dolor sit amet consectetur';
这道题目可以算是C语言的入门题目,对于matlab来说就更方便了很多,有太多的函数可以用了。
答:
function out_str = cellstr_joiner(in_cell, delim)
out_str = strjoin(in_cell,delim);
最简单的,strjoin函数可以串联数组中的字符串,str = strjoin(
通过将 C
,delimiter
)C
的每个元素与 delimiter
中的元素联接来构造 str
。
答:
function out_str = cellstr_joiner(in_cell, delim)
c=in_cell;
L=length(c);
str=[];
for i=1:L
if i~=L
str=[str,c{1,i}];
str=[str,delim];
else
str=[str,c{1,i}];
end
end
out_str=str;
end
如果是顺逻辑的话,难的地方在于如何处理delim中的引号。
(2)另外一道题目是计算勾股定理的,我试了试sqrt和^0.5的时间区别,发现sqrt还是稍微快一点的。