Anonymous avatar Anonymous committed 8098978

renamed phone2words.java to telephonewords.java

Comments (0)

Files changed (2)

src/Phone2Words.java

-import java.util.ArrayList;
-
-/**
- * A non-recursive way to find all combination of words from a
- * telephone number
- *
- * @author Absolute Noob
- *
- */
-public class Phone2Words {
-
-	private static final char[] TWO_CHARACTERS = { 'A', 'B', 'C' };
-	private static final char[] THREE_CHARACTERS = { 'D', 'E', 'F' };
-	private static final char[] FOUR_CHARACTERS = { 'G', 'H', 'I' };
-	private static final char[] FIVE_CHARACTERS = { 'J', 'K', 'L' };
-	private static final char[] SIX_CHARACTERS = { 'M', 'N', 'O' };
-	private static final char[] SEVEN_CHARACTERS = { 'P', 'R', 'S' };
-	private static final char[] EIGHT_CHARACTERS = { 'T', 'U', 'V' };
-	private static final char[] NINE_CHARACTERS = { 'W', 'X', 'Y' };
-
-	/**
-	 * get character from phone number digit
-	 *
-	 * @param phoneNumber
-	 *            phone number digit
-	 * @param place
-	 *            index of all possible character
-	 * @return character from the phone number digit
-	 */
-	private static char getCharKey(int phoneNumber, int place) {
-		char result = '?';
-		switch (phoneNumber) {
-		case 0:
-			result = '0';
-			break;
-		case 1:
-			result = '1';
-			break;
-		case 2:
-			result = Phone2Words.TWO_CHARACTERS[place];
-			break;
-		case 3:
-			result = Phone2Words.THREE_CHARACTERS[place];
-			break;
-		case 4:
-			result = Phone2Words.FOUR_CHARACTERS[place];
-			break;
-		case 5:
-			result = Phone2Words.FIVE_CHARACTERS[place];
-			break;
-		case 6:
-			result = Phone2Words.SIX_CHARACTERS[place];
-			break;
-		case 7:
-			result = Phone2Words.SEVEN_CHARACTERS[place];
-			break;
-		case 8:
-			result = Phone2Words.EIGHT_CHARACTERS[place];
-			break;
-		case 9:
-			result = Phone2Words.NINE_CHARACTERS[place];
-			break;
-		default:
-			result = '?';
-			break;
-		}
-		return result;
-	}
-
-	/**
-	 * check if the input number is a '0' or a '1'
-	 *
-	 * @param number
-	 *            input number
-	 * @return boolean if the input is '0' or '1'
-	 */
-	private static boolean isOneOrZero(int number) {
-		return number == 1 || number == 0;
-	}
-
-	/**
-	 * process one telephone phone
-	 *
-	 * @param input
-	 *            intermediate result
-	 * @param phoneNum
-	 *            phone number
-	 * @param currentNum
-	 *            index of the number to be processed
-	 * @return all combination of word for all numbers
-	 */
-	private static ArrayList<String> processOneNumber(
-			ArrayList<String> input, int[] phoneNum, int currentNum) {
-		ArrayList<String> output = new ArrayList<String>();
-		int numberToProcess = phoneNum[currentNum];
-		char c;
-		// process 1st number
-		if (input.isEmpty()) {
-			if (Phone2Words.isOneOrZero(numberToProcess)) {
-				c = Phone2Words.getCharKey(numberToProcess, 0);
-				output.add(Character.toString(c));
-			} else {
-				for (int i = 0; i < 3; i++) {
-					c = Phone2Words.getCharKey(numberToProcess, i);
-					output.add(Character.toString(c));
-				}
-			}
-		} else { // process rest of the number
-			for (String result : input) {
-				if (Phone2Words.isOneOrZero(numberToProcess)) {
-					c = Phone2Words.getCharKey(numberToProcess, 0);
-					output.add(result.concat(Character.toString(c)));
-				} else {
-					for (int i = 0; i < 3; i++) {
-						c = Phone2Words
-								.getCharKey(numberToProcess, i);
-						output.add(result.concat(Character
-								.toString(c)));
-					}
-				}
-			}
-		}
-		return output;
-	}
-
-	/**
-	 * Get all combinations of words from a phone number
-	 *
-	 * @param phoneNum
-	 *            input phone number as an array
-	 * @return all possible words
-	 */
-	public static ArrayList<String> telephoneWords(int[] phoneNum) {
-		ArrayList<String> result = new ArrayList<String>();
-
-		for (int i = 0; i < phoneNum.length; i++) {
-			result = Phone2Words
-					.processOneNumber(result, phoneNum, i);
-		}
-		return result;
-	}
-
-	/**
-	 * recursive way to print all combinations of word from a phone
-	 * number
-	 *
-	 * @param phoneNum
-	 *            input phone number as an array
-	 * @param index
-	 *            index of the output
-	 * @param output
-	 *            output word
-	 */
-	public static void telephoneWordsRecursive(int[] phoneNum,
-			int index, char[] output) {
-		if (index == output.length) {
-			System.out.println(output);
-			return;
-		}
-
-		int currentNum = phoneNum[index];
-		if (isOneOrZero(currentNum)) {
-			output[index] = getCharKey(currentNum, 0);
-			telephoneWordsRecursive(phoneNum, index + 1, output);
-		} else {
-			for (int i = 0; i < 3; i++) {
-				output[index] = getCharKey(currentNum, i);
-				telephoneWordsRecursive(phoneNum, index + 1, output);
-			}
-		}
-	}
-
-	public static void main(String[] args) {
-		int[] phoneNumber = { 2, 3, 7, 8, 2, 8, 9 }; // best buy
-		String expectedResult = "BESTBUY";
-
-		ArrayList<String> result = Phone2Words
-				.telephoneWords(phoneNumber);
-
-		int i = 1;
-		for (String str : result) {
-			System.out.print(str + " ");
-			if (i % 10 == 0)
-				System.out.print("\r"); // print new line very 10
-										// lines
-			++i;
-		}
-
-		System.out.println();
-
-		int index = result.indexOf(expectedResult);
-		if (index >= 0) {
-			int col = index % 10;
-			int row = (index - col) / 10;
-			System.out
-					.printf("\"BESTBUY\" found at index %d out of %d words, row %d col %d\n",
-							index, result.size(), row + 1, col + 1);
-		} else {
-			System.out.println("FAIL. \"BESTBUY\" not found");
-		}
-
-		char[] output = new char[phoneNumber.length];
-		Phone2Words.telephoneWordsRecursive(phoneNumber, 0, output);
-	}
-
-}

