Problem 55. Counting Sequence
Given a vector x, find the "counting sequence" y.
A counting sequence is formed by "counting" the entries in a given sequence.
For example, the sequence
x = 5, 5, 2, 1, 1, 1, 1, 3
can be read as
Two 5's, one 2, four 1's, one 3
which translates to
y = 2, 5, 1, 2, 4, 1, 1, 3
So y is the counting sequence for x.
For this problem, all elements in the sequences x and y will be in the range from 1 to 9.
Solution Stats
Problem Comments
-
13 Comments
wow, last test case is so fun!
This only took me a few minutes until I saw Case 5 haha! Fun and mind boggling
I’m pretty new to programming and took on the cody counting sequence problem. My sample code is below. I wasn’t going for the most compact code and wanted the code to be generic to solve any size input vector X. My code does work, but when I submit it in the problem section, I get an error running the test scripts? Not a wrong result, just errors. Comments - suggestions? Yes, I know the codes messy. lol.
x =[ 5, 5, 2, 1,1, 1,1,3];
n = numel(x);
seqy = zeros(2*n,1);
seqpos=1;
seqcount=1;
i=1;
while i < n ;
if x(i) == x( i+1);
seqcount = seqcount+1;
else
seqy(seqpos)=seqcount;
seqpos = seqpos+1 ;
seqy(seqpos) = x(i);
seqpos = seqpos+1;
seqcount =1;
end
i = i+1;
end
seqy(seqpos) = seqcount;
seqy(seqpos+1)=x(i);
seqy;
c =numel(seqy);
h = 1;
p =0;
while h < c ;
if seqy(h) ~= 0;
else
p=h;
b =trimdata(seqy,p-1);
break
end
h= h+1;
end
y = b;
y % final result
Solution Comments
Show commentsProblem Recent Solvers2064
Suggested Problems
-
3338 Solvers
-
1665 Solvers
-
Project Euler: Problem 2, Sum of even Fibonacci
2497 Solvers
-
477 Solvers
-
Rounding off numbers to n decimals
4773 Solvers
More from this Author96
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!