CODY Contest 2020 Basics on Cell Arrays 全10题

第一题 Problem 41. 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';

 

将cell种的字符串拼接为一个完整的字符串,子字符串间用 delim隔开。

遍历,然后用[a,b]连接字符串a和b即可,注意:使用strcat连接' '失效。

function out_str = cellstr_joiner(in_cell, delim)
    out_str='';
    for i=1:length(in_cell)
        if i~=1
            out_str=[out_str,delim,in_cell{i}];
        else
            out_str=[out_str,in_cell{i}];
        end
    end
end

第二题 Problem 152. Create a cell array out of a struct

Create a cell array out of a (single) struct with the fieldname in the first column and the value in the second column:

in:

S.foo = 'hello';

S.bar = 3.14;

out:

{'foo', 'hello';

'bar', 3.14}

将结构体转为cell。

用到了fieldnames函数和getfield,前者返回结构体的所有变量名,以n*1大小的cell存储,n为变量数,后者根据结构体和字符串的变量名返回变量值。

function c = struct2namedCell(S)
    c=fieldnames(S);
    
    for i=1:length(fieldnames(S))
        c{i,2}=getfield(S,c{i,1});
    end
end

 第三题 Problem 380. Convert a numerical matrix into a cell array of strings

Given a numerical matrix, output a cell array of string.

For example:

if input = 1:3

output is {'1','2','3'}

which is a cell 1x3

将矩阵转为cell。

注意cell内的格式为字符串,所以先吧input的每个数转为字符串,然后调用num2cell即可。

function output = matrix2cell(input)
    input=string(input);
    output = num2cell(input);
end

第四题 Problem 967. Split a string into chunks of specified length

Given a string and a vector of integers, break the string into chunks whose lengths are given by the elements of the vector. Extra characters leftover at the end should be dropped. Example:

break_string('I seem to be having tremendous difficulty with my lifestyle',[4 1 5 22])

should return a 4x1 cell array:

ans =

'I se' 'e' 'm to ' 'be having tremendous d'

给定字符串和数值向量,返回一个n*1的cell,n为向量长度,cell的内容为依次从头裁剪的字符串内容,长度为向量值。截取1到k存入即可,k为当前值,题目中说为n*1,但是测试用例是1*n:

function y = break_string(s,b)
  for i=1:length(b)
    y{i}=s(1:b(i));
    s=s(b(i)+1:end);
  end
end

第五题 Problem 1899. Convert a Cell Array into an Array

Given a square cell array:

x = {'01', '56'; '234', '789'};

return a single character array:

y = '0123456789'

给定一个n*n的cell,将其列优先拼接为字符串,x{:}可列优先展开,[]可直接拼接。

function y = your_fcn_name(x)
  y = [x{:}];
end

第六题 Problem 1971. Remove element(s) from cell array

You can easily remove an element (or a column in any dimension) from a normal matrix, but assigning that value (or range) empty. For example

A = 1:10

A(5) = []

results in

1 2 3 4 6 7 8 9 10

You task is to find the shortest, elegant, way in Matlab to do the same for cell arrays. Regexp, eval, and other workarounds that trick mtree are considered stupid, and will not be appreciated.

将cell中的某些数删掉,根据他样例的意思, A = 1:10 A(5) = []表示的是删除第5个数,而不是删除值为5的数(虽然结果相同),而且他的测试样例中数字都是1到n的,即值和索引一致。并且作者说正则表达式等是非常stupid的。作者还要求最短,但是对cell某些元素直接置为空是无法达到效果的,参考Matlab中删除cell数组中的空元素的方法,先把指定元素置为空,然后删除空:

function x = remove_from_cell_array(x,to_remove)
  x(to_remove)=[];
  x(cellfun(@isempty,x))=[];
end

但是似乎 x(to_remove)=[];就可以啊。。。

第七题 Problem 1036. Cell Counting: How Many Draws?

You are given a cell array containing information about a number of soccer games. Each cell contains one of the following:

  • 'H', meaning the home team won
  • 'A', meaning the away team won
  • 'D', meaning a draw, or tie game

So if

games = {'D','D','A','H','D','H'}

then

draws = 3

cell中只含有三种字符,H A 和D ,分别代表主队赢、客队赢和平局,求平局数。

对于cell是不能直接通过games=='D'来判断的,所以可以对cell拼接为字符串,然后统计‘D’的个数,或者遍历cell。

function draws = how_many_draws(games)
  games=[games{:}];
  draws=sum(games=='D');
end

 第八题 Problem 43677. String Array Basics, Part 1: Convert Cell Array to String Array; No Missing Values

String array and cell array are two types of containers for storing pieces of data. In this problem, you will be given a cell array of character vectors. Your job is to convert the cell array to a string array, which stores the same pieces of text data.

To begin with, let's assume that there are no missing type values in the input cell array.

Example:

Input:

>> x = {'I','Love','MATLAB'}

x =

1×3 cell array

'I' 'Love' 'MATLAB'

 

Output:

>> y = strings(size(x));

>> [y{:}] = x{:}

y =

1×3 string array

"I" "Love" "MATLAB"

Note that the example shown above is not the best way to solve this problem. Try other approaches in order to achieve a leading score.

Related Problems in this series:

将cell转为字符串向量,其中字符串向量每个元素等于对应cell储存的字符串。

直接用string()进行转换就可以,

function y = cell2str(x)
  y = string(x);
end

测试样例给出了作者的转换方法:

function y = cell2str(x)
  y=strings(size(x));
  [y{:}] = x{:};
end

第九题 Problem 1755. Fix the last element of a cell array

Note: this is lifted directly from Puzzler for a Monday (on MATLAB Answers) by the cyclist.

----

Given a cell array of strings

A = {'MATLAB','HURRAY','SPARKLY','KITTENS','FUN'};

and a particular string value

B = 'KITTENS';

ensure that B is the last element of the cell array. If it isn't, move it to the end of A.

You cannot assume that B appears at all (in which case return A unchanged), but you can assume B does not appear more than once.

So in the example,

C = {'MATLAB','HURRAY','SPARKLY','FUN','KITTENS'};

如果B不在A中,那么不变,否则把B放在最后而其他相对顺序不变。鉴于之前的思想,可以先查找到所有与B相同的位置(假设存在多个和B相同的值,matlab中字符串相等必须使用strcmp),然后全部删除,再在删除后的尾部添加若干个B,数量为删除的数量。

function A = puzzler(A,B)
    find_index=[];
    j=0;
    for i=1:length(A)
        if strcmp(A{i},B)
            j=j+1;
            find_index(j)=i;
        end
    end
    A(find_index)=[];
    n=length(A);
    for i=1:length(find_index)
        A{i+n}=B;
    end
end

第十题 Problem 2300. Natural numbers in string form

Create a cell array of strings containing the first n natural numbers.  Slightly harder than it seems like it should be.

Example

>> y = naturalnumbers(4)

y =

'1' '2' '3' '4'

返回一个从1到n的cell,并且元素格式为字符串,题意中说考虑地复杂些,但是似乎考虑不出来。简单的话,直接列出1到n,然后转为字符串向量,再转为cell:

function x = naturalnumbers(x)
  x=num2cell(string(1:x));
end

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值