patternjavaMinor
Find word in two dimensional char array
Viewed 0 times
arraychartwowordfinddimensional
Problem
Task: I have two dimensional array of chars filled with random values
In my program, I go through all characters and try to find word in all dimensions.
I feel there is more intelligent way to do this task which will:
Any advice?
[a-z] and need to find if the word ala appears vertically, horizontally or diagonally in it.In my program, I go through all characters and try to find word in all dimensions.
I feel there is more intelligent way to do this task which will:
- Work with words of different length without hard-coding.
- Replace bunch of if statements so it will be easier.
Any advice?
import java.util.Random;
public class Ala {
public static void main(String[] args) {
Random randomGenerator = new Random();
char[][] arr = new char[50][50];
for (int i = 0; i 1) {
if (arr[i][j] == 'a' && arr[i][j - 1] == 'l' && arr[i][j - 2] == 'a') {//left
ala = true;
System.out.println(i + " " + j);
}
}
if (j1) {
if (arr[i][j] == 'a' && arr[i-1][j] == 'l' && arr[i - 2][j] == 'a') {//top
ala = true;
System.out.println(i + " " + j);
}
}
if (i1 && j>1) {
if (arr[i][j] == 'a' && arr[i - 1][j - 1] == 'l' && arr[i - 2][j - 2] == 'a') {//top&left
ala = true;
System.out.println(i + " " + j);
}
}
if (i>1 && j1) {
if (arr[i][j] == 'a' && arr[i + 1][j - 1] == 'l' && arr[i + 2][j - 2] == 'a') {//bot&left
ala = true;
System.out.println(i + " " + j);
}
}
}
}
System.out.println(ala);
}
}Solution
Your code is indeed sub-optimal.
When looking at a task do not start writing some loops, but think about it is made up of simpler tasks.
A word is in a two-dimensional crossword grid if any of the below is True:
So, the general outline of the method may be:
Now you just have to translate this pseudocode to real code and implement the three
This is my suggestion because:
When looking at a task do not start writing some loops, but think about it is made up of simpler tasks.
A word is in a two-dimensional crossword grid if any of the below is True:
- The word is in any of the rows.
- The word is in any of the columns.
- The word is in any of the diagonals.
So, the general outline of the method may be:
class WordGrid {
public contains(String word) {
return any(word in row for row in getRows()) ||
any(word in column for column in getColumns()) ||
any(word in diagonal for diagonal in getDiagonals())
}
}Now you just have to translate this pseudocode to real code and implement the three
get methods and the problem will be solved.This is my suggestion because:
- The general idea can be understood at a glance, if the reader wants more details he can read the helper methods.
- It is made of many small pieces, that may be reused and are simpler to write on their own.
- It is general working, for any grid and word size.
Code Snippets
class WordGrid {
public contains(String word) {
return any(word in row for row in getRows()) ||
any(word in column for column in getColumns()) ||
any(word in diagonal for diagonal in getDiagonals())
}
}Context
StackExchange Code Review Q#111497, answer score: 5
Revisions (0)
No revisions yet.