src/TelephoneWords.java

+import java.util.ArrayList;
+
+/**
+ * A non-recursive way to find all combination of words from a
+ * telephone number
+ *
+ * @author Absolute Noob
+ *
+ */
+public class Phone2Words {
+
+	private static final char[] TWO_CHARACTERS = { 'A', 'B', 'C' };
+	private static final char[] THREE_CHARACTERS = { 'D', 'E', 'F' };
+	private static final char[] FOUR_CHARACTERS = { 'G', 'H', 'I' };
+	private static final char[] FIVE_CHARACTERS = { 'J', 'K', 'L' };
+	private static final char[] SIX_CHARACTERS = { 'M', 'N', 'O' };
+	private static final char[] SEVEN_CHARACTERS = { 'P', 'R', 'S' };
+	private static final char[] EIGHT_CHARACTERS = { 'T', 'U', 'V' };
+	private static final char[] NINE_CHARACTERS = { 'W', 'X', 'Y' };
+
+	/**
+	 * get character from phone number digit
+	 *
+	 * @param phoneNumber
+	 *            phone number digit
+	 * @param place
+	 *            index of all possible character
+	 * @return character from the phone number digit
+	 */
+	private static char getCharKey(int phoneNumber, int place) {
+		char result = '?';
+		switch (phoneNumber) {
+		case 0:
+			result = '0';
+			break;
+		case 1:
+			result = '1';
+			break;
+		case 2:
+			result = Phone2Words.TWO_CHARACTERS[place];
+			break;
+		case 3:
+			result = Phone2Words.THREE_CHARACTERS[place];
+			break;
+		case 4:
+			result = Phone2Words.FOUR_CHARACTERS[place];
+			break;
+		case 5:
+			result = Phone2Words.FIVE_CHARACTERS[place];
+			break;
+		case 6:
+			result = Phone2Words.SIX_CHARACTERS[place];
+			break;
+		case 7:
+			result = Phone2Words.SEVEN_CHARACTERS[place];
+			break;
+		case 8:
+			result = Phone2Words.EIGHT_CHARACTERS[place];
+			break;
+		case 9:
+			result = Phone2Words.NINE_CHARACTERS[place];
+			break;
+		default:
+			result = '?';
+			break;
+		}
+		return result;
+	}
+
+	/**
+	 * check if the input number is a '0' or a '1'
+	 *
+	 * @param number
+	 *            input number
+	 * @return boolean if the input is '0' or '1'
+	 */
+	private static boolean isOneOrZero(int number) {
+		return number == 1 || number == 0;
+	}
+
+	/**
+	 * process one telephone phone
+	 *
+	 * @param input
+	 *            intermediate result
+	 * @param phoneNum
+	 *            phone number
+	 * @param currentNum
+	 *            index of the number to be processed
+	 * @return all combination of word for all numbers
+	 */
+	private static ArrayList<String> processOneNumber(
+			ArrayList<String> input, int[] phoneNum, int currentNum) {
+		ArrayList<String> output = new ArrayList<String>();
+		int numberToProcess = phoneNum[currentNum];
+		char c;
+		// process 1st number
+		if (input.isEmpty()) {
+			if (Phone2Words.isOneOrZero(numberToProcess)) {
+				c = Phone2Words.getCharKey(numberToProcess, 0);
+				output.add(Character.toString(c));
+			} else {
+				for (int i = 0; i < 3; i++) {
+					c = Phone2Words.getCharKey(numberToProcess, i);
+					output.add(Character.toString(c));
+				}
+			}
+		} else { // process rest of the number
+			for (String result : input) {
+				if (Phone2Words.isOneOrZero(numberToProcess)) {
+					c = Phone2Words.getCharKey(numberToProcess, 0);
+					output.add(result.concat(Character.toString(c)));
+				} else {
+					for (int i = 0; i < 3; i++) {
+						c = Phone2Words
+								.getCharKey(numberToProcess, i);
+						output.add(result.concat(Character
+								.toString(c)));
+					}
+				}
+			}
+		}
+		return output;
+	}
+
+	/**
+	 * Get all combinations of words from a phone number
+	 *
+	 * @param phoneNum
+	 *            input phone number as an array
+	 * @return all possible words
+	 */
+	public static ArrayList<String> telephoneWords(int[] phoneNum) {
+		ArrayList<String> result = new ArrayList<String>();
+
+		for (int i = 0; i < phoneNum.length; i++) {
+			result = Phone2Words
+					.processOneNumber(result, phoneNum, i);
+		}
+		return result;
+	}
+
+	/**
+	 * recursive way to print all combinations of word from a phone
+	 * number
+	 *
+	 * @param phoneNum
+	 *            input phone number as an array
+	 * @param index
+	 *            index of the output
+	 * @param output
+	 *            output word
+	 */
+	public static void telephoneWordsRecursive(int[] phoneNum,
+			int index, char[] output) {
+		if (index == output.length) {
+			System.out.println(output);
+			return;
+		}
+
+		int currentNum = phoneNum[index];
+		if (isOneOrZero(currentNum)) {
+			output[index] = getCharKey(currentNum, 0);
+			telephoneWordsRecursive(phoneNum, index + 1, output);
+		} else {
+			for (int i = 0; i < 3; i++) {
+				output[index] = getCharKey(currentNum, i);
+				telephoneWordsRecursive(phoneNum, index + 1, output);
+			}
+		}
+	}
+
+	public static void main(String[] args) {
+		int[] phoneNumber = { 2, 3, 7, 8, 2, 8, 9 }; // best buy
+		String expectedResult = "BESTBUY";
+
+		ArrayList<String> result = Phone2Words
+				.telephoneWords(phoneNumber);
+
+		int i = 1;
+		for (String str : result) {
+			System.out.print(str + " ");
+			if (i % 10 == 0)
+				System.out.print("\r"); // print new line very 10
+										// lines
+			++i;
+		}
+
+		System.out.println();
+
+		int index = result.indexOf(expectedResult);
+		if (index >= 0) {
+			int col = index % 10;
+			int row = (index - col) / 10;
+			System.out
+					.printf("\"BESTBUY\" found at index %d out of %d words, row %d col %d\n",
+							index, result.size(), row + 1, col + 1);
+		} else {
+			System.out.println("FAIL. \"BESTBUY\" not found");
+		}
+
+		char[] output = new char[phoneNumber.length];
+		Phone2Words.telephoneWordsRecursive(phoneNumber, 0, output);
+	}
+
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.