Problem 672. Longest run of consecutive numbers
Given a vector a, find the number(s) that is/are repeated consecutively most often. For example, if you have
a = [1 2 2 2 1 3 2 1 4 5 1]
The answer would be 2, because it shows up three consecutive times.
If your vector is a row vector, your output should be a row vector. If your input is a column vector, your output should be a column vector. You can assume there are no Inf or NaN in the input. Super (albeit non-scored) bonus points if you get a solution that works with these, though.
Solution Stats
Problem Comments
-
41 Comments
I don't understand why for test suite 4 :
a=[0 1 1 1 0 2 2 0 1 1 1 0];
y_correct = [1 1];
I expect y_correct = [1 1 1]
The idea seems to be to return the element that occurs in the longest run, or all such elements in case of a tie. In case 4 there are two longest runs, both with element 1.
Thanks Tim for the explanation . So as the vector [1 1 1] appears twice in test 4 with the unique value 1 , the result must be twice the unqiue value -> [ 1 1]. ok ok thanks again
Nice
I have this array as I counted the times it repeats. However, can someone give me a command to pull out the max values with the number to the left? Any advise? The first value in column on the right top doesn't matter as it should always be 1. I try the max command it only shows the max value of 3...:
1 0
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
Nice
Great problem. Thank you.
Good one
Nice problem
Is there a way to test output without submitting?
I really liked this one
This was fun.
The problem should specify that if the same number repeats the longest run of consecutive numbers more than once, it must be repeated in the output.
Great stuff!
I like this problem, it very fun
Should be more specific about the expected output, the solution checkers expect the output of numbers to be listed in the order of when they occurred in the sequence and also if the same number repeats in the sequence (with the largest run again) it needs to be repeated in the output. Both of these criteria broke my solution.
that was the first matlab problem i solved by myself, i'm so excited:)
Nice problem!
to be honest, though I solved the problem ,I didn't quite understand how to improve my code. I mean, the top of the solutions seemed quite strange to me .why by doing that the size would be smaller?
Hi 昱树 何,
In Matlab Cody, size of a solution is calculated by various factors, one of them being the number of variables used. However, the top solution in this problem (and in many others as well) exploit the use of regexp to gain a lower solution size. This was done because earlier the scoring was based on your solution size, which has now been changed to a fixed 10 points.
Hope this helps.
took a while doing this but overall good question. I got stuck in the last test case.
Good problem
hm, I don't think the assertion in test 4 is intuitively correct. The problem asks for the number(s) that have the longest consecutive repetitions in the input vector. So there can be multiple numbers having the longest runs (of equal size), but if all those numbers are the same, as in [1 1 2 1 1], surely the answer is "1", not "1 and 1".
I can only imagine that's what the implementation turned out to produce and instead of fixing the implementation to fit the problem, the tests was introduced to check for exactly that output ;)
Which is fine, you'll always have clients in real life that say they want something and when you roll it out they meant something different, it just bothers me that my nice solution doesn't work because of this ?
The problem specifically states that column and row vectors must be supported, but then doesn't have a test for this. I'd suggest changing at least one of the tests to use a column vector.
Or maybe add one test where it is asserted that `longrun(a) == transpose(longrun(transpose(a)))`
Nice one
nice
Tricky!
Repeated consecutively makes the problem more difficult otherwise, we can use unique and histcounts to solve this quickly.
Not as difficult as it seems.
Managed to finally get the solution. Took some thought...
In my opinion a bad description of the task.
There is no information, that the order of apperance has to be correct and also not, that if a number comes more than one time with same occurace, you have to output them this times.
good problem
Test 7 is not correct. There are no *consecutively repeated* numbers in the sequence, as clearly stated in the task.
@Marcos the test is correct insofar as that "repeated consecutively" also covers the case where a number appears just once in a row.
test 6 ... y_correct = [3 2]' >> transpose...... is making it harder as i've check test7 now.. if i ouput transpose it'll crease itssues.. how could i do it!! Damn.. it's not a hard problem.. to start with.. if it was in python, c or C++ but here... it's getting clever.. man..*_*
function val=longrun(a)
% Given vector
% a = [1 2 2 2 1 3 2 1 4 5 1];
maxn = 1;
skip = 2;
i = 1;
sz = size(a);
if sz(1)==1
a = [a,max(a)+1];
elseif sz(2) == 1
a = [a;max(a)+1];
end
max1 = -(max(a)+1);
while i < length(a)
for j = skip:length(a)
if a(j) ~= a(i)
skip = j+1;
% maxn = max(maxn, j-i);
if (j-i>maxn)
max1 = a(i);
elseif (j-i==maxn)
if (i~=1)
max1 = [max1, a(i)];
else
max1 = a(i);
end
end
maxn = max(maxn, j-i);
i = j;
break;
end
end
end
val = max1;
if sz(2) == 1
val = val'
end
end
Nice problem
The requirements for this problem are unclear and / or the tests are poor. If there are 2 equal lengths of the same number, e.g. [ 2 2 2 1 1 2 2 2 3 4 5], the expected result from the test is [2 2]. Why not just [2]?
Or if the input is [ 3 3 3 2 2 2 1 5 4] and the routine returns [2 3], why is this wrong? The test expects output of [3 2]...
@Andy The problem posed is basically this: in a given vector, for each run of identical numbers, identify the length of said run; then remove all runs of less than the maximal length, and replace all remaining runs with a single occurrence of the number they repeat.
I finally tackled the last problem!
I’m so proud of myself! I know my programming skills still have room for improvement, but I just had to share this amazing feeling with everyone. What a fantastic moment!
Ultimately solved it. I know that reduced size of the code is rewarded on Cody, but I prioritized readability and code with comments that explains what is logically happening. I didn't rely on diff or vertcat because I wanted to understand the basics of what the code should be doing.
Solution Comments
Show comments
Problem Recent Solvers5562
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!