forked from lauraPrp/exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyboard.java
More file actions
75 lines (56 loc) · 2 KB
/
Keyboard.java
File metadata and controls
75 lines (56 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package exercise;
import java.util.ArrayList;
import java.util.Scanner;
import javafx.util.Pair;
/*
* this class simulates the use of keyboard with a tv controller, the aim is count the steps needed to digit a word
* each letter is followed by a OK button press that has to be included in the count, it's necessary to take the shorter route
* from one letter to another, the prompt always starts on the letter A (0,0)
* */
public class Keyboard {
private static final String[][] keyboard ={//new String[5][8]
{"a","b","c","d","e","1","2","3"},
{"f","g","h","i","j","4","5","6"},
{"k","l","m","n","o","7","8","9"},
{"p","q","r","s","t",".","@","0"},
{"u","v","w","x","y","z","_","/"}
};
public static int stepCounter(ArrayList<Pair<Integer,Integer>> pair) {
int counter=0;
for(int i=0; i<pair.size()-1;i++) {
Pair<Integer,Integer> p = pair.get(i);
Pair<Integer,Integer> p2 = pair.get(i+1);
int step1= Math.abs(p.getKey()-p2.getKey());
int step2= Math.abs(p.getValue()-p2.getValue());
counter=counter+(step1+step2);
counter++;//simulates OK button
//System.out.println("*****************************"+ counter);
}
return counter;
}
public static Pair<Integer,Integer> findLetter(String letter) {
Pair<Integer,Integer> resPair = new Pair<> (0,0);
for(int i = 0; i<keyboard.length;i++) {
for (int j=0; j<keyboard[i].length; j++) {
if(keyboard[i][j].equalsIgnoreCase(letter)) {
resPair=new Pair<>(i,j);
}
}
}
return resPair;
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String input1=sc.next();
String letter="";
ArrayList<Pair<Integer,Integer>> arr= new ArrayList<>();
Pair<Integer,Integer> pairs= new Pair<>(0,0);
for(int i=0; i<input1.length();i++) {
letter=Character.toString(input1.charAt(i));
pairs= findLetter(letter);
arr.add(pairs);
}
System.out.println("total steps:"+stepCounter(arr));
sc.close();
}
}