-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
264 lines (247 loc) · 8.57 KB
/
Main.java
File metadata and controls
264 lines (247 loc) · 8.57 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import java.io.BufferedReader; // File IO
import java.io.File;
import java.io.FileReader; // More File IO
//import java.util.Scanner; // User input
import java.util.ArrayList; // ArrayList for storing file
import java.io.FileWriter;
import java.io.IOException;
import java.io.BufferedWriter;
class Main {
// Set some static variables:
static ArrayList<String> scrabble;
//static ArrayList<String> output = new ArrayList<String>();
static String toCheck;
/**
* Open and read a file, and return the lines in the file as a list
* of Strings.
* (Demonstrates Java FileReader, BufferedReader, and Java5.)
* Stolen shamelessly from https://alvinalexander.com/blog/post/java/how-open-read-file-java-string-array-list
*/
public static ArrayList<String> readFile(String filename) {
ArrayList<String> records = new ArrayList<String>();
try {
BufferedReader reader = new BufferedReader(new FileReader(filename));
String line;
while ((line = reader.readLine()) != null) {
records.add(line.substring(0, line.length()-1));
}
reader.close();
return records;
}
catch (IOException e) {
System.err.format("IOException occurred trying to read '%s'.", filename);
e.printStackTrace();
return null;
}
}
public static void writeFile(String filename, ArrayList<String> data) {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
String newdata = "";
for (int i = 0; i<data.size(); i++) {
newdata = data.get(i) + "\n";
writer.write(newdata);
}
writer.close();
} catch(Exception e) {
System.out.println("Error finding file");
e.printStackTrace();
}
}
public static void combinationUtil(String arr[], String data[], int start, int end, int index, int r) {
toCheck = "";
if (index == r) {
for (int j=0; j<r; j++) {
toCheck = toCheck + data[j];
}
// Using linear search because for some reason, binary search is not producing all of the results
//if (linearSearch(scrabble, toCheck) > -1) {
wordIt(toCheck);
//}
} else {
for (int i=start; (i <= end && end-i+1 >= r-index); i++) {
data[index] = arr[i];
combinationUtil(arr, data, i+1, end, index+1, r);
}
}
}
public static void printCombination(String arr[], int n, int r) {
String[] data = new String[r];
combinationUtil(arr, data, 0, n-1, 0, r);
}
public static void permutation(String str) {
permutation("", str);
}
private static void permutation(String prefix, String str) {
int n = str.length();
if (n == 0) {
System.out.println(prefix);
} else {
for (int i = 0; i < n; i++) {
permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i+1, n));
}
}
}
/*
public static ArrayList<String> getCombinations(String input) {
ArrayList<String> output = new ArrayList<String>();
//We will follow these rules:
//Every word has at least one vowel.
//Every syllable has one vowel.
//Q is always followed by a u (queen).
//Double the consonants f, l, and s at the end of a one-syllable word //that has just one vowel (stiff, spell, pass).
input = input.toLowerCase();
ArrayList<String> fragments = new ArrayList<String>();
ArrayList<String> characters = new ArrayList<String>();
for (int i=0;i<input.length(); i++) {
characters.add(input.substring(i,i+1));
}
characters = insertionSort(characters);
String c;
// Process the characters into fragments
for (int i=0; i<characters.size(); i++) {
c = characters.get(i);
System.out.println(c);
if (c.equals("q")) {
int uPos = linearSearch(characters, "u");
if (uPos != -1) {
fragments.add("qu");
}
} else if (c.equals("f") || c.equals("l") || c.equals("s")) {
int dPos = linearSearch(characters, c, i+1, characters.size());
if (dPos != -1) {
String doubled = c + c;
fragments.add(doubled);
}
} else {
fragments.add(c);
}
}
// Convert fragments into word
String e, n;
for (String fragment : fragments) {
if (fragment.length() == 1) {
e = fragment.substring(0,1);
} else if (fragment.length() == 2) {
e = fragment.substring(1, 2);
} else {
e = "";
}
n = new String(e);
output.add(n);
}
return fragments;
}
*/
public static int binarySearch(ArrayList<String> items, String toFind) {
int isThere = -1;
int start = 0;
int end = items.size();
int center = (start+end)/2;
String cv;
while (isThere == -1) {
center = (start+end)/2;
cv = items.get(center);
if ((items.get(center-1).compareTo(toFind) < 0) && (cv.compareTo(toFind) > 0)) {
return -1;
} else if (cv.compareTo(toFind) < 0) {
start = center;
} else if (cv.compareTo(toFind) > 0) {
end = center;
} else if (cv.compareTo(toFind) == 0) {
isThere = center;
}
}
return isThere;
}
public static void wordIt(String toWord) {
// Do stuff with the word here
System.out.println(toWord);
}
public static int linearSearch(ArrayList<String> items, String toFind) {
return linearSearch(items, toFind, 0, items.size());
}
public static int linearSearch(ArrayList<String> items, String toFind, int beginIndex, int endIndex) {
for (int i=beginIndex; i<endIndex; i++) {
if (items.get(i).equals(toFind)) {
return i;
}
}
return -1;
}
/*
public static ArrayList<String> insertionSort(ArrayList<String> data) {
int n = data.size();
for (int i = 1; i < n; ++i) {
String key = data.get(i);
int j = i - 1;
while (j >= 0 && data.get(j).compareTo(key) == 1) {
data.set(j+1, data.get(j));
j = j - 1;
}
data.set(j+1, key);
}
return data;
}
*/
public static void main(String[] args) {
File scrabble_new_file = new File("./scrabble_new.txt");
if (!scrabble_new_file.exists()) {
// Create the new file with trimmed words
ArrayList<String> scrabble_old = readFile("scrabble.txt");
ArrayList<String> scrabble_new = new ArrayList<String>();
for (int i = 0; i<scrabble_old.size(); i++) {
if (scrabble_old.get(i).length()>2 && scrabble_old.get(i).length()<8) {
scrabble_new.add(scrabble_old.get(i));
}
}
writeFile("scrabble_new.txt", scrabble_new);
} else {
scrabble = readFile("./scrabble_new.txt");
}
String inputArr[] = {"r", "e", "p", "r", "o", "o", "f"};
//ArrayList<String> almostdone = new ArrayList<String>();
//ArrayList<String> done = new ArrayList<String>();
//for (int i=2; i<=inputArr.length; i++) {
//almostdone.addAll(printCombination(inputArr, inputArr.length, i));
// printCombination(inputArr, inputArr.length, i+1);
//}
for (int i=0; i<inputArr.length; i++) {
for (int j=0; j<i; j++) {
String[] newInputArr = new String[j];
}
permutation("reproof");
}
/*
for (String e : almostdone) {
if (!done.contains(e)) {
done.add(e);
}
}
System.out.println(done.toString());
*/
/*
ERR
FOE
FOR
FRO
ORE
PER
PRO
REF
REP
ROE
FORE
POOR
PORE
ROOF
ROPE
PROOF
ROPER
POORER
ROOFER
PROOFER
REPROOF
*/
}
}