Skip to content

Conversation

@andrada12
Copy link
Collaborator

No description provided.

*/
public class Dictionary {

static HashMap<String, ArrayList<String>> dictionary = new HashMap<String, ArrayList<String>>();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to have either:

  • a comment
  • or a good variable name (better)
    so we can tell this is a map from prefixes to all words that start with that prefix.

I'd probably call to variable something like "prefixesToWords" but choose something that reads well for you.


public static void setDictionary(String[] words){

for(int i = 0; i < words.length; i++){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: Did you know you could do for(String word : words) {
for the outer loop? This is optional, but many people find it more readable.

* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving auto-generated javadoc in your files is usually bad style :-)

public class WordGrid {
int[] Ox = new int[]{-1,0,1};
int[] Oy = new int[]{-1,0,1};

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These variable names aren't very explanatory, and especially as O and 0 are easily confused make your code a little hard to read. Can you think of a good name?
I'd use something like "xSteps" or "xNeighbours"


static HashMap<String, ArrayList<String>> dictionary = new HashMap<String, ArrayList<String>>();

public static void setDictionary(String[] words){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: this is a perfectly good data structure, but isn't as space efficient as it could be. Can you do better? This is the sort of follow up question that gets asked at an interview.

If you get stuck you might want to look at https://en.wikipedia.org/wiki/Trie

}

/* Given a position (x,y) it generates all its neighbours from a matrix */
public ArrayList<Position> neighbors(int x, int y, char[][] chars, String result, boolean[][] visited){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extracting a position class is nice design! Can you make it more object oriented? A few ideas:

  • maybe pass a Position into this method instead of x,y
  • move the neighbours() method to the Position class (you'd have to look again about how you handle visited)

ArrayList<String> expResult = new ArrayList<String>();
expResult.add("sky");
expResult.add("key");

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: maybe be consistent with your number of blank lines.

*/
public class WordGridTest {

public WordGridTest() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can leave out the constructor, before and after, they aren't required if you don't use them

* Test of dfs method, of class WordGrid.
*/
@Test
public void testDfs() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These methods could be improved by making the name reflect what is being tested, instead of just numbering them. So for example:

testWordGridTraversesWholeTree() or testWordGridIgnoresVisited()

/**
* Test of words method, of class WordGrid.
*/
@Test
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well done on writing tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants