-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmatches.java
68 lines (51 loc) · 1.59 KB
/
matches.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package mumTestPackage;
import static org.junit.jupiter.api.Assertions.*;
import java.util.Arrays;
import org.junit.jupiter.api.Test;
class MumTest23 {
@Test
void test() {
assertEquals(1, matches(new int[] {1, 2, 3, -5, -5, 2, 3, 18}, new int[] {3, -2, 3}));
assertEquals(1, matches(new int[] {1, 2, 3, -5, -5, 2, 3, 18}, new int[] {2, 1, -1, -1, 2, 1}));
assertEquals(1, matches(new int[] {1, 2, 3, -5, -5, 2, 3, 18}, new int[] {2, 1, -2, 3}));
assertEquals(1, matches(new int[] {1, 2, 3, -5, -5, 2, 3, 18}, new int[] {1, 1, 1, -1, -1, 1, 1, 1}));
assertEquals(0, matches(new int[] {1, 2, 3, -5, -5, 2, 3, 18}, new int[] {4, -1, 3}));
}
int matches(int[ ] a, int[ ] p) {
if (a == null || a.length == 0 || p == null || p.length == 0) {
return 0;
}
int startSequence = 0;
int sumP = 0;
for (int i = 0; i < p.length; i++) {
int lengthSequence = Math.abs(p[i]);
sumP +=lengthSequence;
if(startSequence + lengthSequence > a.length || lengthSequence == 0)
return 0;
int[] sequences = Arrays.copyOfRange(a, startSequence, startSequence+lengthSequence);
boolean nPositive = isPositive(p[i]);
boolean checkPositive = checkPositiveArray(sequences);
if(nPositive != checkPositive)
return 0;
startSequence += lengthSequence;
}
if(sumP == a.length)
return 1;
else
return 0;
}
boolean checkPositiveArray(int[] a) {
boolean checkPositive = true;
for (int anA : a) {
if(anA <= 0)
checkPositive = false;
}
return checkPositive;
}
boolean isPositive(int n) {
if(n>0)
return true;
else
return false;
}
}