package test4; import java.util.List; public class Test4B { public Test4B() { // empty be design } /** * Returns the string formed by counting starting at from * up to and including to in increments of 1. Each * number in the string is separated from the next number by a * comma and a space. * *
	 * countTo(1, 1)    returns the string equal to "1"
	 * countTo(5, 6)    returns the string equal to "5, 6"
	 * countTo(10, 12)  returns the string equal to "10, 11, 12"
	 * 
* * @param from * the number to count from * @param to * the number to count to * @return the string formed by repeating the specified string n times * @pre. from is less than or equal to to */ public static String countTo(int from, int to) { if (from == to) { return "" + to; } return from + ", " + countTo(from + 1, to); } /** * Given two non-null strings of equal length, returns the string formed by * alternating characters from s and t. For example: * *
     * zipper("", "") returns the empty string
     * zipper("a", "1") returns the string "a1"
     * zipper("abcd", "1234") returns the string "a1b2c3d4"
     * 
* * @param s a string of length n * @param t a second string of length n * @return the string of length 2n formed by alternating characters * of s and t */ public static String zipper(String s, String t) { if (s.isEmpty()) { return ""; } String pre = "" + s.charAt(0) + t.charAt(0); String post = zipper(s.substring(1), t.substring(1)); return pre + post; } /** * Removes all duplicated strings from a sorted list of strings * maintaining the sorted order of the strings. If the list contains unique * strings then the list remains unchanged. For example, if t is the list * ["a", "b", "c", "d"] then unique(t) would do nothing. If t is the list * ["w", "w", "w", "x", "y", "y", "z"] then unique(t) would modify t so that * it became the list ["w", "x", "y", "z"]. * * @param t * a sorted list of strings that the method modifies * so that it contains no duplicated strings (and the list remains in * sorted order). */ public static void unique(List t) { if (t.size() < 2) { return; } String first = t.get(0); String second = t.get(1); if (first.equals(second)) { t.remove(0); Test4B.unique(t); } Test4B.unique(t.subList(1, t.size())); } /** * Returns the index of the element equal to zero in the * specified sorted list. The list t may be sorted in either * ascending or descending order. The list t is not modified * by this method. * *

* The computational complexity of the method is O(log n) where * n is the size of the list t. * * @param t a sorted list * @return the index of the element equal to zero * @pre. the list is sorted in either ascending or descending order * and the list contains zero */ public static int findZero(List t) { int i = t.size() / 2; int mid = t.get(i); if (mid == 0) { return i; } int first = t.get(0); if (first >= 0 && mid < 0 || first <= 0 && mid > 0) { return findZero(t.subList(0, i)); } return i + 1 + findZero(t.subList(i + 1, t.size())); } }