Problem 15. Find the longest sequence of 1's in a binary sequence.
Solution Stats
Problem Comments

8 Comments
Nice and useful !
good one
Nice one, didn't think it was possible to do in one line initially...
agree, useful
good problem.should think to solve
nice range of solutions
interesting
great problem
Solution Comments

2 Comments
noice
cool

1 Comment
function y = lengthOnes(x)
y = double(x == '1');
L = numel(y);
A = tril(ones(L,L));
C = mat2cell(A,ones(1,L),L);
D = cellfun(@(c) conv(c,y),C,"UniformOutput",false);
E = cellfun(@(c) sum(c),C);
F = cellfun(@(c) max(c),D);
R = max(E(F == E));
if(isempty(R))
y = 0;
else
y = R;
end
end

2 Comments
function y = lengthOnes(x)
cnt=0;
a=[];
L=strlength(x);
for i=1:L
if x(i)=='1'
cnt=cnt+1;
else
cnt=0;
end
a(i)=cnt;
end
[y,index]=max(a);
end
Excellent work man :)

1 Comment
They are characters not doubles!

3 Comments
function y = lengthOnes(x)
a = [];
idx = [1,find(x=='0') + 1,length(x)+2];
le = length(idx);
a(1:le1) = idx(2:le)idx(1:le1)1;
y = max(a);
end
no regexp is needed actually
Cool

2 Comments
Solved on iPhone
@Martin solved on Nokia X202

1 Comment
Any suggestions on how to improve more ?

1 Comment
How can i improve the code?

1 Comment
a=regexp(x,'0+','split')
a=a{max(length(a))}
y=length(a)
With this code, every test passes except 4th one. Why is it so?

1 Comment
any way of improving this algorithm?
when y is empty vector, max(y) is not returning zero which is making my code complicated

3 Comments
It runs well in my Matlab program.
How much time it takes in your MATLAB? There is a limit of 50 seconds for Cody
Aditya Jain
Do you have a source for that time limit info?

1 Comment
probably the most inefficient code ever written :)

3 Comments
I'm loving it
efficient idea
cool!

1 Comment
I know it's not very efficient, but I sort of liked an idea behind this loop. Hence shared.

1 Comment
Great solution that's easy to understand adn translatable to future use.

1 Comment
extremely unfamiliar with "regexp" function results in a extremely stupid code...

1 Comment
hmmmmm.... ;) nice

2 Comments
dynamic expression! this is super cool, i didnt know matlab had this and that you could write a matlab code that's so hard to read. Why the 49?
Honestly, right now I find it hard to read as well. 49 is the decimal representation of the ASCII character '1' (type +'1' in the Matlab Command Window).

1 Comment
finally a proper use of regexp in Cody

1 Comment
can you explain your code?
Problem Recent Solvers4513
Suggested Problems

given 3 sides, find area of this triangle
730 Solvers

246 Solvers

Rotate and display numbered tile
288 Solvers

689 Solvers

4353 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!