Problem:
CharGrid
Problem
CharGrid
The CharGrid
CharGridclass encapsulates a 2-d char array with a couple couple operations.int countPlus()
Look for a '+' pattern in the grid made with repetitions of a character. A '+' is made of single character in the middle and four "arms" extending out up, down, left, and right. The arms start with the middle char and extend until the first different character or grid edge. To count as a '+', all the arms should have two or more chars and should all be the same length. For example, the grid below contains exactly 2 +'s: the p-plus in the top left and the x-plus in the top right. The y in the bottom right doesn't work because the top, right, and left arms would be length 2, but the bottom arm would be length 3.
p p x ppppp xxx p y x p yyy zzzzzyzzz xx yPlusses may be formed using any character such as digits or punctuation marks, they do not have to be letters. Just treat all chars the same and your code should work fine.
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
public class CharGridTest {
//
// CountPlus Tests
//
@Test
public void testCountPlus1() {
char[][] grid1 = new char[][]{
{'x'}
};
char[][] grid2 = new char[][]{
{'a'},
{'b'}
};
char[][] grid3 = new char[][]{
{'a'},
{'b'},
{'c'}
};
char[][] grid4 = new char[][]{
{'a'},
{'b'},
{'c'},
{'d'}
};
char[][] grid5 = new char[][]{
{'a', 'b', 'c'}
};
char[][] grid6 = new char[][]{
{'a', 'b', 'c', 'd'}
};
CharGrid cg1 = new CharGrid(grid1);
CharGrid cg2 = new CharGrid(grid2);
CharGrid cg3 = new CharGrid(grid3);
CharGrid cg4 = new CharGrid(grid4);
CharGrid cg5 = new CharGrid(grid5);
CharGrid cg6 = new CharGrid(grid6);
assertEquals(0, cg1.countPlus());
assertEquals(0, cg2.countPlus());
assertEquals(0, cg3.countPlus());
assertEquals(0, cg4.countPlus());
assertEquals(0, cg5.countPlus());
assertEquals(0, cg6.countPlus());
}
@Test
public void testCountPlus2() {
char[][] grid1 = new char[][]{
{'x', 'y'},};
char[][] grid2 = new char[][]{
{'x', 'y'},
{'a', 'b'},};
char[][] grid3 = new char[][]{
{'a', 'b'},
{'c', 'd'},
{'e', 'f'},};
char[][] grid4 = new char[][]{
{'a', 'b'},
{'c', 'd'},
{'e', 'f'},
{'g', 'h'},};
char[][] grid5 = new char[][]{
{'a', 'b', 'c'},
{'d', 'e', 'f'}
};
char[][] grid6 = new char[][]{
{'a', 'b', 'c', 'd'},
{'a', 'b', 'c', 'd'},};
CharGrid cg1 = new CharGrid(grid1);
CharGrid cg2 = new CharGrid(grid2);
CharGrid cg3 = new CharGrid(grid3);
CharGrid cg4 = new CharGrid(grid4);
CharGrid cg5 = new CharGrid(grid5);
CharGrid cg6 = new CharGrid(grid6);
assertEquals(0, cg1.countPlus());
assertEquals(0, cg2.countPlus());
assertEquals(0, cg3.countPlus());
assertEquals(0, cg4.countPlus());
assertEquals(0, cg5.countPlus());
assertEquals(0, cg6.countPlus());
}
@Test
public void testCountPlus3() {
char[][] grid1 = new char[][]{
{' ', 'x', ' '},
{'x', 'x', 'x'},
{' ', 'x', ' '}
};
CharGrid cg1 = new CharGrid(grid1);
assertEquals(1, cg1.countPlus());
}
@Test
public void testCountPlus32() {
char[][] grid2 = new char[][]{
{' ', ' ', 'x', ' ', ' '},
{' ', ' ', 'x', ' ', ' '},
{'x', 'x', 'x', 'x', 'x'},
{' ', ' ', 'x', ' ', ' '},
{' ', ' ', 'x', ' ', ' '}
};
CharGrid cg2 = new CharGrid(grid2);
assertEquals(1, cg2.countPlus());
}
@Test
public void testCountPlus33() {
char[][] grid3 = new char[][]{
{' ', ' ', 'x', ' ', ' '},
{' ', ' ', 'x', ' ', ' '},
{'x', 'x', 'x', 'x', 'x'},
{'x', ' ', 'x', ' ', ' '},
{' ', ' ', 'x', ' ', ' '}
};
CharGrid cg3 = new CharGrid(grid3);
assertEquals(0, cg3.countPlus());
}
@Test
public void testCountPlus34() {
char[][] grid4 = new char[][]{
{' ', ' ', ' ', 'x', ' ', ' ', ' '},
{' ', ' ', ' ', 'x', ' ', ' ', ' '},
{' ', ' ', ' ', 'x', ' ', ' ', ' '},
{' ', 'x', 'x', 'x', 'x', 'x', ' '},
{' ', ' ', ' ', 'x', ' ', ' ', ' '},
{' ', ' ', ' ', 'x', ' ', 'x', ' '},
{' ', ' ', ' ', 'x', ' ', ' ', ' '}
};
CharGrid cg4 = new CharGrid(grid4);
assertEquals(0, cg4.countPlus());
}
@Test
public void testCountPlus35() {
char[][] grid5 = new char[][]{
{' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', 'x', ' ', ' ', ' '},
{' ', ' ', ' ', 'x', ' ', ' ', ' '},
{' ', 'x', 'x', 'x', 'x', 'x', ' '},
{' ', ' ', ' ', 'x', ' ', ' ', ' '},
{' ', ' ', ' ', 'x', ' ', 'x', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' '}
};
CharGrid cg5 = new CharGrid(grid5);
assertEquals(1, cg5.countPlus());
}
@Test
public void testCountPlus36() {
char[][] grid6 = new char[][]{
{' ', 't', ' ', 'x', ' ', ' ', ' '},
{'t', 't', 't', 'x', ' ', ' ', ' '},
{' ', 't', ' ', 'x', ' ', ' ', ' '},
{'x', 'x', 'x', 'x', 'x', 'x', 'x'},
{' ', ' ', ' ', 'x', ' ', ' ', ' '},
{' ', ' ', ' ', 'x', ' ', 'x', ' '},
{' ', ' ', ' ', 'x', ' ', ' ', ' '}
};
CharGrid cg6 = new CharGrid(grid6);
assertEquals(2, cg6.countPlus());
}
@Test
public void testCountPlus37() {
char[][] grid7 = new char[][]{
{' ', ' ', 'p', ' ', ' ', 'y', ' ', ' ', ' '},
{' ', ' ', 'p', ' ', 'y', 'y', 'y', 'x', ' '},
{'p', 'p', 'p', 'p', 'p', 'y', 'x', 'x', 'x'},
{' ', ' ', 'p', ' ', ' ', 'y', ' ', 'x', ' '},
{' ', ' ', 'p', ' ', 'y', 'y', 'y', ' ', ' '},
{'z', 'z', 'z', 'z', 'z', 'y', 'z', 'z', 'z'},
{' ', ' ', 'x', 'x', ' ', ' ', ' ', ' ', ' '},};
CharGrid cg7 = new CharGrid(grid7);
assertEquals(2, cg7.countPlus());
}
@Test
public void testCountPlus38() {
char[][] grid8 = new char[][]{
{' ', ' ', 'p', ' ', ' ', 'q', ' ', ' ', ' '},
{' ', ' ', 'p', ' ', 'q', 'q', 'q', 'x', ' '},
{'p', 'p', 'p', 'p', 'p', 'q', 'x', 'x', 'x'},
{' ', ' ', 'p', ' ', ' ', 'y', ' ', 'x', ' '},
{' ', ' ', 'p', ' ', 'y', 'y', 'y', ' ', ' '},
{'z', 'z', 'z', 'z', 'z', 'y', 'z', 'z', 'z'},
{' ', ' ', 'x', 'x', ' ', 'y', ' ', ' ', ' '},};
CharGrid cg8 = new CharGrid(grid8);
assertEquals(3, cg8.countPlus());
}
}
