// 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))); int[] numbers2 = {9, 1, 3, -1}; System.out.println(Arrays.toString(delta(numbers2))); String[] pets = {"cat", "dog", "fish", "dog", "cow", "bird"}; String[] majors = {"amath", "info", "cse", "hcde", "ee"}; System.out.println(isUnique(pets)); System.out.println(isUnique(majors)); } // 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} // build up an example by hand: // {5, 3, 7, 2} --> {5, -2, 3, 4, 7, -5, 2} // numbers that just get copied over // old -> new // 0 -> 0 // 1 -> 2 // 2 -> 4 // i -> i * 2 // finding length of new array // 3 -> 5 // 4 -> 7 // len -> len * 2 - 1 public static int[] delta(int[] arr) { int[] result = new int[arr.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 < arr.length - 1; i++) { result[i * 2] = arr[i]; // copy over result[i * 2 + 1] = arr[i + 1] - arr[i]; // delta goes next to copy } // fencepost since there is one fewer pair than elements result[result.length - 1] = arr[arr.length - 1]; return result; } // Write a static method called isUnique that takes an array of Strings 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. // a i j k n k l -> false (as soon as there's a match, return) // a i r t u p o -> true (need to compare every pair of values) public static boolean isUnique(String[] arr) { for (int i = 0; i < arr.length - 1; i++) { for (int j = i + 1; j < arr.length; j++) { if (arr[i].equals(arr[j])) { return false; // found a match: not unique } } } return true; // can only return true after comparing every pair } // 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; } } }