def minimum_index(seq):\n if len(seq) == 0:\n raise ValueError(\"Cannot get the minimum value index from an empty sequence\")\n min_idx = 0\n for i in range(1, len(seq)):\n if a[i] < a[min_idx]:\n min_idx = i\n return min_idx<\/code><\/pre>\n\n\n\nA coworker has prepared functions that will perform the tests and validate return values. Finish the implementation of classes to provide data and expected results for the tests.<\/p>\n\n\n\n
Complete the following methods.<\/p>\n\n\n\n
In the class TestDataEmptyArray<\/code>:<\/p>\n\n\n\nget_array()<\/code> returns an empty array<\/li><\/ul>\n\n\n\nIn the class TestDataUniqueValues<\/code>:<\/p>\n\n\n\nget_array()<\/code> returns an array of size at least 2 with all unique elements<\/li>get_expected_result()<\/code> returns the expected minimum value index for this array<\/li><\/ul>\n\n\n\nIn the class TestDataExactlyTwoDifferentMinimums<\/code>:<\/p>\n\n\n\nget_array()<\/code> returns an array where the minimum value occurs at exactly 2 indices<\/li>get_expected_result()<\/code> returns the expected index<\/li><\/ul>\n\n\n\nTake a look at the code template to see the exact implementation of functions that your colleague already implemented.<\/p>\n\n\n\n
Note: The arrays are indexed from 0<\/strong>.<\/p>\n\n\n\n<\/span>Solution – Day 27: Testing<\/strong><\/span><\/h2>\n\n\n\n<\/span>Java<\/strong><\/span><\/h3>\n\n\n\nimport java.util.*;\n\npublic class Solution {\n public static int minimum_index(int[] seq) {\n if (seq.length == 0) {\n throw new IllegalArgumentException(\"Cannot get the minimum value index from an empty sequence\");\n }\n int min_idx = 0;\n for (int i = 1; i < seq.length; ++i) {\n if (seq[i] < seq[min_idx]) {\n min_idx = i;\n }\n }\n return min_idx;\n }\n static class TestDataEmptyArray {\n public static int[] get_array() {\n int arr[]={};\n return arr;\n }\n }\n static class TestDataUniqueValues {\n public static int[] get_array() {\n \/\/ complete this function\n int arr[]={1,2,3,4,5};\n return arr;\n }\n public static int get_expected_result() {\n \/\/ complete this function\n return 0;\n }\n }\n static class TestDataExactlyTwoDifferentMinimums {\n public static int[] get_array() {\n \/\/ complete this function\n int arr[]={1,3,1};\n return arr;\n }\n public static int get_expected_result() {\n \/\/ complete this function\n return 0;\n }\n }\n public static void TestWithEmptyArray() {\n try {\n int[] seq = TestDataEmptyArray.get_array();\n int result = minimum_index(seq);\n } catch (IllegalArgumentException e) {\n return;\n }\n throw new AssertionError(\"Exception wasn't thrown as expected\");\n }\n public static void TestWithUniqueValues() {\n int[] seq = TestDataUniqueValues.get_array();\n if (seq.length < 2) {\n throw new AssertionError(\"less than 2 elements in the array\");\n }\n Integer[] tmp = new Integer[seq.length];\n for (int i = 0; i < seq.length; ++i) {\n tmp[i] = Integer.valueOf(seq[i]);\n }\n if (!((new LinkedHashSet<Integer>(Arrays.asList(tmp))).size() == seq.length)) {\n throw new AssertionError(\"not all values are unique\");\n }\n int expected_result = TestDataUniqueValues.get_expected_result();\n int result = minimum_index(seq);\n if (result != expected_result) {\n throw new AssertionError(\"result is different than the expected result\");\n }\n }\n public static void TestWithExactlyTwoDifferentMinimums() {\n int[] seq = TestDataExactlyTwoDifferentMinimums.get_array();\n if (seq.length < 2) {\n throw new AssertionError(\"less than 2 elements in the array\");\n }\n int[] tmp = seq.clone();\n Arrays.sort(tmp);\n if (!(tmp[0] == tmp[1] && (tmp.length == 2 || tmp[1] < tmp[2]))) {\n throw new AssertionError(\"there are not exactly two minimums in the array\");\n }\n int expected_result = TestDataExactlyTwoDifferentMinimums.get_expected_result();\n int result = minimum_index(seq);\n if (result != expected_result) {\n throw new AssertionError(\"result is different than the expected result\");\n }\n }\n public static void main(String[] args) {\n TestWithEmptyArray();\n TestWithUniqueValues();\n TestWithExactlyTwoDifferentMinimums();\n System.out.println(\"OK\");\n }\n}<\/pre>\n\n\n\n<\/span>Python<\/strong><\/span><\/h3>\n\n\n\ndef minimum_index(seq):\n if len(seq) == 0:\n raise ValueError(\"Cannot get the minimum value index from an empty sequence\")\n min_idx = 0\n for i in range(1, len(seq)):\n if seq[i] < seq[min_idx]:\n min_idx = i\n return min_idx\n\nclass TestDataEmptyArray(object):\n \n @staticmethod\n def get_array():\n return []\n # complete this function\nclass TestDataUniqueValues(object):\n data = []\n for i in range(5):\n data.append(i)\n data[::-1] \n @staticmethod\n def get_array():\n return TestDataUniqueValues.data\n @staticmethod\n def get_expected_result():\n data = TestDataUniqueValues.get_array()\n return data.index(min(data))\n # complete this function\n\nclass TestDataExactlyTwoDifferentMinimums(object):\n data = []\n for i in range(5):\n data.append(i)\n data[::-1] \n data.insert(0,0)\n \n @staticmethod\n def get_array():\n return TestDataExactlyTwoDifferentMinimums.data\n @staticmethod\n def get_expected_result():\n data = TestDataExactlyTwoDifferentMinimums.get_array()\n return data.index(min(data))\n # complete this function\n\n\ndef TestWithEmptyArray():\n try:\n seq = TestDataEmptyArray.get_array()\n result = minimum_index(seq)\n except ValueError as e:\n pass\n else:\n assert False\n\n\ndef TestWithUniqueValues():\n seq = TestDataUniqueValues.get_array()\n assert len(seq) >= 2\n\n assert len(list(set(seq))) == len(seq)\n\n expected_result = TestDataUniqueValues.get_expected_result()\n result = minimum_index(seq)\n assert result == expected_result\n\n\ndef TestiWithExactyTwoDifferentMinimums():\n seq = TestDataExactlyTwoDifferentMinimums.get_array()\n assert len(seq) >= 2\n tmp = sorted(seq)\n assert tmp[0] == tmp[1] and (len(tmp) == 2 or tmp[1] < tmp[2])\n\n expected_result = TestDataExactlyTwoDifferentMinimums.get_expected_result()\n result = minimum_index(seq)\n assert result == expected_result\n\nTestWithEmptyArray()\nTestWithUniqueValues()\nTestiWithExactyTwoDifferentMinimums()\nprint(\"OK\")<\/pre>\n\n\n\n