/
Codewords
88 lines (85 loc) · 2.29 KB
/
Codewords
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
76
77
78
79
80
81
82
83
84
85
86
87
88
public int getWordsCount() {
int cnt = 0;
String pattern = "[A-Za-z]{4,}[A-Za-z0-9]*";
Matcher m = null;
String s = null;
try (BufferedReader reader =
new BufferedReader(new InputStreamReader(new FileInputStream(inPath)))) {
Pattern r = Pattern.compile(pattern);
while ((s = reader.readLine()) != null) {
String[] s1 = s.split("[^a-zA-Z0-9]");
for (String tp : s1) {
m = r.matcher(tp);
if (m.matches()) {
cnt++;
map.merge(tp.toLowerCase(), 1, Integer::sum);
}
}
}
return cnt;
} catch (IOException ie) {
ie.printStackTrace();
}
public int getValidLines() {
String s;
int cnt = 0;
try (BufferedReader reader =
new BufferedReader(new InputStreamReader(new FileInputStream(inPath)))) {
while ((s = reader.readLine()) != null) {
if (!"".equals(s.trim())) {
cnt++;
}
}
return cnt;
} catch (IOException ie) {
ie.printStackTrace();
}
return 0;
}
public int getCharCount() {
int tp;
int cnt = 0;
try (BufferedReader reader =
new BufferedReader(new InputStreamReader(new FileInputStream(inPath)))) {
while ((tp = reader.read()) != -1) {
if (tp < 128) {
cnt++;
}
}
return cnt;
} catch (IOException ie) {
ie.printStackTrace();
}
return 0;
}
void quick_sort(String[] list, int l, int r) {
if(l>=r) return;
int i = l, j = r;
int k = i+1;
int x=map.get(list[l]);
if(k<j){
while (k<=j) {
if (map.get(list[k]) < x){
String tp = list[j];
list[j] = list[k];
list[k] = tp;
j--;
}
else if (map.get(list[k]) > x){
String tp = list[i];
list[i] = list[k];
list[k] = tp;
k++;
i++;
}else{
k++;
}
}
if(r-l>20){
quick_sort(list, l, i - 1);
}else{
quick_sort(list, l, i - 1);
quick_sort(list, j + 1, r);
}
}
}