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';
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.
This would be the replication of a single built-in function.
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!
747 Solvers
Make a run-length companion vector
453 Solvers
Cell Counting: How Many Draws?
254 Solvers
Is this triangle right-angled?
1920 Solvers
502 Solvers