Problem 55. Counting Sequence
Solution Stats
Problem Comments
-
12 Comments
The problem does not define correct solution if same number is not in consecutive order, like x = [5 5 2 1 1 5 3]; Should it be three 5's, or two 5's then one 5? If it's an encoding problem the correct solution should be the last, right?
Typical DPCM encoding removes the DC and thus starts at 0, not 1. An 11% enhancement in encoding.
Good problem.
Most of the solutions with UNIQUE don't work when same number is not in consecutive order.
in the new test case 5, with x=[1 2 2 1], the test suite answer is [2 1 2 2] (two 1's, two 2's). I would expect the correct answer to be [1 1 2 2 1 1] (one 1, two 2's, one 1). I guess I am misinterpreting the question?
No. you are right. The test case 5 is wrong.
My mistake guys. Sorry about that. By the way, these comments are very helpful for adding (and fixing) bad test cases. Thanks for taking the time to comment.
Test case 5 answer is wrong. the correct answer is [2 1 2 2]. Change the test case 5 answer
test case 5 answer is wrong
Just to clarify: test case 5 isnt wrong.
wow, last test case is so fun!
This only took me a few minutes until I saw Case 5 haha! Fun and mind boggling
Solution Comments
-
1 Comment
yes!
-
1 Comment
function y = CountSeq(x)
ele=[];
cnt=[];
ele=[ele x(1)];
cnt=ones(1,20);
L=length(x);
k=1;
for i=2:L
if x(i)==x(i-1)
cnt(k)=cnt(k)+1;
else
ele=[ele x(i)];
k=k+1;
end
end
L1=length(ele);
y=zeros(1,2*L1);
for i=1:L1
y(2*i-1)=cnt(i);
y(2*i)=ele(i);
end
end
-
2 Comments
-
4 Comments
Smart !
Doesn't work with [1 2 2 1] for example.
Thanks for the additional test question!
Test case 5 answer is wrong. the correct answer is [2 1 2 2]. Change the test case 5 answer
i need the code part of 3rd test case ..
can anyone help
-
2 Comments
does not work if numbers have 2 digits , for example : if x = [1 5 8 9 15 14 8 15]
Yes, it only works for 1 to 9, but that is what the problem specified. It is a good solution.
-
1 Comment
Solutions like this look much more elegant to me than the regexp expressions; and although using 'ans' would shorten it slightly, who would do that in serious code?
Problem Recent Solvers1346
Suggested Problems
-
2877 Solvers
-
239 Solvers
-
Back to basics 12 - Input Arguments
546 Solvers
-
Rotate and display numbered tile
288 Solvers
-
Is this is a Tic Tac Toe X Win?
483 Solvers
More from this Author95
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!