MATLAB Answers

Help with Cody's 'Problem 672. Longest run of consecutive numbers', I wrote 'if a(i)==a(i+1)' and it says error

28 views (last 30 days)
N/A
N/A on 10 Jan 2020
Hi, I'm trying to solve this problem but I'm getting an error so far.
Problem:
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
What I've written so far (not done):
a = [1 2 2 2 1 3 2 1 4 5 1];
[x,y] = size(a);
counter = zeros(1,10);
if x == 1
for i=1:1:y
if a(i) == a(i+1)
counter(a(i)) = counter(a(i))+1
end
end
else
for i=1:1:x
if a(i) == a(i+1)
counter(a(i)) = counter(a(i))+1
end
end
end
But it says "error" in the line of "if a(i) == a(i+1)". I noticed that it creates a variable called "i" which value is 11, but it should create a vector from 1 to 11. What's wrong here?
I know my solution might not be in the right direction or something, but please don't tell me anything!
Thanks in advance
  8 Comments
Anand H Lokapur
Anand H Lokapur on 23 Mar 2020
could you please provide solution or the snippet of the main part, I have tried to figure this out but can't and now I really want to know how to .

Sign in to comment.

Answers (4)

David Hill
David Hill on 23 Mar 2020
function ans = longrun(a)
find(vertcat(1,diff(a(:)),1));
a(ans(diff(ans)==max(diff(ans))));
end
  7 Comments
John D'Errico
John D'Errico on 22 Jun 2020
The ans solution is a common one used to improve Cody scores. It is also terribly poor coding style in my opinion, because it tends to create hard to read and debug code, for no good reason except to gain an improvement in Cody score. But sadly, Cody encourages it. Personally, I think the Cody scoring algorithm should deprecate that coding style.

Sign in to comment.


Peeyush Pankaj
Peeyush Pankaj on 28 Oct 2020
While diff() approach gives you good scores, here's logic based solution for the ones who want to understand how it should be done
function val=longrun(a)
N=max(size(a,1),size(a,2));
count=1; max_count=1; reset=0; j=1;
for i=2:N
if a(i) == a(i-1)
count=count+1;
% Enter this if loop for the first set of continuos numbers
if reset==0
max_count=count;
val(reset+1)=a(i);
% Elseif condition for next set of continuos numbers, but matching the max_count with previously detected
elseif reset>0 & count==max_count
j=j+1;
val(j)=a(i)
elseif count>max_count
max_count=count; j=1;
val(j)=a(i);
end
elseif count>1
count=1; reset=reset+1
else
count=1;
end
end
% If there's no duplicate values in a row
if reset==0 & max_count==1
val=a
% For outputing as column vector
elseif size(a,1)>size(a,2)
val=val'
end
end
  1 Comment
aziz muhammed akkaya
aziz muhammed akkaya on 15 Mar 2021
Thank you for the code, it really helped me understand the logic. However, there is a subtle gap in the code. In the condition count>max_count if val is not emptied, it repeats the max number. For example in a case like this one a = [1 1 1 2 2 2 2 3 3 3], the answer will be [2 2]. So, in that condition val should be emptied somewhere before it is added the new value.
Again thank you for the code, it helped a lot.

Sign in to comment.


Ciro Bermudez
Ciro Bermudez on 8 Dec 2020
Edited: Ciro Bermudez on 8 Dec 2020
In my opinion It's a great problem, this is the solution I came up with
function val=longrun(a)
count = 1; max_count = 1; val = a(1); idx = 1;
for i = 2:length(a)
if a(i) == a(i-1)
count = count + 1;
else
count = 1;
end
if max_count == count
% if val(idx) ~= a(i)
idx = idx + 1;
val(idx) = a(i);
% end
end
if max_count < count
max_count = count;
val = a(i);
idx = 1;
end
end
if size(a,2) < size(a,1)
val = val.';
end
end
Something weird happens when you use the complete code, uncommenting the if statement, in my opinion gives a better solution, the down side is that it wouldn't pass Cody tests. Hopes it helps other people with ideas to solve this problem.

Hikmat Binyaminov
Hikmat Binyaminov on 27 Feb 2021
function val=longrun(a)
sz = size(a);
n = zeros(sz);
k = 1;
n(k)=1;
for i=2:length(a)
if a(i)-a(i-1) == 0
n(k)=n(k)+1;
else
k = k+1;
n(k)=1;
end
end
n=n(n~=0);
I = find(n==max(n));
for j=1:length(I)
val(j) = a(sum(n(1:I(j))));
end
if sz(1)~=1;
val=val';
end
end

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!