Cody

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

Solution 2541210

Submitted on 13 Jun 2020 by Antoni Prus
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 = 'function y = count_pits(X) if ~any(X==1) y = 0 else n=size(X,1); m=size(X,2); cord = []; for a=1:n for b=1:m if X(a,b) == 1 cord = [cord; [a b]]; end end end con = []; for c=1:size(cord,1) same = max(abs(cord(c, 1)-cord(:,1)),abs(cord(c,2)-cord(:,2))) <= 1; con = [con same]; end cord = [cord zeros(size(cord,1),1)]; conLog = con == 1; n = 1; for d = 1:size(con,2) if max(cord(conLog(:,d),3)) == 0 cord(conLog(:,d),3) = n; n = n+1; elseif nnz(unique(cord(conLog(:,d),3))) <= 1 cord(d,3) = max(cord(conLog(:,d),3)); else un = unique(cord(conLog(:,d),3)) un = un(un~=0) for f = 1:length(un) cord((cord(:,3) == un(f)),3) =n; end cord(d, 3) = n; n= n+1; end end y = length(unique(cord(:,3))) end %This code written by profile_id 15735109 '

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

y = 1

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

y = 0

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

y = 3

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

y = 5

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

un = 0 6 12 un = 6 12 un = 0 9 13 un = 9 13 un = 0 15 16 un = 15 16 y = 25

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

un = 0 5 6 un = 5 6 y = 5

9   Pass
x = eye(20); assert(isequal(count_pits(x),1))

y = 1

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

y = 9

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

un = 0 1 2 un = 1 2 un = 13 16 un = 13 16 un = 0 17 18 un = 17 18 un = 0 15 20 un = 15 20 y = 21

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

un = 0 1 2 un = 1 2 un = 0 3 5 un = 3 5 un = 0 4 6 un = 4 6 y = 1

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

y = 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))

un = 0 2 5 un = 2 5 un = 3 4 un = 3 4 un = 0 6 7 un = 6 7 un = 0 8 10 un = 8 10 un = 0 19 22 un = 19 22 un = 21 26 un = 21 26 un = 24 27 un = 24 27 y = 15

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

un = 0 2 7 un = 2 7 un = 0 3 8 un = 3 8 un = 0 9 10 un = 9 10 un = 0 5 11 un = 5 11 un = 0 12 13 un = 12 13 un = 0 14 15 un = 14 15 un = 16 17 un = 16 17 un = 0 18 19 un = 18 19 un = 0 20 21 un = 20 21 un = 0 22 23 un = 22 23 un = 0 24 25 un = 24 25 un = 0 26 27 un = 26 27 un = 0 28 29 un = 28 29 un = 0 30 31 un = 30 31 un = 32 33 un = 32 33 y = 5

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

y = 30

### Community Treasure Hunt

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

Start Hunting!