Skip to content

Commit 79d0d12

Browse files
authored
Update tests word-search (#2750)
* Update tests word-serach * Format changes
1 parent aa2b33d commit 79d0d12

File tree

4 files changed

+234
-127
lines changed

4 files changed

+234
-127
lines changed

exercises/practice/word-search/.meta/config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"contributors": [
66
"hyuko21",
77
"ivanvotti",
8+
"jagdish-15",
89
"msomji",
910
"rchavarria",
1011
"SleeplessByte"

exercises/practice/word-search/.meta/proof.ci.js

Lines changed: 49 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,11 @@ function searchHorizontally({ word, grid }) {
2626
}
2727
rowIndex += 1;
2828
}
29-
return false;
29+
return undefined;
3030
}
3131

3232
function flipCoordinates(coords) {
33-
if (!coords) {
34-
return undefined;
35-
}
33+
if (!coords) return undefined;
3634
return {
3735
start: coords.start.reverse(),
3836
end: coords.end.reverse(),
@@ -41,118 +39,93 @@ function flipCoordinates(coords) {
4139

4240
function flipGrid(grid) {
4341
return [...grid[0]]
44-
.map((col, c) => grid.map((row, r) => grid[r][c]))
42+
.map((_, c) => grid.map((row) => row[c]))
4543
.map((row) => row.join(''));
4644
}
4745

48-
function diagonalFind(r, c, word, grid, rIncrement, outOfRange, buildCoords) {
46+
function diagonalFind(r, c, word, grid, rIncrement, cIncrement) {
4947
let currentRow = r;
5048
let currentColumn = c;
5149
let foundLetters = '';
5250
const startR = r + 1;
5351
const startC = c + 1;
54-
let result;
55-
word.split('').forEach((letter) => {
52+
53+
for (const letter of word) {
54+
// Bounds check
5655
if (
57-
!outOfRange(
58-
currentRow,
59-
currentColumn,
60-
word.length,
61-
grid[currentRow].length,
62-
foundLetters.length,
63-
)
56+
currentRow < 0 ||
57+
currentRow >= grid.length ||
58+
currentColumn < 0 ||
59+
currentColumn >= grid[currentRow].length
6460
) {
65-
const currLetterInGrid = grid[currentRow].charAt(currentColumn);
66-
currentColumn += 1;
67-
if (currLetterInGrid === letter) {
68-
foundLetters += currLetterInGrid;
69-
if (foundLetters === word) {
70-
result = buildCoords(startR, startC, currentRow, currentColumn);
71-
}
72-
currentRow += rIncrement;
73-
}
61+
return undefined;
7462
}
75-
});
76-
return result;
77-
}
78-
79-
function findAWordDiagonallyTopDown(r, c, word, grid) {
80-
function outOfRange(row, column, words, columns, letters) {
81-
return (
82-
row > columns - words + letters || column > columns - words + letters
83-
);
84-
}
85-
86-
function buildCoords(startR, startC, row, column) {
87-
return {
88-
start: [startR, startC],
89-
end: [row + 1, column],
90-
};
91-
}
92-
93-
return diagonalFind(r, c, word, grid, 1, outOfRange, buildCoords);
94-
}
9563

96-
function findAWordDiagonallyBottomUp(r, c, word, grid) {
97-
function outOfRange(row, column, words, columns, letters) {
98-
return row < words - letters - 1 || column > columns - words + letters;
99-
}
64+
const currLetterInGrid = grid[currentRow].charAt(currentColumn);
65+
if (currLetterInGrid === letter) {
66+
foundLetters += currLetterInGrid;
67+
if (foundLetters === word) {
68+
return {
69+
start: [startR, startC],
70+
end: [currentRow + 1, currentColumn + 1],
71+
};
72+
}
73+
} else {
74+
return undefined;
75+
}
10076

101-
function buildCoords(startR, startC, row, column) {
102-
return {
103-
start: [startR, startC],
104-
end: [row + 1, column],
105-
};
77+
currentRow += rIncrement;
78+
currentColumn += cIncrement;
10679
}
10780

108-
return diagonalFind(r, c, word, grid, -1, outOfRange, buildCoords);
109-
}
110-
111-
function formatCoordinates(coords, isReversed) {
112-
return {
113-
true: {
114-
start: coords.end,
115-
end: coords.start,
116-
},
117-
false: coords,
118-
}[isReversed];
81+
return undefined;
11982
}
12083

121-
function searchDiagonally({ word, grid, isReversed = false, fromTop = true }) {
84+
function searchDiagonally({ word, grid, fromTop = true, reversed = false }) {
12285
const rIncrement = fromTop ? 1 : -1;
12386
const startRow = fromTop ? 0 : grid.length - 1;
124-
const endRow = fromTop ? (r) => r < grid.length : (r) => r > 0;
125-
const findDirection = fromTop
126-
? findAWordDiagonallyTopDown
127-
: findAWordDiagonallyBottomUp;
87+
const endRow = fromTop ? (r) => r < grid.length : (r) => r >= 0;
12888

12989
for (let r = startRow; endRow(r); r += rIncrement) {
13090
for (let c = 0; c < grid[r].length; c += 1) {
131-
const possibleCoords = findDirection(r, c, word, grid);
132-
if (possibleCoords) {
133-
return formatCoordinates(possibleCoords, isReversed);
91+
const dirs = [
92+
[1, 1], // top-left to bottom-right
93+
[1, -1], // top-right to bottom-left
94+
[-1, 1], // bottom-left to top-right
95+
[-1, -1], // bottom-right to top-left
96+
];
97+
98+
for (const [dr, dc] of dirs) {
99+
const possible = diagonalFind(r, c, word, grid, dr, dc);
100+
if (possible) {
101+
if (reversed) {
102+
return { start: possible.end, end: possible.start };
103+
}
104+
return possible;
105+
}
134106
}
135107
}
136108
}
137109

138-
if (!isReversed) {
139-
// now find the reversed version
110+
// Try reversed word
111+
if (!reversed) {
140112
const reversedWord = [...word].reverse().join('');
141113
return searchDiagonally({
142114
word: reversedWord,
143115
grid,
144-
isReversed: true,
145116
fromTop,
117+
reversed: true,
146118
});
147119
}
120+
148121
return undefined;
149122
}
150123

151124
function findWordInAnyDirection(word, grid) {
152125
return (
153126
searchHorizontally({ word, grid }) ||
154127
flipCoordinates(searchHorizontally({ word, grid: flipGrid(grid) })) ||
155-
searchDiagonally({ word, grid }) ||
128+
searchDiagonally({ word, grid, fromTop: true }) ||
156129
searchDiagonally({ word, grid, fromTop: false })
157130
);
158131
}

exercises/practice/word-search/.meta/tests.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,15 @@ description = "Should locate words written top right to bottom left"
6868

6969
[695531db-69eb-463f-8bad-8de3bf5ef198]
7070
description = "Should fail to locate a word that is not in the puzzle"
71+
72+
[fda5b937-6774-4a52-8f89-f64ed833b175]
73+
description = "Should fail to locate words that are not on horizontal, vertical, or diagonal lines"
74+
75+
[5b6198eb-2847-4e2f-8efe-65045df16bd3]
76+
description = "Should not concatenate different lines to find a horizontal word"
77+
78+
[eba44139-a34f-4a92-98e1-bd5f259e5769]
79+
description = "Should not wrap around horizontally to find a word"
80+
81+
[cd1f0fa8-76af-4167-b105-935f78364dac]
82+
description = "Should not wrap around vertically to find a word"

0 commit comments

Comments
 (0)