🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 # mallardlicker's solution

## to Rectangles in the Java Track

Published at Jan 28 2021 · 0 comments
Instructions
Test suite
Solution

Count the rectangles in an ASCII diagram like the one below.

``````   +--+
++  |
+-++--+
|  |  |
+--+--+
``````

The above diagram contains 6 rectangles:

``````

+-----+
|     |
+-----+
``````
``````   +--+
|  |
|  |
|  |
+--+
``````
``````   +--+
|  |
+--+

``````
``````

+--+
|  |
+--+
``````
``````

+--+
|  |
+--+
``````
``````
++
++

``````

You may assume that the input is always a proper rectangle (i.e. the length of every line equals the length of the first line).

## Setup

Go through the setup instructions for Java to install the necessary dependencies:

https://exercism.io/tracks/java/installation

# Running the tests

You can run all the tests for an exercise by entering the following in your terminal:

``````\$ gradle test
``````

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by removing the `@Ignore("Remove to run test")` annotation.

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### RectangleCounterTest.java

``````import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class RectangleCounterTest {

private RectangleCounter rectangleCounter;

@Before
public void setUp() {
rectangleCounter = new RectangleCounter();
}

@Test
public void testInputWithNoRowsContainsNoRectangles() {
String[] inputGrid = new String[]{};

assertEquals(0, rectangleCounter.countRectangles(inputGrid));
}

@Ignore("Remove to run test")
@Test
public void testInputWithNoColumnsContainsNoRectangles() {
String[] inputGrid = new String[]{""};

assertEquals(0, rectangleCounter.countRectangles(inputGrid));
}

@Ignore("Remove to run test")
@Test
public void testNonTrivialInputWithNoRectangles() {
String[] inputGrid = new String[]{" "};

assertEquals(0, rectangleCounter.countRectangles(inputGrid));
}

@Ignore("Remove to run test")
@Test
public void testInputWithOneRectangle() {
String[] inputGrid = new String[]{
"+-+",
"| |",
"+-+"
};

assertEquals(1, rectangleCounter.countRectangles(inputGrid));
}

@Ignore("Remove to run test")
@Test
public void testInputWithTwoRectanglesWithoutSharedEdges() {
String[] inputGrid = new String[]{
"  +-+",
"  | |",
"+-+-+",
"| |  ",
"+-+  "
};

assertEquals(2, rectangleCounter.countRectangles(inputGrid));
}

@Ignore("Remove to run test")
@Test
public void testInputWithFiveRectanglesWithSharedEdges() {
String[] inputGrid = new String[]{
"  +-+",
"  | |",
"+-+-+",
"| | |",
"+-+-+"
};

assertEquals(5, rectangleCounter.countRectangles(inputGrid));
}

@Ignore("Remove to run test")
@Test
public void testThatRectangleOfHeightOneIsCounted() {
String[] inputGrid = new String[]{
"+--+",
"+--+"
};

assertEquals(1, rectangleCounter.countRectangles(inputGrid));
}

@Ignore("Remove to run test")
@Test
public void testThatRectangleOfWidthOneIsCounted() {
String[] inputGrid = new String[]{
"++",
"||",
"++"
};

assertEquals(1, rectangleCounter.countRectangles(inputGrid));
}

@Ignore("Remove to run test")
@Test
public void testThatOneByOneSquareIsCounted() {
String[] inputGrid = new String[]{
"++",
"++"
};

assertEquals(1, rectangleCounter.countRectangles(inputGrid));
}

@Ignore("Remove to run test")
@Test
public void testThatIncompleteRectanglesAreNotCounted() {
String[] inputGrid = new String[]{
"  +-+",
"    |",
"+-+-+",
"| | -",
"+-+-+"
};

assertEquals(1, rectangleCounter.countRectangles(inputGrid));
}

@Ignore("Remove to run test")
@Test
public void testThatRectanglesOfDifferentSizesAreAllCounted() {
String[] inputGrid = new String[]{
"+------+----+",
"|      |    |",
"+---+--+    |",
"|   |       |",
"+---+-------+"
};

assertEquals(3, rectangleCounter.countRectangles(inputGrid));
}

@Ignore("Remove to run test")
@Test
public void testThatIntersectionsWithoutCornerCharacterDoNotCountAsRectangleCorners() {
String[] inputGrid = new String[]{
"+------+----+",
"|      |    |",
"+------+    |",
"|   |       |",
"+---+-------+"
};

assertEquals(2, rectangleCounter.countRectangles(inputGrid));
}

@Ignore("Remove to run test")
@Test
public void testLargeInputWithManyRectangles() {
String[] inputGrid = new String[]{
"+---+--+----+",
"|   +--+----+",
"+---+--+    |",
"|   +--+----+",
"+---+--+--+-+",
"+---+--+--+-+",
"+------+  | |",
"          +-+"
};

assertEquals(60, rectangleCounter.countRectangles(inputGrid));
}

}``````
``````public class RectangleCounter {

/*String[] rects = {"   +--+",
"  ++  |",
"+-++--+",
"|  |  |",
"+--+--+"};*/

/*String[] rects = {"+   + ++  ",
"+    + +  ",
"    + +  +",
"    +  +  ",
"+        +"};*/

/*String[] rects = {"+   + ++  ",
"+    + +  ",
"+   + +  +",
"    +  +  ",
"+    ++  +"};*/

public int countRectangles(String[] rects) {
int total = 0;

int a, b;
for (int i = 0; i < rects.length; i++) {
for (int j = 0; j < rects[i].length(); j++) {

if (rects[i].substring(j, j+1).equals("+")) {
a = j+1;
while (a < rects[i].length()) {
if (rects[i].substring(a, a+1).equals("+")) {
b = i+1;
while (b < rects.length) {
if (rects[b].substring(j, j+1).equals("+") && rects[b].substring(a, a+1).equals("+"))
total++;
b++;
}
}
a++;
}
}

}
}

return total;
}

/*public void printRects() {
for (String s : rects)
System.out.println(s);
}*/

}``````

## Community comments

Find this solution interesting? Ask the author a question to learn more.

### What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

• What compromises have been made?
• Are there new concepts here that you could read more about to improve your understanding?