patterncppMinor
The Grid Search
Viewed 0 times
thegridsearch
Problem
Given a 2D array of digits, try to find the location of a given 2D
pattern of digits.
Input Format
The first line contains an integer,
cases.
The first line contains two space-separated integers,
indicating the number of rows and columns in the grid
respectively.
This is followed by
represent the grid
The following line contains two space-separated integers,
indicating the number of rows and columns in the pattern grid
This is followed by
represent the pattern
Output Format
Display
larger grid
will be case sensitive.
Taken from HackerRank challenge "The Grid Search"
Please provide any tips from efficiency to readability.
```
#include
#include
#include
#include
using namespace std;
int main () {
int testCases;
cin >> testCases;
// Iterate through each test case
for (int i = 0; i > rows >> cols;
vector grid(rows);
for (int j = 0; j > grid[j];
}
/ PATTERN GRID /
// Iterate through each row of the pattern grid and store it in 'pattern' a vector of strings
int patternRows, patternCols;
cin >> patternRows >> patternCols;
vector pattern(patternRows);
for (int j = 0; j > pattern[j];
}
/ GRID INSPECTION /
bool found = false; // To know when to leave the loop and what answer to return
// Iterate through each row until the pattern would extend off the grid
for (int j = 0; j <= (rows - patternRows); j++) {
// Iterate through each column until the pattern would extend off the grid
for (
pattern of digits.
Input Format
The first line contains an integer,
T, which is the number of testcases.
T test cases follow, each having a structure as described below:The first line contains two space-separated integers,
R and C,indicating the number of rows and columns in the grid
G,respectively.
This is followed by
R lines, each with a string of C digits, whichrepresent the grid
G.The following line contains two space-separated integers,
r and c,indicating the number of rows and columns in the pattern grid
P.This is followed by
r lines, each with a string of c digits, whichrepresent the pattern
P.Output Format
Display
YES or NO, depending on whether (or not) you find that thelarger grid
G contains the rectangular pattern P. The evaluationwill be case sensitive.
Taken from HackerRank challenge "The Grid Search"
Please provide any tips from efficiency to readability.
```
#include
#include
#include
#include
using namespace std;
int main () {
int testCases;
cin >> testCases;
// Iterate through each test case
for (int i = 0; i > rows >> cols;
vector grid(rows);
for (int j = 0; j > grid[j];
}
/ PATTERN GRID /
// Iterate through each row of the pattern grid and store it in 'pattern' a vector of strings
int patternRows, patternCols;
cin >> patternRows >> patternCols;
vector pattern(patternRows);
for (int j = 0; j > pattern[j];
}
/ GRID INSPECTION /
bool found = false; // To know when to leave the loop and what answer to return
// Iterate through each row until the pattern would extend off the grid
for (int j = 0; j <= (rows - patternRows); j++) {
// Iterate through each column until the pattern would extend off the grid
for (
Solution
For readability I'd suggest breaking the algorithm up into a number of functions. Any time you find you need to write "heading" comments, it suggests that the code under that heading could go into a separate function. Breaking the code up into functions like this makes the code easier to reason about and, in my experience, easier to test and debug.
Your main() could look like this:
While the function ReadGrid would be simple enough, the new IsPatternInGrid function would still be quite complex and could possibly also benefit from factoring out some its code into yet another function.
Other things that I changed in the block of code above aren't as important for readability but are common for coding standards:
Your main() could look like this:
int main () {
int testCases;
std::cin >> testCases;
for (int i = 0; i mainGrid = ReadGrid(std::cin);
std::vector pattern = ReadGrid(std::cin);
if (IsPatternInGrid(mainGrid, pattern)) {
std::cout << "YES\n";
} else {
std::cout << "NO\n";
}
}
}While the function ReadGrid would be simple enough, the new IsPatternInGrid function would still be quite complex and could possibly also benefit from factoring out some its code into yet another function.
Other things that I changed in the block of code above aren't as important for readability but are common for coding standards:
- No
using namespace std;- usestd::where needed.
- Prefer
'\n'overstd::endlexcept when flushing the output buffer is explicitly needed.
- No need for a return 0 as it is the default return value for
int main.
Code Snippets
int main () {
int testCases;
std::cin >> testCases;
for (int i = 0; i < testCases; ++i) {
std::vector<std::string> mainGrid = ReadGrid(std::cin);
std::vector<std::string> pattern = ReadGrid(std::cin);
if (IsPatternInGrid(mainGrid, pattern)) {
std::cout << "YES\n";
} else {
std::cout << "NO\n";
}
}
}Context
StackExchange Code Review Q#117073, answer score: 2
Revisions (0)
No revisions yet.