CS 367 Assignment 3 Solution 1. Suppose I have a method int num(char c) that takes a character representing a digit and returns its corresponding int. For example, num('0') returns 0, num('3') returns 3, etc. This method breaks if it is passed any character that is not a digit. Write me a method int number(String s) that takes a multi-digit numeral, uses num() and recursion, and returns the numeral's corresponding int value. You do not need to handle decimals, negative numbers, or being passed any strings containing non-digit characters (though you should handle empty strings). Two of possible solutions : int number(String s) { if (s.length() == 0) { // O(1) return 0; //? throw new NumberFormatException(); } // base case if (s.length() == 1) { // O(1) return num(s.charAt(0)); } // recursive case else { return num(s.charAt(0)) * Math.pow(10, s.length() -1) + number(s.substring(1)); // O(1) } } (or) int number(String s){ if (s.length() == 0) { return 0; // ? throw new NumberFormatException(); } // base case if (s.length() == 1) { // O(1) return num(s.charAt(0)); } // recursive case else { return num(s.charAt(s.length() - 1)) + // O(1) 10*number(s.substring(0, s.length()-1)); } } _______________________________________________________________ 2. Write a recurrence relation for your number() method. Solve for a closed form of your relation, being sure to check your answer. Say what big-O runtime your answer represents, and explain in words why this runtime makes sense for your method. T(0) = 1 T(1) = 1 T(N) = 1 + T(N-1) T(2) = 1 + T(1) = 2 T(3) = 1 + T(2) = 3 . . . T(N) = N Check: T(N) = 1 + T(N-1) N ?= 1 + (N - 1) N ?= 1 + N - 1 N = N This represents a big-O runtime of O(N) where N is the length of the String passed to the method. This makes sense because there is a recursive call for each character in the String till the base case and it returns after some constant time operations. _______________________________________________________________ 3. T(0) = 0 T(1) = 1 T(N) = 1 + 2*T(N - 1) T(2) = 1 + 2*T(1) = 3 T(3) = 1 + 2*T(2) = 7 T(4) = 1 + 2*T(3) = 15 . . T(N) = 2^N - 1 Check: T(N) = 1 + 2*T(N-1) 2^N - 1 ?= 1 + 2*(2^(N-1) - 1) 2^N - 1 ?= 1 + 2*(2^(N-1)) - 2 2^N - 1 ?= 2*(2^(N-1)) - 1 2^N - 1 = 2^N - 1 This corresponds to a big-O runtime of O(2^N) where N is the number passed to the method. This makes sense because the method has two recursive calls which grows exponentially as the method executes till the base case and it returns after a constant time operation. _______________________________________________________________ 4. public int compareTo(Height other){ int tmp = feet.compareTo(other.feet); //O(1) if (tmp != 0) { //O(1) return tmp; } return inches.compareTo(other.inches); //O(1) } (or) public int compareTo(Height other) { if(feet == other.feet && inches == other.inches) //O(1) { return 0; } else if(feet < other.feet || (feet == other.feet && inches < other.inches)) //O(1) { return -1; } else { return 1; } } The runtime of the above method is constant time or O(1) since it has only comparisons and assignments which are constant time operations.