Cody

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

Solution 3666288

Submitted on 15 Nov 2020 by A C
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 L = largest_pit(x) %%% Code % detect the ones by find the rows and columns [r,c] = find(x==1); % make a loop to find the first one [lenr, lenc] = size(x); A = x; % mark 1 to 0 B = x; % number pits if isempty(r) else row = r(1); col = c(1); stor = []; lenrc = []; count = 0; end %%% while A has ones in the matrix loop while any(find(A==1))==1 count = count+1; A(row,col) = 0; B(row,col) = count; stor = [row,col]; stor1 = stor; storr = stor(:,1); storc = stor(:,2); storr1 = stor(:,1); storc1 = stor(:,2); lenrc = length(storr); II = 1; while ~isempty(stor1) row = storr1(II); col = storc1(II); % TOP if row - 1 > 0 ck = A(row-1,col) == 1; if ck == 1 stor = [stor; row-1, col]; end end % BOTTOM if row + 1 <= lenr ck = A(row+1,col) == 1; if ck == 1 stor = [stor; row+1, col]; end end % RIGHT if col + 1 <= lenc ck = A(row,col+1) == 1; if ck == 1 stor = [stor; row, col+1]; end end % LEFT if col - 1 > 0 ck = A(row,col-1) == 1; if ck == 1 stor = [stor; row, col-1]; end end % TOP RIGHT CORNER if row - 1 > 0 & col - 1 > 0 ck = A(row-1,col-1) == 1; if ck == 1 stor = [stor; row-1, col-1]; end end % TOP LEFT CORNER if row - 1 > 0 & col + 1 <= lenc ck = A(row-1,col+1) == 1; if ck == 1 stor = [stor; row-1, col+1]; end end % BOTTOM RIGHT CORNER if row + 1 <= lenr & col + 1 <= lenc ck = A(row+1,col+1) == 1; if ck == 1 stor = [stor; row+1, col+1]; end end % BOTTOM LEFT CORNER if row + 1 <= lenr & col - 1 > 0 ck = A(row+1,col-1) == 1; if ck == 1 stor = [stor; row+1, col-1]; end end if II < numel(stor1(:,1)) II=II+1; elseif isempty(stor) & II >= numel(stor1(:,1)) break else II = 1; stor1 = stor; storr = stor(:,1); storc = stor(:,2); storr1 = stor(:,1); storc1 = stor(:,2); lenrc = length(storr); for I = 1:numel(storr) A(storr(I),storc(I)) = 0; B(storr(I),storc(I)) = count; end stor = []; end end if any(find(A==1)) == 1 [r,c] = find(A==1); row = r(1); col = c(1); end end % how many pits are there on the flat plate pits = max(max(B)) % find the largest pit b = 0; for III = 1:pits d = numel(find(B==III)); if d > b L = d; b = d; end end if b == 0 L = 0; end L end %This code written by profile_id 9486485 '

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

pits = 1 L = 1

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

pits = 0 L = 0

4   Pass
x = [1 0 1 0 1]; y_correct = 1; assert(isequal(largest_pit(x),y_correct))

pits = 3 L = 1

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 0 0 0 0 0 0 0 1 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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 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))

pits = 24 L = 27

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

pits = 0 L = 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))

pits = 4 L = 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))

pits = 20 L = 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))

pits = 3 L = 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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