Two more tests:
1. Input x = [-4 -2 0 -4 -1 0]
Output y is -1.
2. Input x = [-9 -3 0 0 -5 0 -2 -1]
Output y is -2.
Good comment, Gaik. A lot of solutions (including mine) will break with two consecutive zeros.
Why: "Input x = [-9 -3 0 0 -5 0 -2 -1] Output y is -2."???
Output should be 0. It doesn't say anywhere that the number adjacent to a zero must be nonzero!
This was challenging
function y = nearZero(x)
x=[-inf,x,-inf];
a=x(find(x==0)-1);
b=x(find(x==0)+1);
y=max([a,b])
end
u can avoid loops friend..
function y = nearZero(x)
y = [];
a=[];
k=1;
L=length(x);
for i=1:L
if x(i)==0
if i==1
a(k)=x(i+1);
k=k+1;
elseif i==L
a(k)=x(i-1);
k=k+1;
else
a(k)=x(i-1);
a(k+1)=x(i+1);
k=k+2;
end
end
end
[y,index]=max(a);
end
function y = nearZero(x)
x = [min(x(x~=0)),x,min(x(x~=0))];
index = find(x == 0);
y = max([x(index-1),x(index+1)]);
end
function y = nearZero(x)
x=[-inf,x,-inf];
zeroindex=find(~x);
zeroindexl=zeroindex-1;
zeroindexr=zeroindex+1;
y=max([x(zeroindexl),x(zeroindexr)])
end
Very interesting problem
I understand how this function works, but how did you know to use [1 0 1] for the convolution?
Quiet hard, but satisfying! :)
It is really cool to see someone has the exact chain of thoughts as you .... !!!
I believe size(x,2) is equivalent to length(x). Great solution though! Made me realize where I put in too much work.
Because of 2 bytes space charactor
Damn that's a lot higher score than I was expecting.
Trying to emulate imdilate.
Hi everyone. I realized this code is very large and cannot be run on this website because of the many loops that it has to go through. It works on matlab so if anyone could help me keep the general form of it but have it cut down in the number of loops that would be great. Thanks.
the imdilate function apparently can't be used... this is a clumsy clumsy workaround
This one was hard. Tried my best but couldn't get it shorter
I think the test suite needs a test for a = [0 0 0 0]. I passed with the above code but would not pass this case.
I ended up with the same logic, I'm glad I wasn't the only one.
It's a shame this is basically a verbose reworking of the imdilate function (which is not accessible for this challenge)
well, this looks a bit.. long?
I suppose overt is more at it. This will catch multiple consecutive zeros. I suppose I could have put comments too. Not to mention it's clocking 10^-6sec. Why do these Cody problems go by size isn't speed incredibly more important in computational programming.
regexp should really be banned from problems not related with word handling...
smart solution.
This solution gives the wrong value if the largest value is at the end of the vector. For example for x = [1 0 2 0 3] it returns 2 instead of 3.
Thanks for the tip!
It can handle adjacent zeroes.
No way that the leading solution is the least complex one! The rating function is questionable.
Fails when the max value output should be negative and the last value of the input vector is zero, e.g. a = [5 4 -1 0 -2 0 -5 0]. Should return -1, but returns 0.
this should work
Nice! It's quite clever to put the front and rear in pairs.
Peter's comment about Solution 20356 appears to apply here as well.
1160 Solvers
Remove the polynomials that have positive real elements of their roots.
630 Solvers
235 Solvers
352 Solvers
708 Solvers
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!