In: Computer Science
Write a method that will accept a 2D character array. The 2D array represents a grid (table) of characters in which a triple may occur. A triple is 3 matching characters. This method will search through the array to determine whether or not it contains a set of 3 matching characters. Specifically, it will check to see if the 3 matching characters appear somewhere in the array as three adjacent characters either horizontally (left to right) or vertically (top to bottom). You should NOT consider diagonals. If a set is found, stop searching and return the character in the set, if no set is found throw the SetNotFoundException and provide an appropriate message.
You may either write your answer in the space provided or upload a PDF containing your solution.
Examples:
For the following array a search will result in a triple being
found and returning the character C:
For the following array a search will result in a triple being
found and returning the character B:
For the following array a search will result in a triple NOT
being found and throwing the SetNotFoundException:
/**
Searches the character grid for the appearance of a set of 3
identical characters, horizontally or vertically.
@param searchGrid The 2D character array in which the search will
occur.
@return The character that was in the set.
@throws SetNotFoundException if the searchGrid does not contain a
set of exactly 3 matching characters.
*/
public static char setSearchChar(char[][] searchGrid) throws
SetNotFoundException {
The language is Java
import java.util.*;
import java.lang.*;
import java.io.*;
class setNotFoundException extends Exception{
setNotFoundException(String s){
super(s);
}
}
public class Solution
{
static int MAX = 26;
public static char setSearchChar(char[][] searchGrid) throws
setNotFoundException
{
int x_len = searchGrid[0].length;
int y_len = searchGrid.length;
for(int i = 0;i<x_len;i++){
for(int j = 0;j<y_len-2;j++){
if(searchGrid[i][j] == searchGrid[i][j+1] &&
searchGrid[i][j+1] == searchGrid[i][j+2])
return searchGrid[i][j];
}
}
for(int i = 0;i<y_len;i++)
{
for(int j =0;j<x_len-2;j++){
if(searchGrid[j][i] == searchGrid[j+1][i] &&
searchGrid[j+1][i] == searchGrid[j+2][i])
return searchGrid[j][i];
}
}
throw new setNotFoundException("For the following array a search
will result in a triple NOT being found and throwing the
SetNotFoundException");
}
public static void main (String[] args) throws
java.lang.Exception
{
// your code goes here
Scanner sc = new
Scanner(System.in);
char []alphabet = { 'a', 'b', 'c',
'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u',
'v', 'w', 'x', 'y', 'z' };
char[][] searchGrid = new
char[10][10];
// for(int i =
0;i<10;i++){
// for(int j =
0;j<10;j++){
// char ch =
sc.next().charAt(0);
// searchGrid[i][j] = ch;
// }
// }
// The following is a code to generate random characters to be
place inside searchGrid if manual
// entry is required then commnt this code and uncomment above
code.
for(int i = 0;i<10;i++){
for(int j = 0;j<10;j++){
char ch = alphabet[(int) (Math.random() * 10 % MAX)];
searchGrid[i][j] = ch;
}
}
char c=
setSearchChar(searchGrid);
System.out.println("For the
following array a search will result in a triple being found and
returning the character " + c);
}
}
/* The code has been compiled in the code chef compiler. Tested for about 20 random results seems workable please let me know if some error persists */