Cody

Problem 45498. Trace the path of a harmful chemical in an ecological network

Solution 2942203

Submitted on 13 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('trace_chemical.m') assert(isempty(strfind(filetext, 'rand'))) assert(isempty(strfind(filetext, 'fileread'))) assert(isempty(strfind(filetext, 'assert'))) assert(isempty(strfind(filetext, 'echo')))

filetext = 'function y = trace_chemical(P,S) y = S; % Initiate y as the input site S N = length(P); % Loop to determine impacted sites % Stop if the path goes back to a currently impacted site or all the sites % are involved. ii = 2; while ii <= N if ismember(P(S),y) return; else y = [y P(S)]; S = P(S); end ii = ii+1; end end %This code written by profile_id 7191923 '

2   Pass
P = [3 1 5 2 4]; ans = [2 1 3 5 4]; assert(isequal(trace_chemical(P,2),ans))

3   Pass
P = [3 1 5 2 4]; ans = [1 3 5 4 2]; assert(isequal(trace_chemical(P,1),ans))

4   Pass
P = [2 5 8 6 10 9 3 4 7 1 ]; ans = [8 4 6 9 7 3 ]; assert(isequal(trace_chemical(P,8),ans))

5   Pass
P = [36 15 70 23 1 60 35 13 19 41 48 20 49 44 68 29 9 61 51 38 ... 32 2 40 8 72 39 26 28 67 69 42 76 66 34 47 46 11 77 71 64 ... 4 37 80 18 74 52 7 33 58 50 27 3 45 55 65 43 21 54 31 6 ... 24 63 57 30 12 78 22 75 14 10 16 17 81 53 59 25 56 5 73 62 ... 79 ]; ans = [60 6 ]; assert(isequal(trace_chemical(P,60),ans))

6   Pass
P = [8 45 12 53 33 15 29 39 40 21 9 26 32 58 20 43 54 17 48 55 ... 5 49 37 57 16 46 36 10 34 6 38 50 11 27 22 42 19 4 13 47 ... 52 2 23 25 35 24 30 56 44 41 28 14 51 31 7 3 1 18 ]; ans = [35 22 49 44 25 16 43 23 37 19 48 56 3 12 26 46 24 57 1 8 ... 39 13 32 50 41 52 14 58 18 17 54 31 38 4 53 51 28 10 21 5 ... 33 11 9 40 47 30 6 15 20 55 7 29 34 27 36 42 2 45 ]; assert(isequal(trace_chemical(P,35),ans))

7   Pass
P = [9 28 7 42 18 16 30 17 24 20 41 29 13 15 44 8 27 23 12 19 ... 21 32 40 49 11 47 14 25 35 36 46 38 33 45 34 4 43 48 31 5 ... 3 10 26 39 37 1 22 6 2 ]; ans = [24 49 2 28 25 11 41 3 7 30 36 4 42 10 20 19 12 29 35 34 ... 45 37 43 26 47 22 32 38 48 6 16 8 17 27 14 15 44 39 31 46 ... 1 9 ]; assert(isequal(trace_chemical(P,24),ans))

8   Pass
P = [39 27 32 17 22 3 21 8 4 16 45 37 40 2 19 11 51 36 50 43 ... 13 44 12 30 48 28 42 35 10 14 5 38 15 9 20 18 6 26 31 24 ... 23 41 34 1 46 7 47 33 49 29 25 ]; ans = [11 45 46 7 21 13 40 24 30 14 2 27 42 41 23 12 37 6 3 32 ... 38 26 28 35 20 43 34 9 4 17 51 25 48 33 15 19 50 29 10 16 ]; assert(isequal(trace_chemical(P,11),ans))

9   Pass
P = [19 10 17 9 18 7 13 14 20 21 5 3 6 16 8 12 15 11 2 4 1]; ans = [4 9 20 ]; assert(isequal(trace_chemical(P,4),ans))

10   Pass
P = [12 1 5 66 26 29 64 68 2 33 38 41 55 8 18 49 27 47 22 50 ... 35 24 16 13 60 34 46 36 6 56 67 30 42 48 19 37 63 57 11 17 ... 40 59 15 23 45 32 61 44 53 31 28 10 62 9 21 7 52 14 39 51 ... 58 65 54 43 3 4 20 25 ]; ans = [26 34 48 44 23 16 49 53 62 65 3 5 ]; assert(isequal(trace_chemical(P,26),ans))

11   Pass
P = [65 8 29 66 49 72 61 38 18 33 58 62 67 40 20 27 46 1 5 6 ... 14 75 82 74 23 37 54 22 78 41 4 53 13 47 57 51 17 69 77 71 ... 64 35 25 44 21 70 19 76 36 10 81 42 60 79 28 31 9 48 3 56 ... 24 59 11 50 7 26 30 16 52 39 15 2 68 73 34 80 32 12 63 55 ... 45 43 ]; ans = [17 46 70 39 77 32 53 60 56 31 4 66 26 37 ]; assert(isequal(trace_chemical(P,17),ans))

12   Pass
P = [1 17 15 6 13 33 28 36 4 22 44 23 32 40 26 12 41 30 8 34 ... 37 14 21 5 9 10 29 3 35 38 11 43 31 16 19 27 24 45 39 7 ... 2 42 20 18 25 ]; ans = [14 40 7 28 3 15 26 10 22 ]; assert(isequal(trace_chemical(P,14),ans))

13   Pass
P = [1 17 21 15 6 24 13 5 4 18 2 9 3 29 10 28 12 23 11 25 ... 20 19 8 16 27 26 7 22 14 ]; ans = [10 18 23 8 5 6 24 16 28 22 19 11 2 17 12 9 4 15 ]; assert(isequal(trace_chemical(P,10),ans))

