-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 28.java
103 lines (93 loc) · 3.1 KB
/
Day 28.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import java.util.*;
public class Solution {
public static int minimum_index(int[] seq) {
if (seq.length == 0) {
throw new IllegalArgumentException("Cannot get the minimum value index from an empty sequence");
}
int min_idx = 0;
for (int i = 1; i < seq.length; ++i) {
if (seq[i] < seq[min_idx]) {
min_idx = i;
}
}
return min_idx;
}
static class TestDataEmptyArray
{
public static int [] get_array()
{
return new int[0];
}
}
static class TestDataUniqueValues
{
public static int [] get_array()
{
return new int[] {1,2,3,4,5,6,7,8};
}
public static int get_expected_result()
{
return 0;
}
}
static class TestDataExactlyTwoDifferentMinimums
{
public static int [] get_array()
{
return new int[] {1,2,3,4,5,6,7,8,1,2,3,4,5,6,7};
}
public static int get_expected_result()
{
return 0;
}
}
public static void TestWithEmptyArray() {
try {
int[] seq = TestDataEmptyArray.get_array();
int result = minimum_index(seq);
} catch (IllegalArgumentException e) {
return;
}
throw new AssertionError("Exception wasn't thrown as expected");
}
public static void TestWithUniqueValues() {
int[] seq = TestDataUniqueValues.get_array();
if (seq.length < 2) {
throw new AssertionError("less than 2 elements in the array");
}
Integer[] tmp = new Integer[seq.length];
for (int i = 0; i < seq.length; ++i) {
tmp[i] = Integer.valueOf(seq[i]);
}
if (!((new LinkedHashSet<Integer>(Arrays.asList(tmp))).size() == seq.length)) {
throw new AssertionError("not all values are unique");
}
int expected_result = TestDataUniqueValues.get_expected_result();
int result = minimum_index(seq);
if (result != expected_result) {
throw new AssertionError("result is different than the expected result");
}
}
public static void TestWithExactlyTwoDifferentMinimums() {
int[] seq = TestDataExactlyTwoDifferentMinimums.get_array();
if (seq.length < 2) {
throw new AssertionError("less than 2 elements in the array");
}
int[] tmp = seq.clone();
Arrays.sort(tmp);
if (!(tmp[0] == tmp[1] && (tmp.length == 2 || tmp[1] < tmp[2]))) {
throw new AssertionError("there are not exactly two minimums in the array");
}
int expected_result = TestDataExactlyTwoDifferentMinimums.get_expected_result();
int result = minimum_index(seq);
if (result != expected_result) {
throw new AssertionError("result is different than the expected result");
}
}
public static void main(String[] args) {
TestWithEmptyArray();
TestWithUniqueValues();
TestWithExactlyTwoDifferentMinimums();
System.out.println("OK");
}
}