Codewars里的 6kyu Kata。
题目说明:
Description:
Prior to having fancy iPhones, teenagers would wear out their thumbs sending SMS messages on candybar-shaped feature phones with 3x4 numeric keypads.
------- ------- ------- | | | ABC | | DEF | | 1 | | 2 | | 3 | ------- ------- ------- ------- ------- ------- | GHI | | JKL | | MNO | | 4 | | 5 | | 6 | ------- ------- ------- ------- ------- ------- |PQRS | | TUV | | WXYZ| | 7 | | 8 | | 9 | ------- ------- ------- ------- ------- ------- | | |space| | | | * | | 0 | | # | ------- ------- -------
Prior to the development of T9 (predictive text entry) systems, the method to type words was called "multi-tap" and involved pressing a button repeatedly to cycle through the possible values.
For example, to type a letter
"R"
you would press the7
key three times (as the screen display for the current character cycles throughP->Q->R->S->7
). A character is "locked in" once the user presses a different key or pauses for a short period of time (thus, no extra button presses are required beyond what is needed for each letter individually). The zero key handles spaces, with one press of the key producing a space and two presses producing a zero.In order to send the message
"WHERE DO U WANT 2 MEET L8R"
a teen would have to actually do 47 button presses. No wonder they abbreviated.For this assignment, write a module that can calculate the amount of button presses required for any phrase. Punctuation can be ignored for this exercise. Likewise, you can assume the phone doesn't distinguish between upper/lowercase characters (but you should allow your module to accept input in either for convenience).
Hint: While it wouldn't take too long to hard code the amount of keypresses for all 26 letters by hand, try to avoid doing so! (Imagine you work at a phone manufacturer who might be testing out different keyboard layouts, and you want to be able to test new ones rapidly.)
解题代码:
import java.util.HashMap;
public class Keypad {
private static HashMap<Character, Integer> map = new HashMap<Character, Integer>();
public static int presses(String phrase) {
map.put('A', 1);map.put('D', 1);map.put('G', 1);map.put('J', 1);
map.put('M', 1);map.put('P', 1);map.put('T', 1);map.put('W', 1);
map.put(' ', 1);map.put('#', 1);map.put('*', 1);
map.put('B', 2);map.put('E', 2);map.put('H', 2);map.put('K', 2);
map.put('N', 2);map.put('Q', 2);map.put('U', 2);map.put('X', 2);
map.put('C', 3);map.put('F', 3);map.put('I', 3);map.put('L', 3);
map.put('O', 3);map.put('R', 3);map.put('V', 3);map.put('Y', 3);
map.put('S', 4);map.put('Z', 4);
map.put('0', 2);map.put('1', 1);map.put('2', 4);map.put('3', 4);map.put('4', 4);
map.put('5', 4);map.put('6', 4);map.put('7', 5);map.put('8', 4);map.put('9', 5);
String upperPhrase = new String(phrase.toUpperCase());
Integer res = new Integer(0);
for(int i = 0;i < upperPhrase.length(); i++) {
// res += map.get(upperPhrase.charAt(i));
res += getClick(upperPhrase.charAt(i));
}
return res.intValue();
}
public static Integer getClick(Character c) {
if(map.get(c) != null){
return map.get(c);
}else {
return new Integer(0);
}
}
}
Test Cases:
import static org.junit.Assert.*;
import java.util.Random;
import org.junit.Test;
public class KeypadTest {
private static class KnownKeypad {
private static final String[] KEYS = new String[] { "1", "ABC2", "DEF3", "GHI4", "JKL5", "MNO6", "PQRS7",
"TUV8", "WXYZ9", "*", " 0", "#" };
private static int lookup(char c) {
for (String key : KEYS) {
for (int i = 0; i < key.length(); i++) {
if (key.charAt(i) == c) {
return i + 1;
}
}
}
return 0;
}
public static int presses(String phrase) {
int result = 0;
phrase = phrase.toUpperCase();
for (char c : phrase.toCharArray()) {
// System.out.println("c: " + c);
result += lookup(c);
}
return result;
}
}
@Test
public void examples() {
assertEquals(9, Keypad.presses("LOL"));
assertEquals(13, Keypad.presses("HOW R U"));
}
@Test
public void numericInputs() {
assertEquals(47, Keypad.presses("WHERE DO U WANT 2 MEET L8R"));
}
@Test
public void caseInsensitive() {
assertEquals(9, Keypad.presses("lol"));
}
@Test
public void singleDigits() {
assertEquals(2, Keypad.presses("0"));
assertEquals(1, Keypad.presses("1"));
}
@Test
public void specialCharacters() {
assertEquals(2, Keypad.presses("#*"));
}
@Test
public void punctuation() {
assertEquals(1, Keypad.presses("A,!£$@:"));
}
@Test
public void randomStrings() {
Random rand = new Random();
String letters = "ABCDEFGHIJKLOMNOPQRSTUVWXYZabcdefghijklminopqrstuvwxy#*,!:@'$";
for (int i = 0; i < 40; i++) {
int len = rand.nextInt(25) + 6;
StringBuilder buff = new StringBuilder();
for (int j = 0; j < len; j++) {
buff.append(letters.charAt(rand.nextInt(letters.length())));
}
String testing = buff.toString();
int expected = KnownKeypad.presses(testing);
int actual = Keypad.presses(testing);
assertEquals("presses(\"" + testing + "\")", expected, actual);
}
}
}
个人总结:
public class Keypad {
static String[] keys = { "1", "ABC2", "DEF3", "GHI4", "JKL5", "MNO6", "PQRS7", "TUV8", "WXYZ9", "*", " 0", "#" };
public static int presses(String phrase) {
int nPresses = 0;
for (char c : phrase.toUpperCase().toCharArray())
for (String s : keys)
nPresses += s.indexOf(c) + 1;
return nPresses;
}
}
public class Keypad {
public static int presses(String phrase) {
int count = 0;
String symbols1 = "1AaDdGgJjMmPpTtWw*# ";
String symbols2 = "BbEeHhKkNnQqUuXx0";
String symbols3 = "CcFfIiLlOoRrVvYy";
String symbols4 = "SsZz234568";
String symbols5 = "79";
for (int i = 0; i < phrase.length(); i++) {
if (symbols1.indexOf(phrase.charAt(i)) != -1) count++;
if (symbols2.indexOf(phrase.charAt(i)) != -1) count += 2;
if (symbols3.indexOf(phrase.charAt(i)) != -1) count += 3;
if (symbols4.indexOf(phrase.charAt(i)) != -1) count += 4;
if (symbols5.indexOf(phrase.charAt(i)) != -1) count += 5;
}
return count;
}
}
public class Keypad {
public static int presses(String phrase) {
int i;
int c = 0;
String s = phrase.toLowerCase();
char b1[] = new char[] { '1' };
char b2[] = new char[] { 'a', 'b', 'c', '2' };
char b3[] = new char[] { 'd', 'e', 'f', '3' };
char b4[] = new char[] { 'g', 'h', 'i', '4' };
char b5[] = new char[] { 'j', 'k', 'l', '5' };
char b6[] = new char[] { 'm', 'n', 'o', '6' };
char b7[] = new char[] { 'p', 'q', 'r', 's', '7' };
char b8[] = new char[] { 't', 'u', 'v', '8' };
char b9[] = new char[] { 'w', 'x', 'y', 'z', '9' };
char b0[] = new char[] { ' ', '0' };
char star[] = new char[] { '*' };
char diez[] = new char[] { '#' };
for (int j = 0; j < phrase.length(); j++) {
for (i = 0; i < b1.length; i++) {
if (s.charAt(j) == b1[i]) {
c = c + (i + 1);
}
}
}
for (int j = 0; j < phrase.length(); j++) {
for (i = 0; i < b2.length; i++) {
if (s.charAt(j) == b2[i]) {
c = c + (i + 1);
}
}
}
for (int j = 0; j < phrase.length(); j++) {
for (i = 0; i < b3.length; i++) {
if (s.charAt(j) == b3[i]) {
c = c + (i + 1);
}
}
}
for (int j = 0; j < phrase.length(); j++) {
for (i = 0; i < b4.length; i++) {
if (s.charAt(j) == b4[i]) {
c = c + (i + 1);
}
}
}
for (int j = 0; j < phrase.length(); j++) {
for (i = 0; i < b5.length; i++) {
if (s.charAt(j) == b5[i]) {
c = c + (i + 1);
}
}
}
for (int j = 0; j < phrase.length(); j++) {
for (i = 0; i < b6.length; i++) {
if (s.charAt(j) == b6[i]) {
c = c + (i + 1);
}
}
}
for (int j = 0; j < phrase.length(); j++) {
for (i = 0; i < b7.length; i++) {
if (s.charAt(j) == b7[i]) {
c = c + (i + 1);
}
}
}
for (int j = 0; j < phrase.length(); j++) {
for (i = 0; i < b8.length; i++) {
if (s.charAt(j) == b8[i]) {
c = c + (i + 1);
}
}
}
for (int j = 0; j < phrase.length(); j++) {
for (i = 0; i < b9.length; i++) {
if (s.charAt(j) == b9[i]) {
c = c + (i + 1);
}
}
}
for (int j = 0; j < phrase.length(); j++) {
for (i = 0; i < b0.length; i++) {
if (s.charAt(j) == b0[i]) {
c = c + (i + 1);
}
}
}
for (int j = 0; j < phrase.length(); j++) {
for (i = 0; i < star.length; i++) {
if (s.charAt(j) == star[i]) {
c = c + (i + 1);
}
}
}
for (int j = 0; j < phrase.length(); j++) {
for (i = 0; i < diez.length; i++) {
if (s.charAt(j) == diez[i]) {
c = c + (i + 1);
}
}
}
return c;
}
}
import java.util.*;
public class Keypad {
static private List<String> layout = Arrays.asList(" 0", "1", "abc2", "def3", "ghi4", "jkl5", "mno6", "pqrs7",
"tuv8", "wxyz9", "#", "*");
static private Map<Character, Integer> LAYOUT_MAP = new HashMap<Character, Integer>();
static private int DEFAULT = 0;
static {
for (String button : layout) {
for (int i = 0; i < button.length(); i++)
LAYOUT_MAP.put(button.charAt(i), i + 1);
}
}
public static int presses(String phrase) {
int s = 0;
for (int i = 0; i < phrase.length(); i++)
s += LAYOUT_MAP.getOrDefault(Character.toLowerCase(phrase.charAt(i)), DEFAULT);
return s;
}
}