Cody

Problem 45431. Pitting corrosion on a metal plate: Count the number of pits

Solution 2916712

Submitted on 6 Sep 2020 by Nanfei Cheng
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('count_pits.m') assert(isempty(strfind(filetext, 'rand'))) assert(isempty(strfind(filetext, 'fileread'))) assert(isempty(strfind(filetext, 'assert')))

filetext = 'function y = count_pits(X) y = 0; [r c] = find(X); % the row and col indecies for the corroded areas % Check if there is no corrosion if isempty(r) return; end % Store r and c in an unknown*2 matrix rc = [r c]; if r == 1 rc = [r' c']; end remove = rc(1,:); % Initiate a pit left = setdiff(rc,remove,'rows'); % The other areas except the pit flag1 = 1; % Initiate flag1 = 1 if left is not empty % Loop to count the pit nums while flag1 y = y + 1; flag2 = 1; % Initiate flag2 = 1 if % Pick out left elements belong to the dynamic pit remove while flag2 flag2 = 0; n_left = size(left,1); if ~n_left flag1 = 0; break; end % Check if left elements belong to the current pit remove for ii = 1:n_left n_remove = size(remove,1); d = abs(remove - repmat(left(ii,:),n_remove,1)); for jj = 1:n_remove if all(d(jj,:)<=1) flag2 = 1; remove = [remove; left(ii,:)]; left(ii,:) = [-1 -1]; break; end end end if ~flag2 % No left element belongs to the current pit remove remove = left(1,:); left = left(2:end,:); break; else % One or more left elements belongs to the current pit remove left = setdiff(left,[-1 -1],'rows'); end end end end %This code written by profile_id 7191923 '

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

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

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

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

13   Pass
x = repmat([0 1;1 0],5,7); assert(isequal(count_pits(x),1))

14   Pass
x = repmat([0 1;0 0],5,7); assert(isequal(count_pits(x),35))

15   Pass
x = [0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 1 0 1 1 0 0 1 0 1 1 0 0 0 0 1 0 1 0 1 0 0 1 1 0 1 0 1 1 0 0 0 0 1 0 0 0 0 0 0 0 1 0 1 0 1 1 0 0 1 0 0 0 1 1 1 0 1 0 0 0 0 0 1 0 0 1 1 0 0 0 1 1 0 0 0 1 1 0 0 0 0 0 0 0 1 1 0 1 1 1 0 0 0 1 1 0 0 0 0 0 0 0 0 1 0 1 0 0 0 1 0 0 0 0 0 0 1 1 1 0 0 0 1 0 0 0 0 0 1 1 1 0 1 0 0 0 0 0 1 0 0 0 1 0 1 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 1 0 0 1 1 0 0 0 1 0 0 1 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 1 1 1 0 0 0 1 0 0 1 1 0 0 1 0 0 1 0 0 1 0 0 0 1 0 0 1 0 0 0 0 1 1 0 0 0 0 1 0 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0 0 1 0 0 0 1 0 0 0 1 1 0 1 0 0 1 0]; assert(isequal(count_pits(x),15))

16   Pass
x = [0 0 1 1 1 0 1 0 0 1 0 0 0 0 0 0 0 0 1 0 1 0 1 1 0 0 0 0 0 0 1 0 0 1 0 1 1 1 1 0 1 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 1 0 1 0 0 1 0 0 1 0 0 0 0 0 0 0 1 0 1 1 1 0 0 1 1 0 0 0 0 1 0 0 0 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0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 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 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0 1 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 1 0 0 0 0 0 0 1 0 0 0 1 0 0 1 0 1 0 0 0 0 0 0 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 1 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 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 1 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 1 1 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]; assert(isequal(count_pits(x),30))

Suggested Problems

More from this Author19

Community Treasure Hunt

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

Start Hunting!