Cody

Problem 45432. Pitting corrosion on a metal plate: Find the largest pit

Solution 2690828

Submitted on 14 Jul 2020 by Ruihu Zhu
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test Status Code Input and Output
1   Pass
filetext = fileread('largest_pit.m') assert(isempty(strfind(filetext, 'rand'))) assert(isempty(strfind(filetext, 'fileread'))) assert(isempty(strfind(filetext, 'assert')))

filetext = 'function y = largest_pit(X) [row,col]=size(X); ind=1; counter(1,ind)=0; for i=1:row for j=1:col if X(i,j)==1 counter(1,ind)=counter(1,ind)+1; X(i,j)=2; k=1; position=[]; if i-1>0&&j-1>0&&X(i-1,j-1)==1 X(i-1,j-1)=2; position(1,k)=i-1; position(2,k)=j-1; k=k+1; counter(1,ind)=counter(1,ind)+1; end if i-1>0&&X(i-1,j)==1 X(i-1,j)=2; position(1,k)=i-1; position(2,k)=j; k=k+1; counter(1,ind)=counter(1,ind)+1; end if i-1>0&&j+1<=col&&X(i-1,j+1)==1 X(i-1,j+1)=2; position(1,k)=i-1; position(2,k)=j+1; k=k+1; counter(1,ind)=counter(1,ind)+1; end if j-1>0&&X(i,j-1)==1 X(i,j-1)=2; position(1,k)=i; position(2,k)=j-1; k=k+1; counter(1,ind)=counter(1,ind)+1; end if j+1<=col&&X(i,j+1)==1 X(i,j+1)=2; position(1,k)=i; position(2,k)=j+1; k=k+1; counter(1,ind)=counter(1,ind)+1; end if j-1>0&&i+1<=row&&X(i+1,j-1)==1 X(i+1,j-1)=2; position(1,k)=i+1; position(2,k)=j-1; k=k+1; counter(1,ind)=counter(1,ind)+1; end if i+1<=row&&X(i+1,j)==1 X(i+1,j)=2; position(1,k)=i+1; position(2,k)=j; k=k+1; counter(1,ind)=counter(1,ind)+1; end if i+1<=row&&j+1<=col&&X(i+1,j+1)==1 X(i+1,j+1)=2; position(1,k)=i+1; position(2,k)=j+1; k=k+1; counter(1,ind)=counter(1,ind)+1; end while isempty(position)==0 [~,m]=size(position); k=1; position_co=[]; for n=1:m if position(1,n)-1>0&&position(2,n)-1>0&&X(position(1,n)-1,position(2,n)-1)==1 X(position(1,n)-1,position(2,n)-1)=2; position_co(1,k)=position(1,n)-1; position_co(2,k)=position(2,n)-1; k=k+1; end if position(1,n)-1>0&&X(position(1,n)-1,position(2,n))==1 X(position(1,n)-1,position(2,n))=2; position_co(1,k)=position(1,n)-1; position_co(2,k)=position(2,n); k=k+1; end if position(1,n)-1>0&&position(2,n)+1<=col&&X(position(1,n)-1,position(2,n)+1)==1 X(position(1,n)-1,position(2,n)+1)=2; position_co(1,k)=position(1,n)-1; position_co(2,k)=position(2,n)+1; k=k+1; end if position(2,n)-1>0&&X(position(1,n),position(2,n)-1)==1 X(position(1,n),position(2,n)-1)=2; position_co(1,k)=position(1,n); position_co(2,k)=position(2,n)-1; k=k+1; end if position(2,n)+1<=col&&X(position(1,n),position(2,n)+1)==1 X(position(1,n),position(2,n)+1)=2; position_co(1,k)=position(1,n); position_co(2,k)=position(2,n)+1; k=k+1; end if position(2,n)-1>0&&position(1,n)+1<=row&&X(position(1,n)+1,position(2,n)-1)==1 X(position(1,n)+1,position(2,n)-1)=2; position_co(1,k)=position(1,n)+1; position_co(2,k)=position(2,n)-1; k=k+1; end if position(1,n)+1<=row&&X(position(1,n)+1,position(2,n))==1 X(position(1,n)+1,position(2,n))=2; position_co(1,k)=position(1,n)+1; position_co(2,k)=position(2,n); k=k+1; end if position(1,n)+1<=row&&position(2,n)+1<=col&&X(position(1,n)+1,position(2,n)+1)==1 X(position(1,n)+1,position(2,n)+1)=2; position_co(1,k)=position(1,n)+1; position_co(2,k)=position(2,n)+1; k=k+1; end end position=unique(position_co','rows')'; [~,a]=size(position); counter(1,ind)=counter(1,ind)+a; end end ind=ind+1; counter(1,ind)=0; end end y=max(counter); end %This code written by profile_id 13231766 '

2   Pass
x = 1; y_correct = 1; assert(isequal(largest_pit(x),y_correct))

3   Pass
x = 0; y_correct = 0; assert(isequal(largest_pit(x),y_correct))

4   Pass
x = [1 0 1 0 1]; y_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t

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

Start Hunting!