-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKSort.java
More file actions
53 lines (46 loc) · 1.4 KB
/
Copy pathKSort.java
File metadata and controls
53 lines (46 loc) · 1.4 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
public class ksort {
public String alphabet = "abcdefgh";
public String[] items;
public ksort(){
int size = 100 * (alphabet.length() - 1) + 10 * 9 + 10;
items = new String[size];
}
public boolean add(String s){
int index = isSuitable(s);
if (index != -1){
items[index] = s;
return true;
}else {
return false;
}
}
public int index(String s) {
return isSuitable(s);
}
public int isSuitable(String item) {
int index = -1;
if (item.length() == 3) {
for (int i = 0; i < alphabet.length(); i++) {
if (alphabet.charAt(i) == item.charAt(0)) {
index *= 100*i *(-1);
i = alphabet.length();
}
}
//checking whether alphabet contains the first letter of "item"
if (index == -1) {
return -1;
} else {
if (def(item)) {
index = index + 10 * Character.getNumericValue(item.charAt(1)) + Character.getNumericValue(item.charAt(2));
return index;
}else {
return -1;
}
}
}
return -1;
}
public boolean def(String item){
return Character.isDigit(item.charAt(1))&& Character.isDigit(item.charAt(2));
}
}