-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
38 lines (34 loc) · 1.09 KB
/
Main.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
import java.util.*;
import java.lang.*;
class Solution {
/**
Given two positive integers a and b, return the even digits between a
and b, in ascending order.
For example:
generateIntegers(2, 8) => [2, 4, 6, 8]
generateIntegers(8, 2) => [2, 4, 6, 8]
generateIntegers(10, 14) => []
*/
public List<Integer> generateIntegers(int a, int b) {
int lower = Math.max(2, Math.min(a, b));
int upper = Math.min(8, Math.max(a, b));
List<Integer> result = new ArrayList<>();
for (int i = lower; i < upper; i += 2) {
result.add(i);
}
return result;
}
}
public class Main {
public static void main(String[] args) {
Solution s = new Solution();
List<Boolean> correct = Arrays.asList(
s.generateIntegers(2, 8).equals(Arrays.asList(2, 4, 6, 8)),
s.generateIntegers(8, 2).equals(Arrays.asList(2, 4, 6, 8)),
s.generateIntegers(10, 14).equals(List.of())
);
if (correct.contains(false)) {
throw new AssertionError();
}
}
}