rectangles: add to track#243
Conversation
|
I have tests done locally, still need to implement. FYI, I submitted this PR upstream since the final test case caught me a little off-guard. |
| final class RectangleCounter { | ||
|
|
||
| int countRectangles(final String[] rawGrid) { | ||
| System.out.println(Arrays.toString(rawGrid)); |
| for (int rightCol = leftCol + 1; rightCol < nCols; rightCol++) { | ||
|
|
||
| if (formsRectangle(topRow, bottomRow, leftCol, rightCol)) { | ||
| result++; |
There was a problem hiding this comment.
Inelegant but effective...
There was a problem hiding this comment.
... which is just fine for the example... :)
|
|
||
| private static final class Grid { | ||
|
|
||
| private enum Entry { |
There was a problem hiding this comment.
Is this enum overkill given how little it is used?
There was a problem hiding this comment.
Nah... I think it adds to the readability of the example code (yeah, while we are laxed about it, let's not go out of our way to avoid clean code!). I'm less excited about the name Entry... but I don't have a great suggestion right this second for a better term... Stroke?... I'll get back to ya on that.
|
|
||
| private boolean isHorizontalLineSegment(final int row, final int leftCol, final int rightCol) { | ||
| return stream(copyOfRange(getRow(row), leftCol, rightCol)) | ||
| .allMatch(entry -> entry.equals(Entry.HLINE) || entry.equals(Entry.CORNER)); |
There was a problem hiding this comment.
If the Entry enum stays, I could add an isHorizontalConnector method to wrap this functionality.
|
|
||
| private boolean isVerticalLineSegment(final int col, final int topRow, final int bottomRow) { | ||
| return stream(copyOfRange(getCol(col), topRow, bottomRow)) | ||
| .allMatch(entry -> entry.equals(Entry.VLINE) || entry.equals(Entry.CORNER)); |
There was a problem hiding this comment.
If the Entry enum stays, I could add an isVerticalConnector method to wrap this functionality.
|
I miss Python's 😂 |
|
Upstream issue was resolved; I'll add new tests soon :) |
|
New tests added, this is a-ok to pull for review 🔍 |
jtigger
left a comment
There was a problem hiding this comment.
I'm super excited about this exercise. I love that we're getting more of these that are just straight-up fun+challenging! Thank you for this, @stkent.
I think I had some lame nitpick about the name of the enum in the example code, but not enough to hold this work up. If you are so inspired, you can come back with a follow-up PR.
Looks beautiful. It's complete. A great add.
| for (int rightCol = leftCol + 1; rightCol < nCols; rightCol++) { | ||
|
|
||
| if (formsRectangle(topRow, bottomRow, leftCol, rightCol)) { | ||
| result++; |
There was a problem hiding this comment.
... which is just fine for the example... :)
|
|
||
| private static final class Grid { | ||
|
|
||
| private enum Entry { |
There was a problem hiding this comment.
Nah... I think it adds to the readability of the example code (yeah, while we are laxed about it, let's not go out of our way to avoid clean code!). I'm less excited about the name Entry... but I don't have a great suggestion right this second for a better term... Stroke?... I'll get back to ya on that.
|
|
||
| private boolean isHorizontalLineSegment(final int row, final int leftCol, final int rightCol) { | ||
| return stream(copyOfRange(getRow(row), leftCol, rightCol)) | ||
| .allMatch(entry -> entry.equals(Entry.HLINE) || entry.equals(Entry.CORNER)); |
|
|
||
| private boolean isVerticalLineSegment(final int col, final int topRow, final int bottomRow) { | ||
| return stream(copyOfRange(getCol(col), topRow, bottomRow)) | ||
| .allMatch(entry -> entry.equals(Entry.VLINE) || entry.equals(Entry.CORNER)); |
| include 'pig-latin' | ||
| include 'queen-attack' | ||
| include 'raindrops' | ||
| include 'rectangles' |
|
Thanks for the kind words, @jtigger! I'll follow-up with some polish in the next couple of days :) |
x-common exercise directory