There has been a function added in 2013a for this!!
I make it in R2013a,but I don't know the R2012b or other previous versions have this function.
Most of the solutions consider that delim=1char
i don't know
tricky one!!
Regards.
May I ask.
I have tried in Matlab, all three test cases were validated but I got incorrect from cody.
Here is what inside the function :
%%%%%%%%%%
out_str=char(strjoin(in_cell,delim));
l=out_str==' ';
out_str(find((l(2:end)+l(1:end-1))==2))=[];
%%%%%%%%%
Thanks and regards.
function out_str=cellstr_joiner(in_cell, delim)
L=strlength(in_cell);
n=length(L);
summ=L(1);
m=length(delim);
for k=2:n-1
L(k)=summ+L(k);
summ=L(k);
end
L(end)=[];
out_str=cell2mat(in_cell);
for i=1:length(L)
out_str=strcat(out_str(1:L(i)),abs(delim),out_str(L(i)+1:end));
L=L+m;
end
end
who can tell me why my code is wrong.thank you
nicequestion
Hello everyone :)
Just a quick one: the code I provide below here work fine in Matlab (r2019b), so each solution matches the one I'm asked to provide. However, when evaluating the code on Cody, I get "Assertion failed".
Can anyone please explain me if it's a matter of conversion or some details I am missing?
KR, Andrea
out_str=[];
for jj=1:length(in_cell)
tmp=char(in_cell(jj));
c=[tmp,delim];
if jj==1
out_str=c;
else
out_str=[out_str,c];
end
end
I used strjoin(in_string,' ')
passed the first two, third one failed.
function out_str = cellstr_joiner(in_cell, delim)
out_str = strjoin(in_cell,delim);
end
Super easy one
Hint:- Use strjoin to solve this problem
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
This would be the replication of a single built-in function.
out_str = strjoin(in_cell, delim);
or
lx = length(in_cell)*2-1;
a = repmat({delim},1,lx);
a(1:2:lx) = in_cell;
out_str = cell2mat(a);
only
out_str=strjoin(in_cell,delim);
work properly
funny!
Assertion fails in Cody even though the assertion passes in MATLAB R2014b. Does any one know why?
Hi. The assertion could only have passed outside of Cody if the wrong "correct solution" were specified for comparison, and this would happen if you copy the HTML-rendered text — due to a kind of bug in the way the solution vector is displayed on the Cody webpage (better: copy from the HTML _source code_). -----
The correct solution has delimiters (here shown as ^ for clarity) as in 'this^one^^has^ ^some tricky^stuff', whereas your code produces 'this^one^has^some tricky^stuff', so you don't get the correct result between "one" and "has", nor between "has" and "some".
I think that the test 2 must be checked.
I compared the test 2 correct answer with the program output using strcmp, and it gave 1 as output.( This was before introducing the program lines which also take into consideration the single space given in 5th element of in_cell in test 2)
Anyone, who has solved or has an idea how to solve, Please let me know what is the error in my program.
Thanks, By the way, program might be a bit lengthy.
THE TEST CASE 2 OUTPUT YOU HAVE DISPLAYED IS WRONG
CORRECT IT.
There are two issues here:
(1) The HTML display in Cody automatically only displays a single space. Therefore, while the solution *vector* _specified_ as the answer to Test 2 in Cody is correct, the *text* that is _displayed_ is not. You can confirm that the correct information was specified by viewing the HTML source. It is unfortunate that this is not displayed correctly. Of course, any results should match the solution, not the incorrect display.
(2) Your candidate solution fails because it is incorrect. Even if it matches the HTML-parsed display, it doesn't match the true specified solution vector. To make this apparent, imagine if delim='^' (no other changes). The correct solution to Test 2 would then be 'this^one^^has^ ^some tricky^stuff', whereas your output would be 'this^one^has^ ^some tricky^stuff' (provided in Solution 1280078). In other words, you needed a double space between "one" and "has".
My solution is:
function out_str = cellstr_joiner(in_cell, delim)
y=in_cell;
y(find(cellfun(@isempty,cellfun(@strtrim,y,'UniformOutput',false))))=[];
out_str = strjoin(y);
out_str(find(out_str==' '))=delim;
end
On Matlab all tests pass but on the cody website the second test fail!
This code is working as expected in MATLAB command window. can i know why it is failed here
why deblank?
ok, is not really elegant not short, but it's correct. why doesn't it pass the test?
The problem is that your output is a cell string and not a character array. You can get a char array by adding out_str=char(out_str); in the end, or work with char arrays from the beginning by using in_cell{1} and in_cell{i} instead of in_cell(1) and in_cell(i).
I think there is something wrong here, this solution seems to work.
nice, I had never heard of this one!
I had tried this, but kept getting: "strjoin not found" on my workstation :(
So I had to work around it.
The best one! (without STRJOIN)
answers just the test suite...
There are better ways to cheat!
Simple and easy to understand, and works in versions before strjoin appeared.
Ugh, for loop!
printf works wonders
Wow! Brilliant!
9875 Solvers
Find relatively common elements in matrix rows
865 Solvers
Project Euler: Problem 5, Smallest multiple
397 Solvers
511 Solvers
262 Solvers
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!