└── Drill03.java /Drill03.java: -------------------------------------------------------------------------------- 1 | 2 | public class Drill03 { 3 | 4 | // takes a string and two characters as parameters 5 | // returns a string that is the same as the passed in string 6 | // except that all occurrences of the first character are replaced 7 | // with the second 8 | public static String replaceAll(String s, char from, char to) { 9 | if (s.length() == 0) { 10 | return ""; 11 | } 12 | else { 13 | String string = s.substring(1); 14 | char check = s.charAt(0); 15 | if (check == from) { 16 | return to + replaceAll(string, from, to); 17 | } 18 | else { 19 | return check + replaceAll(string, from, to); 20 | } 21 | } 22 | } 23 | 24 | // takes an integer as a parameter 25 | // returns true if the digits are in sorted order ascending, 26 | // false otherwise 27 | public static boolean digitsSorted(int x) { 28 | if (x < 0) { 29 | x = Math.abs(x); 30 | } 31 | if (x == 0) { 32 | return true; 33 | } 34 | else { 35 | int check = x % 10; 36 | int integer = x / 10; 37 | if (check >= integer % 10) { 38 | return digitsSorted(integer); 39 | } 40 | else { 41 | return false; 42 | } 43 | } 44 | } 45 | 46 | /* 47 | * Write a recursive function which returns the input string 48 | * but with adjacent duplicate char- acters removed. Do not use 49 | * any String functions other than .charAt(), .length(), .isEmpty() 50 | * and .substring(). Do not use any loops. We recommend you use a 51 | * helper function so we have provided the helper function header 52 | * below 53 | */ 54 | public static String removeAdjacentDuplicateChars(String s) { 55 | if (s.length() == 0) { 56 | return ""; 57 | } 58 | else { 59 | int len = s.length(); 60 | String result = removeHelper(s, len - 1); 61 | return result; 62 | } 63 | } 64 | // Note that the helper function is 'private' since no other code 65 | // outside of this file needs to call this method. 66 | private static String removeHelper(String s, int index) { 67 | if (index == 0) { 68 | return s; 69 | } 70 | else { 71 | char cur = s.charAt(index); 72 | char prev = s.charAt(index - 1); 73 | String result = s.substring(0, index); 74 | if (cur == prev) { 75 | return removeHelper(result, index - 1); 76 | } 77 | else { 78 | return removeHelper(result, index - 1) + cur; 79 | } 80 | } 81 | } 82 | 83 | /* 84 | * Write a recursive function that returns the number of occurrences 85 | * of an integer ’n’ inside of an array of integers. You may not use 86 | * loops or any array functions. You may use array.length to determine 87 | * the length of the array. We recommend you use a helper similar to the 88 | * one above. It would be useful if your helper function kept track of 89 | * which index in the array you are currently checking. 90 | */ 91 | public static int countOccurrences(int[] arr, int n) { 92 | if (arr.length == 0) { 93 | return 0; 94 | } 95 | else { 96 | int len = arr.length; 97 | int result = countOccurrencesHelper(arr, n, len - 1); 98 | return result; 99 | } 100 | } 101 | 102 | private static int countOccurrencesHelper(int[] arr, int n, int index) { 103 | if (index == -1) { 104 | return 0; 105 | } 106 | else { 107 | if (arr[index] == n) { 108 | return countOccurrencesHelper(arr, n, index - 1) + 1; 109 | } 110 | else { 111 | return countOccurrencesHelper(arr, n, index - 1); 112 | } 113 | } 114 | } 115 | } 116 | --------------------------------------------------------------------------------