Skip to content

Commit a6fabe0

Browse files
committed
Update tests word-serach
1 parent 7c5a6f0 commit a6fabe0

File tree

4 files changed

+188
-85
lines changed

4 files changed

+188
-85
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: 51 additions & 81 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,90 @@ 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
88+
? (r) => r < grid.length
89+
: (r) => r >= 0;
12890

12991
for (let r = startRow; endRow(r); r += rIncrement) {
13092
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);
93+
const dirs = [
94+
[1, 1], // top-left to bottom-right
95+
[1, -1], // top-right to bottom-left
96+
[-1, 1], // bottom-left to top-right
97+
[-1, -1], // bottom-right to top-left
98+
];
99+
100+
for (const [dr, dc] of dirs) {
101+
const possible = diagonalFind(r, c, word, grid, dr, dc);
102+
if (possible) {
103+
if (reversed) {
104+
return { start: possible.end, end: possible.start };
105+
}
106+
return possible;
107+
}
134108
}
135109
}
136110
}
137111

138-
if (!isReversed) {
139-
// now find the reversed version
112+
// Try reversed word
113+
if (!reversed) {
140114
const reversedWord = [...word].reverse().join('');
141-
return searchDiagonally({
142-
word: reversedWord,
143-
grid,
144-
isReversed: true,
145-
fromTop,
146-
});
115+
return searchDiagonally({ word: reversedWord, grid, fromTop, reversed: true });
147116
}
117+
148118
return undefined;
149119
}
150120

151121
function findWordInAnyDirection(word, grid) {
152122
return (
153123
searchHorizontally({ word, grid }) ||
154124
flipCoordinates(searchHorizontally({ word, grid: flipGrid(grid) })) ||
155-
searchDiagonally({ word, grid }) ||
125+
searchDiagonally({ word, grid, fromTop: true }) ||
156126
searchDiagonally({ word, grid, fromTop: false })
157127
);
158128
}

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"

exercises/practice/word-search/word-search.spec.js

Lines changed: 124 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ describe('vertical directions', () => {
543543
});
544544

545545
describe("word doesn't exist", () => {
546-
xtest('should fail to locate a word that is not in the puzzle', () => {
546+
xtest('Should fail to locate a word that is not in the puzzle', () => {
547547
const grid = [
548548
'jefblpepre',
549549
'camdcimgtc',
@@ -556,13 +556,133 @@ describe('vertical directions', () => {
556556
'jalaycalmp',
557557
'clojurermt',
558558
];
559-
559+
560560
const expectedResults = {
561-
fail: undefined,
561+
clojure: {
562+
start: [10, 1],
563+
end: [10, 7],
564+
},
565+
elixir: {
566+
start: [5, 6],
567+
end: [5, 1],
568+
},
569+
ecmascript: {
570+
start: [1, 10],
571+
end: [10, 10],
572+
},
573+
rust: {
574+
start: [5, 9],
575+
end: [2, 9],
576+
},
577+
java: {
578+
start: [1, 1],
579+
end: [4, 4],
580+
},
581+
lua: {
582+
start: [9, 8],
583+
end: [7, 6],
584+
},
585+
lisp: {
586+
start: [6, 3],
587+
end: [3, 6],
588+
},
589+
ruby: {
590+
start: [6, 8],
591+
end: [9, 5],
592+
},
593+
haskell: undefined,
562594
};
595+
563596
const wordSearch = new WordSearch(grid);
597+
598+
expect(
599+
wordSearch.find([
600+
'clojure',
601+
'elixir',
602+
'ecmascript',
603+
'rust',
604+
'java',
605+
'lua',
606+
'lisp',
607+
'ruby',
608+
'haskell',
609+
]),
610+
).toEqual(expectedResults);
611+
});
564612

565-
expect(wordSearch.find(['fail'])).toEqual(expectedResults);
613+
xtest('Should fail to locate words that are not on horizontal, vertical, or diagonal lines', () => {
614+
const grid = [
615+
'abc',
616+
'def',
617+
];
618+
619+
const expectedResults = {
620+
aef: undefined,
621+
ced: undefined,
622+
abf: undefined,
623+
cbd: undefined,
624+
};
625+
626+
const wordSearch = new WordSearch(grid);
627+
628+
expect(
629+
wordSearch.find(['aef', 'ced', 'abf', 'cbd']),
630+
).toEqual(expectedResults);
631+
});
632+
633+
xtest('Should not concatenate different lines to find a horizontal word', () => {
634+
const grid = [
635+
'abceli',
636+
'xirdfg',
637+
];
638+
639+
const expectedResults = {
640+
elixir: undefined,
641+
};
642+
643+
const wordSearch = new WordSearch(grid);
644+
645+
expect(
646+
wordSearch.find(['elixir']),
647+
).toEqual(expectedResults);
648+
});
649+
650+
xtest('Should not wrap around horizontally to find a word', () => {
651+
const grid = [
652+
'silabcdefp',
653+
];
654+
655+
const expectedResults = {
656+
lisp: undefined,
657+
};
658+
659+
const wordSearch = new WordSearch(grid);
660+
661+
expect(
662+
wordSearch.find(['lisp']),
663+
).toEqual(expectedResults);
566664
});
665+
666+
xtest('Should not wrap around vertically to find a word', () => {
667+
const grid = [
668+
's',
669+
'u',
670+
'r',
671+
'a',
672+
'b',
673+
'c',
674+
't',
675+
];
676+
677+
const expectedResults = {
678+
rust: undefined,
679+
};
680+
681+
const wordSearch = new WordSearch(grid);
682+
683+
expect(
684+
wordSearch.find(['rust']),
685+
).toEqual(expectedResults);
686+
});
567687
});
568688
});

0 commit comments

Comments
 (0)