// Helene Martin, CSE 142 // I mentioned that array problems are my favorite final exam problems because // they bring all we've learned together and often include interesting // algorithmic complexity. // These are all examples of 15-point array questions (not the 10 point harder one) import java.util.*; public class ArrayPractice { public static void main(String[] args) { int[] a = {6, 3, 2}; System.out.println(Arrays.toString(delta(a))); System.out.println(isUnique(a)); String[] b = {"a", "b", "c", "d"}; swapPairs(b); System.out.println(Arrays.toString(b)); System.out.println(Arrays.toString(runningSum(a))); } // 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: {3, 5, 8, -2, 6} // 6 3 2 3 // 6 -3 3 -1 2 1 3 // i i*2 // 0 -> 0 // 1 -> 2 // 2 -> 4 public static int[] delta(int[] a) { int[] result = new int[a.length * 2 - 1]; for (int i = 0; i < a.length - 1; i++) { int diff = a[i + 1] - a[i]; result[i * 2] = a[i]; result[i * 2 + 1] = diff; } result[result.length - 1] = a[a.length - 1]; return result; } // Write a static method called runningSum that takes an array of integers // as a parameter and that returns a new array that contains the running // sum of the numbers in the array (where the i-th value of the new array // is the sum of the first i values of the original array) public static int[] runningSum(int[] a) { int[] sums = new int[a.length]; if (a.length > 0) { // careful about empty cases! sums[0] = a[0]; // fencepost for (int i = 1; i < a.length; i++) { sums[i] = a[i] + sums[i - 1]; } } return sums; } // 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. // 4 2 45 1 23 1 2 67 --> false 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]) { // as soon as two match, can't all be unique return false; } } } // we ONLY know that everything was unique once we've checked all // values against all others. return true; } // 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; } } }