// Helene Martin, CSE 142 // As I said in lecture, array problems are my favorite final exam problems because // they bring all we've learned together and often include interesting // algorithmic challenges. // These are all examples of 15-point array questions. I MAY include a harder 10 point // array question. import java.util.*; public class ArrayPractice { public static void main(String[] args) { int[] numbers = {3, 8, 6}; System.out.println(Arrays.toString(delta(numbers))); System.out.println(isUnique(numbers)); int[] numbers2 = {2, 4, 6, 4, 2}; System.out.println(isUnique(numbers2)); // We didn't do this one but here's an example // of an array problem in which we modify the contents // of the array and don't need to return. String[] b = {"a", "b", "c", "d"}; swapPairs(b); System.out.println(Arrays.toString(b)); } // Write a static method named delta that accepts an array of integers as a // parameter and returns a new array formed by inserting between each pair of // values the difference between those values. For example, given this array: // int[] numbers = {3, 8, 6}; The call of delta(numbers) should return the following // array (new elements are bolded): {3, 5, 8, -2, 6} // {4, 5} --> {4, 1, 5} length * 2 - 1 (minus 1 is because last value doesn't have pair) // 0 1 2 // 3, 8, 6 // 0 1 2 3 4 // 3, 5, 8, -2, 6 // values copied from index i to index i * 2 // deltas inserted at index i * 2 + 1 (one after the first in the pair) public static int[] delta(int[] a) { int[] result = new int[a.length * 2 - 1]; // we need to stop one early to compensate for the a[i + 1] in the loop body // run with the jGRASP debugger if you're not sure why for (int i = 0; i < a.length - 1; i++) { // copy over result[i * 2] = a[i]; // calculate delta result[i * 2 + 1] = a[i + 1] - a[i]; } // don't forget the fencepost result[result.length - 1] = a[a.length - 1]; return result; } // Write a static method called isUnique that takes an array of integers as // a parameter and that returns true if the values in the list are unique // and that returns false otherwise. The values in the list are considered // unique if there is no pair of values that are equal. // 3 2 3 5 1 3 42 as soon as there's a match, false! // 3 5 2 7 8 compare all matches before true public static boolean isUnique(int[] a) { for (int i = 0; i < a.length - 1; i++) { for (int j = i + 1; j < a.length; j++) { if (a[i] == a[j]) { return false; // found a match --> not unique } } } return true; // have to look at EVERY pair before we know values are unique } // Write a method named swapPairs that accepts an array of strings as a parameter // and switches the order of values in a pairwise fashion. Your method should switch // the order of the first two values, then switch the order of the next two, switch // the order of the next two, and so on. If there are an odd number of values, the final // element is not moved. // a b c d --> b a d c public static void swapPairs(String[] a) { // be sure to simulate the code in your mind! // we need to go up by twos to make sure we're swapping pairwise // we need to stop 1 short of the end to work for the odd-length case for (int i = 0; i < a.length - 1; i = i + 2) { // also ok: i += 2 String temp = a[i]; a[i] = a[i + 1]; a[i + 1] = temp; } } }