Skip to content

Commit ac48904

Browse files
committed
Clear up
1 parent 6402d43 commit ac48904

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+98
-1521084
lines changed

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
slides
2+
*.class
3+
*.zip
4+
*.pdf
5+
*.txt
6+
DrJava.lnk

checkstyle-algs4.bat

-25
This file was deleted.

checkstyle-algs4.ps1

-43
This file was deleted.

findbugs-algs4.bat

-29
This file was deleted.

findbugs-algs4.ps1

-44
This file was deleted.

java-algs4.bat

-12
This file was deleted.

javac-algs4.bat

-12
This file was deleted.
File renamed without changes.

week5/BoggleSolver.java week4/BoggleSolver.java

+66-29
Original file line numberDiff line numberDiff line change
@@ -8,44 +8,75 @@
88
import java.lang.StringBuilder;
99
import java.util.ArrayList;
1010
import java.util.Iterator;
11+
import java.util.HashSet;
1112
import java.util.Stack;
1213
import edu.princeton.cs.algs4.StdOut;
1314
import edu.princeton.cs.algs4.Queue;
1415
import edu.princeton.cs.algs4.In;
1516
import edu.princeton.cs.algs4.TrieST;
1617

17-
//import java.util.HashMap;
1818
//import java.util.Arrays;
1919

2020
public class BoggleSolver
2121
{
2222
private TrieST<String> dictionaryTrie;
23+
private String[] mydictionary;
2324

2425
// Initializes the data structure using the given array of strings as the dictionary.
2526
// (You can assume each word in the dictionary contains only the uppercase letters A through Z.)
2627
public BoggleSolver(String[] dictionary) {
27-
trace("constructing BoggleSolver");
28+
mydictionary = new String[dictionary.length];
29+
for(int i = 0; i < dictionary.length; i++)
30+
mydictionary[i] = dictionary[i];
31+
}
32+
33+
private void construct_trie(BoggleBoard board) {
34+
StringBuilder sb = new StringBuilder();
35+
for (int i = 0; i < board.rows(); i++) {
36+
for (int j = 0; j < board.cols(); j++) {
37+
char c = board.getLetter(i, j);
38+
sb.append(c);
39+
if(c == 'Q')
40+
sb.append('U');
41+
}
42+
}
43+
String validChars = sb.toString();
44+
2845
dictionaryTrie = new TrieST<String>();
2946

30-
for(String w : dictionary)
31-
dictionaryTrie.put(w, w);
47+
// add only those words that has the chars:
48+
boolean isAllChars;
49+
for(String w : mydictionary) {
50+
isAllChars = true;
51+
for(int i = 0; i < w.length(); i++) {
52+
if(validChars.indexOf(w.charAt(i)) == -1) {
53+
isAllChars = false;
54+
break;
55+
}
56+
}
57+
if(isAllChars)
58+
dictionaryTrie.put(w, w);
59+
}
3260
}
3361

3462
// Returns the set of all valid words in the given Boggle board, as an Iterable.
3563
public Iterable<String> getAllValidWords(BoggleBoard board) {
36-
trace("gets words for board " + board.rows() + "x" + board.cols());
37-
ArrayList<String> words = new ArrayList<String>();
64+
construct_trie(board);
65+
66+
trace("gets words for board " + board.rows() + "x" + board.cols() + " \n");
67+
// walk cell by cell
68+
HashSet<String> words = new HashSet<String>();
3869
for(int i = 0; i < board.rows(); i++){
3970
for(int j = 0; j < board.cols(); j++){
4071
Stack<Point> path = new Stack<Point>();
4172
Point p = new Point(i, j);
4273
path.push(p);
4374
StringBuilder wordBuilder = new StringBuilder();
4475
append(wordBuilder, board, p);
45-
walk(board, path, words, wordBuilder);
76+
walk(board, path, words, wordBuilder, null);
4677
}
4778
}
48-
trace("found " + words.size() + " words");
79+
trace("found " + words.size() + " words\n");
4980
return words;
5081
}
5182

@@ -63,8 +94,12 @@ private void remove(StringBuilder sb, BoggleBoard board, Point p) {
6394
sb.deleteCharAt(sb.length() - 1); // 'U' was removed
6495
}
6596

66-
67-
private void walk(BoggleBoard board, Stack<Point> path, ArrayList<String> found, StringBuilder wordBuilder) {
97+
private void walk(
98+
BoggleBoard board,
99+
Stack<Point> path,
100+
HashSet<String> found,
101+
StringBuilder wordBuilder,
102+
Iterable<String> keys) {
68103
Point v = path.peek();
69104
for(Point p : getAdj(board, v)) {
70105
if(path.contains(p))
@@ -74,21 +109,26 @@ private void walk(BoggleBoard board, Stack<Point> path, ArrayList<String> found,
74109
append(wordBuilder, board, p);
75110
String current = wordBuilder.toString();
76111

77-
if(current.length() < 3) {
78-
walk(board, path, found, wordBuilder);
112+
113+
if(path.size() < 3 && current.length() < 3) {
114+
walk(board, path, found, wordBuilder, keys);
79115
}
80116
else {
81-
Iterable<String> keys = dictionaryTrie.keysWithPrefix(current);
117+
if(path.size() == 3 || current.length() == 3 || keys == null) // reinit
118+
keys = dictionaryTrie.keysWithPrefix(current);
82119
int left = 0;
120+
trace(" walking " + current + ", keys: ");
83121
for(String k : keys) {
84-
if(k == current)
85-
if(!found.contains(current))
86-
found.add(current);
87-
else
122+
trace(k + " ");
123+
if(k.equals(current)) {
124+
found.add(current);
125+
}
126+
else if(k.startsWith(current))
88127
left++;
89128
}
129+
trace("; " + left + " left\n");
90130
if(left > 0)
91-
walk(board, path, found, wordBuilder);
131+
walk(board, path, found, wordBuilder, keys);
92132
}
93133

94134
path.pop();
@@ -113,16 +153,13 @@ private Iterable<Point> getAdj(BoggleBoard board, Point p) {
113153
// Returns the score of the given word if it is in the dictionary, zero otherwise.
114154
// (You can assume the word contains only the uppercase letters A through Z.)
115155
public int scoreOf(String w) {
116-
if(dictionaryTrie.contains(w)) {
117-
int l = w.length();
118-
if(l < 3) return 0;
119-
if(l < 5) return 1;
120-
if(l == 5) return 2;
121-
if(l == 6) return 3;
122-
if(l == 7) return 5;
123-
if(l > 7) return 11;
124-
}
125-
return 0;
156+
int l = w.length();
157+
if(l < 3) return 0;
158+
if(l < 5) return 1;
159+
if(l == 5) return 2;
160+
if(l == 6) return 3;
161+
if(l == 7) return 5;
162+
return 11;
126163
}
127164

128165
public static void main(String[] args)
@@ -164,6 +201,6 @@ public int hashCode() {
164201
}
165202

166203
private void trace(String msg) {
167-
//StdOut.println(msg);
204+
//StdOut.print(msg);
168205
}
169206
}
File renamed without changes.
File renamed without changes.

week6/.gitignore week5/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
burrows
2+
.idea

week5/boggle/board-16q.txt

-5
This file was deleted.

week5/boggle/board-antidisestablishmentarianisms.txt

-2
This file was deleted.

week5/boggle/board-aqua.txt

-5
This file was deleted.

week5/boggle/board-couscous.txt

-5
This file was deleted.

week5/boggle/board-diagonal.txt

-5
This file was deleted.

0 commit comments

Comments
 (0)