Cody

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

Solution 2750632

Submitted on 28 Jul 2020 by Are Mjaavatten
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) k = 1; for R = 1:size(X,1) for C = 1:size(X,2) if X(R,C) == 1 k = k+1; search(R,C); end end end for n = 2:k s(n) = sum(X == n,'all'); end try y = max(s); catch y = 0; end function search(R,C) for i = R-1:R+1 for j = C-1:C+1 try if X(i,j) == 1 X(i,j) = k; search(i,j); end end end end end end %This code written by profile_id 3934907 '

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_correct = 1; assert(isequal(largest_pit(x),y_correct))

5   Pass
x = [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 1 0 0 1 0 0 0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 1 0 0 0 0 1 0 0 0 1 1 1 0 0 0 0 0 0 1 0 1 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 1 0 1 1 0 1 0 0 1 0 0 0 0 0 0 0 0]; assert(isequal(largest_pit(x),27))

10   Pass
x = zeros(14); assert(isequal(largest_pit(x),0))

11   Pass
x = [ 1 0 0 0 0 0 1 0 0 0 1 1 0 0 0 1 0 0 0 1 1 1 0 0 0 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 0 0 1 1 0 0 1 1 1 1 1 1 0 0 1 1 1 1 1 0 1 1 1 0 1 0 1 0 1 1 0 1 0 0 1 1 1 1 1 1 1 0 1 0 1 0 1 0 0 0 1 1 1 0 0 0 1 1 1 1 0 1 0 1 1 0 0 1 1 1 0 1 1 0 1 0 0 1 1 1 1 0 0 0 1 0 1 1 0 1 0 1 0 0 1 1 1 0 0 1 0 0 1 1 0 1 1 0 0 0 1 0 0 0 0 1 0 0 1 1 1 0 1 0 1 0 1 1 1 1 1 0 1 0 1 1 1 0 1 0 0 0 1 0 1 1 0 0 1 0 1 1 1 1 1 0 1 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1 0 1 1 0 1 0 1 1 1 0 1 1 0 0 0 1 1 0 0 1 1 1 0 1 0 0 1 0 0 1 1 1 0 1 1 0 1 1 1 0 1 0 1 1 1 1 1 0 0 1 1 0 1 0 1 1 0 1 1 1 1 0 1 0 1 1 1 1 0 0 1 1 0 1 0]; assert(isequal(largest_pit(x),150))

12   Pass
x = repmat([1 1 1 0;1 0 1 0;1 1 1 0; 0 0 0 0],4,5); assert(isequal(largest_pit(x),8))

13   Pass
x = [0 1 0 1 0 0 0 1 1 0 0 1 1 0 0 0]; assert(isequal(largest_pit(x),3))

14   Pass
x = [ 0 1 0 0 0 1 0 0 0 1 1 1 1 0 1 1 1 1 0 0 1 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 1 0 0 0 0 1 1 1 1 0 1 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