This example comes from Steve Eddins' blog: Learning Lessons from a One-Liner
Write a function that takes a list or array of numbers as input and return the largest number that is adjacent to a zero.
Example:
Input x = [1 5 3 0 2 7 0 8 9 1 0] Output y is 8
This problem was originally posed by Greg Wilson of Software Carpentry.
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
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
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 .... !!!
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.
332 Solvers
207 Solvers
Are all the three given point in the same line?
167 Solvers
163 Solvers
Spot the First Occurrence of 5
290 Solvers