Given a string such as
s = '011110010000000100010111'
find the length of the longest string of consecutive 1's. In this example, the answer would be 4.
Example:
Input x = '110100111' Output y is 3
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
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 :)
They are characters not doubles!
function y = lengthOnes(x)
a = [];
idx = [1,find(x=='0') + 1,length(x)+2];
le = length(idx);
a(1:le-1) = idx(2:le)-idx(1:le-1)-1;
y = max(a);
end
no regexp is needed actually
Cool
Solved on iPhone
Any suggestions on how to improve more ?
How can i improve the code?
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?
any way of improving this algorithm?
when y is empty vector, max(y) is not returning zero which is making my code complicated
It runs well in my Matlab program.
How much time it takes in your MATLAB? There is a limit of 50 seconds for Cody
probably the most inefficient code ever written :)
I'm loving it
efficient idea
I know it's not very efficient, but I sort of liked an idea behind this loop. Hence shared.
Great solution that's easy to understand adn translatable to future use.
extremely unfamiliar with "regexp" function results in a extremely stupid code...
hmmmmm.... ;-) nice
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).
finally a proper use of regexp in Cody
can you explain your code?
Swap the first and last columns
12787 Solvers
934 Solvers
634 Solvers
Project Euler: Problem 9, Pythagorean numbers
254 Solvers
Number of digits in an integer
320 Solvers