14   Pass
P = [6 2 3 4 1 7 5 ]; ans = [3 ]; assert(isequal(trace_chemical(P,3),ans))

15   Pass
P = [11 19 15 23 14 10 3 4 25 7 24 1 18 26 6 16 17 20 12 2 ... 13 22 9 21 8 5 ]; ans = [16 ]; assert(isequal(trace_chemical(P,16),ans))

16   Pass
P = [20 16 5 9 30 28 8 24 14 15 23 4 29 11 22 19 26 17 25 6 ... 27 18 3 2 1 13 31 12 10 21 7 ]; ans = [11 23 3 5 30 21 27 31 7 8 24 2 16 19 25 1 20 6 28 12 ... 4 9 14 ]; assert(isequal(trace_chemical(P,11),ans))

17   Pass
P = [1 18 16 8 15 21 27 22 23 17 26 19 3 4 9 11 7 6 29 2 ... 12 25 24 5 14 28 20 10 13 ]; ans = [22 25 14 4 8 ]; assert(isequal(trace_chemical(P,22),ans))

18   Pass
P = [38 48 42 87 57 89 92 12 20 62 59 51 26 29 45 55 10 71 44 69 ... 34 60 30 77 53 11 54 14 23 15 22 43 49 13 41 5 47 91 68 37 ... 9 3 76 31 85 33 40 1 63 70 18 8 17 35 90 36 24 83 94 21 ... 73 27 61 78 39 82 64 93 66 6 67 84 56 80 19 50 95 2 75 46 ... 74 16 81 72 4 25 86 32 28 52 65 58 88 7 79 ]; ans = [54 35 41 9 20 69 66 82 16 55 90 52 8 12 51 18 71 67 64 78 ... 2 48 1 38 91 65 39 68 93 88 32 43 76 50 70 6 89 28 14 29 ... 23 30 15 45 85 4 87 86 25 53 17 10 62 27 ]; assert(isequal(trace_chemical(P,54),ans))

19   Pass
P = [69 48 11 21 80 50 75 64 41 54 23 82 61 45 25 10 74 63 72 8 ... 15 81 42 60 59 65 35 37 70 33 76 24 36 49 56 18 38 6 44 39 ... 4 17 52 51 32 43 1 46 55 73 34 28 58 31 68 29 67 22 66 12 ... 53 5 16 77 19 7 13 26 57 79 3 47 71 40 14 30 2 20 62 9 ... 78 27 ]; ans = [27 35 56 29 70 79 62 5 80 9 41 4 21 15 25 59 66 7 75 14 ... 45 32 24 60 12 82 ]; assert(isequal(trace_chemical(P,27),ans))

20   Pass
P = [78 59 84 70 19 82 34 69 29 92 6 51 52 28 10 32 31 33 4 73 ... 24 89 99 68 64 47 46 95 94 21 53 44 62 26 93 91 58 55 98 79 ... 11 35 48 40 22 66 87 80 63 43 12 97 13 17 67 20 1 85 60 81 ... 25 50 88 49 96 90 76 83 36 15 75 23 41 86 39 9 8 54 7 61 ... 2 72 45 38 16 71 56 37 3 14 27 74 5 57 65 18 42 30 77 ]; ans = [85 16 32 44 40 79 7 34 26 47 87 56 20 73 41 11 6 82 72 23 ... 99 77 8 69 36 91 27 46 66 90 14 28 95 65 96 18 33 62 50 43 ... 48 80 61 25 64 49 63 88 37 58 ]; assert(isequal(trace_chemical(P,85),ans))

21   Pass
P = [86 17 25 63 38 72 9 64 56 10 7 26 43 28 36 40 24 71 41 22 ... 27 80 21 1 54 84 42 11 60 73 6 46 78 50 67 66 20 23 77 74 ... 57 44 85 75 16 13 47 14 29 48 19 58 2 39 81 83 59 33 49 61 ... 69 53 3 35 8 55 32 18 31 30 12 51 34 65 87 62 5 52 15 45 ... 4 68 82 76 70 37 79 ]; ans = [36 66 55 81 4 63 3 25 54 39 77 5 38 23 21 27 42 44 75 87 ... 79 15 ]; assert(isequal(trace_chemical(P,36),ans))

22   Pass
P = [25 7 4 6 16 30 24 28 9 3 31 13 10 23 2 26 29 8 5 20 ... 18 27 21 11 22 17 12 19 1 15 14 ]; ans = [21 18 8 28 19 5 16 26 17 29 1 25 22 27 12 13 10 3 4 6 ... 30 15 2 7 24 11 31 14 23 ]; assert(isequal(trace_chemical(P,21),ans))

23   Pass
P = [30 31 13 37 59 49 28 25 65 61 22 8 43 80 64 18 2 74 46 14 ... 85 12 62 5 55 67 48 42 78 83 47 15 79 89 34 68 54 90 3 44 ... 72 40 21 24 60 82 35 50 66 11 41 77 75 7 16 27 73 10 76 71 ... 33 56 39 53 38 19 36 84 69 81 23 87 9 51 70 86 88 1 26 57 ... 63 20 4 52 29 45 58 6 17 32 ]; ans = [78 1 30 83 4 37 54 7 28 42 40 44 24 5 59 76 86 45 60 71 ... 23 62 56 27 48 50 11 22 12 8 25 55 16 18 74 51 41 72 87 58 ... 10 61 33 79 26 67 36 68 84 52 77 88 6 49 66 19 46 82 20 14 ... 80 57 73 9 65 38 90 32 15 64 53 75 70 81 63 39 3 13 43 21 ... 85 29 ]; assert(isequal(trace_chemical(P,78),ans))