Skip to content

Assignment 4 - Code review request#7

Open
karineek wants to merge 28 commits intoassignment4_basefrom
master
Open

Assignment 4 - Code review request#7
karineek wants to merge 28 commits intoassignment4_basefrom
master

Conversation

@karineek
Copy link
Owner

It has few changes from assignment 3, please ignore these, thanks!

Copy link
Collaborator

@NataliaDymnikova NataliaDymnikova left a comment

Choose a reason for hiding this comment

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

I like your code :)
But I think it coulde be shorter.

return;
}

if (map[curr_row][curr_col] == false)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think you could write just !map[curr_row][curr_col] without == false

Copy link
Owner Author

Choose a reason for hiding this comment

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

sure, np

if (m_visisted_map[curr_row][curr_col] == true)
{
return;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Also I think that you could combine all if here to the one or split it to the two ifs -- one for checking boundaries and one for checking visiting and it isn't water.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Agreed. It's a very long-winded way of writing some simple tests.

Copy link
Owner Author

@karineek karineek Jul 3, 2017

Choose a reason for hiding this comment

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

OK, will create a big OR condition...

Case isn't the right thing here, since all are "return" - there are no other options...

*/
public class KEMIslandsFinder {

private boolean[][] m_visisted_map;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is better to name it as mVisitedMap?

Copy link
Owner Author

Choose a reason for hiding this comment

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

sure, np

{
for (int j = -1; j < 2; j++)
{
markIslandAsVisited(map,curr_row+i,curr_col+j);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you check here diagonal elements? Read the task more attentively.

Copy link
Owner Author

@karineek karineek Jul 3, 2017

Choose a reason for hiding this comment

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

Yep, only now realised it... thnx

Copy link
Collaborator

@simon-frankau simon-frankau left a comment

Choose a reason for hiding this comment

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

My one recommendation would be to write more compactly. It's possible to be too terse, but you're not at risk of that currently.

You also need to read the assignment more carefully and tweak the algorithm.

* gets: a map and the size of the map (rxc)
* and returns the number of Islands in the
* map
*/
Copy link
Collaborator

Choose a reason for hiding this comment

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

These lines are wrapped unusually short. People normally wrap around, say, 80 columns, although the Google Java coding style suggests 100 columns.

Copy link
Owner Author

Choose a reason for hiding this comment

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

sure, np

return currentNumberOfIslands;
}

private void initVisitedMap()
Copy link
Collaborator

Choose a reason for hiding this comment

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

This method does nothing, since the boolean arrays are automatically initialised to false.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Wasn't sure about it, but it passes the tests without it, so I remove it now

}

/* Private Helpers */
private int countIslands(boolean[][] map, int curr_row, int curr_col)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This method could be written a lot more simply as a pair of nested for loops iterating over the rows and columns.

/* Move to the next location */
if ((curr_col+1) < map[curr_row].length)
{
currentNumberOfIslands += this.countIslands(map, curr_row, curr_col + 1);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why "this."?

Copy link
Owner Author

Choose a reason for hiding this comment

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

will remove it...

if (m_visisted_map[curr_row][curr_col] == true)
{
return;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Agreed. It's a very long-winded way of writing some simple tests.

{
return 0;
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

return (map[curr_row][curr_col] && !m_visited_map[curr_row][curr_col]) ? 1 : 0;

Alternatively, take out the ternary, make this bool isNewIsland(...), and have the caller do "if (isNewIsland(...)) { currentNumberOfIslands++; }"

Copy link
Owner Author

Choose a reason for hiding this comment

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

sure, will change it


public static void tearDownClass() {
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

Remove empty boilerplate methods that aren't needed.

Copy link
Owner Author

Choose a reason for hiding this comment

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

np

boolean[][] map = {{true,false},{false,true}};
KEMIslandsFinder instance = new KEMIslandsFinder();
assertEquals(1, instance.countIslands(row, col, map));
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Incorrect behaviour.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Yep, only now realised it... thnx

KEMIslandsFinder instance = new KEMIslandsFinder();
assertEquals(5, instance.countIslands(row, col, map));
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it would have been good to test an island with a lake inside it (a doughnut island), and I think you misunderstood the details of the assignment so you've got some incorrect expectations in your tests, but it's basically pretty good testing.

Copy link
Owner Author

@karineek karineek Jul 3, 2017

Choose a reason for hiding this comment

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

Yes, I missed a word there... thanks

Sure, will add this test

@karineek
Copy link
Owner Author

karineek commented Jul 3, 2017

Fixed most of the comments, thanks

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