-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypoglycemia.java
More file actions
64 lines (57 loc) · 1.87 KB
/
Copy pathTypoglycemia.java
File metadata and controls
64 lines (57 loc) · 1.87 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
// Generates interior-scrambled words and interior-reversed words,
// proceeds one line of String at a time.
// (It doesn't consider the non-alphabetic letters in the text. May work on the problem latter.)
import java.util.*;
public class Typoglycemia {
private String[] parts;
public static final int UNCHANGABLE = 3; // the word length when it won't be changed
public Typoglycemia(String original) {
parts = original.split("[ \t]+");
}
public String reorganizeJumble() {
String result = "";
for (String s: parts) {
if (s.length() <= UNCHANGABLE) {
result += s + " ";
} else {
int last = s.length() - 1;
result += s.charAt(0) + chaos(s.substring(1, last)) + s.charAt(last) + " ";
}
}
return result.trim();
}
public String reorganizeReverse() {
String result = "";
for (String s: parts) {
if (s.length() <= UNCHANGABLE) {
result += s + " ";
} else {
int last = s.length() - 1;
result += s.charAt(0) + reverse(s.substring(1, last)) + s.charAt(last) + " ";
}
}
return result.trim();
}
// randomly reorganizes letters
public String chaos(String interior) {
ArrayList<Character> letterList = new ArrayList<Character>();
for (int i = 0; i < interior.length(); i++) {
letterList.add(interior.charAt(i));
}
Collections.shuffle(letterList);
String result = "";
for (char ch: letterList) {
result += ch;
}
return result;
}
// reverse letters
public String reverse(String interior) {
if (interior.length() < 2) {
return interior;
} else {
int last = interior.length() - 1;
return interior.charAt(last) + reverse(interior.substring(1, last)) + interior.charAt(0);
}
}
}