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

16 次查看(过去 30 天)
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 个评论
Anand H Lokapur
Anand H Lokapur 2020-3-23
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 .

请先登录,再进行评论。

回答(6 个)

David Hill
David Hill 2020-3-23
function ans = longrun(a)
find(vertcat(1,diff(a(:)),1));
a(ans(diff(ans)==max(diff(ans))));
end
  7 个评论
John D'Errico
John D'Errico 2020-6-22
编辑:John D'Errico 2020-6-22
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.

请先登录,再进行评论。


Peeyush Pankaj
Peeyush Pankaj 2020-10-28
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 个评论
aziz muhammed akkaya
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.

请先登录,再进行评论。


Ciro Bermudez
Ciro Bermudez 2020-12-8
编辑:Ciro Bermudez 2020-12-8
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 2021-2-27
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

Agam Sharma
Agam Sharma 2022-6-11
count=zeros(1,length(a))
for i=2:length(a)
if(a(i)==a(i-1))
count(i)=count(i-1)+1
end
end
[m e]=max(count)
a(e)

Kurt
Kurt 2023-8-30
I found I didn't need the "vertcat" function.
Here is my code inside the function with Comments to explain the logic behind the "diff" & "find" functions.
% Convert the Vector or Matrix into a single column Vector to simplify indexing and Consecutive Run Counting
b = a(:);
% Compare the values of the next adjacent element to the current element, the original vector length is reduced by 1; Zeros "flag" where the same Consecutive Number starting location is, the next nonzero value where it stops
c = diff(b);
% Surround the Difference Calculation Vector with nonzero elements. The original Vector length is now increased by 1
d = [1;c;1];
% Locate the Indices of the nonzero elements using the "find" function. The first & last indices are the ones that were added to "wrap" the vector in nonzero elements.
f = find(d);
% Count the occurance of each vector element in order of it's appearance. Values > 1 indicate a consecutive "run" but not neccessarily the most. Summing all these element values will give you the original Vector length.
g = diff(f);
% Determine the most frequent reoccurance(s), "mode" function isn't enough, there could be multiple numbers reoccuring for the same amount of times.
h = max(g);
% Retrieve the starting indices for where the most element values occur in the original vector
k = f(g==h);
% Return the Element Values that have the Longest Run of Consecutive Numbers which matches the input style
val = a(k);

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by