Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion src/count-islands/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,37 @@
function countIslands(grid) {}
function countIslands(grid) {
const locateIsland = (grid, x, y, checked) => {
if (x < 0 || x > grid.length - 1 || y < 0 || y > grid[x].length - 1) return;

if (checked[x][y] === true) return;

checked[x][y] = true;
if (grid[x][y] === 0) return;

// @traversing the 2D Array
locateIsland(grid, x - 1, y, checked);
locateIsland(grid, x + 1, y, checked);
locateIsland(grid, x, y - 1, checked);
locateIsland(grid, x, y + 1, checked);
};

// @creation of boolean 2D Array of equal size with grid
// to help keep track of the visited node
let checked = [];
let count = 0;
for (let i = 0; i < grid.length; i++) {
checked.push([]);
}

for (let x = 0; x < grid.length; x++) {
for (let y = 0; y < grid[x].length; y++) {
if (!checked[x][y] && grid[x][y] === 1) {
count++;
locateIsland(grid, x, y, checked);
}
checked[x][y] = true;
}
}
return count;
}

module.exports = countIslands;
10 changes: 4 additions & 6 deletions src/count-islands/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@ describe("Count Islands", () => {

test("4x4 island", () => {
const grid = [
[
[1, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1],
[0, 1, 0, 0],
],
[1, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1],
[0, 1, 0, 0],
];

expect(countIslands(grid)).toBe(4);
Expand Down
46 changes: 45 additions & 1 deletion src/word-search/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,47 @@
function wordSearch(words, word) {}
function wordSearch(words, word) {
const findLink = (words, x, y, checked, word_index) => {
if (x < 0 || x > words.length - 1 || y < 0 || y > words[x].length - 1)
return;

if (checked[x][y] === true) return;

if (words[x][y] !== word[word_index]) return;

checked[x][y] = true;

if (word_index === word.length - 1) {
return true;
}

// @traversing four possible directions (left, right, up, down)
if (
findLink(words, x - 1, y, checked, word_index + 1) ||
findLink(words, x + 1, y, checked, word_index + 1) ||
findLink(words, x, y - 1, checked, word_index + 1) ||
findLink(words, x, y + 1, checked, word_index + 1)
) {
return true;
} else {
checked[x][y] = false;
}
};

// @boolean 2D Array of equal size with words
// to help keep track of the visited node
let checked = [];
for (let i = 0; i < words.length; i++) {
checked.push([]);
}

for (let x = 0; x < words.length; x++) {
for (let y = 0; y < words[x].length; y++) {
if (!checked[x][y] && words[x][y] === word[0]) {
if (findLink(words, x, y, checked, 0)) return true;
}
}
}

return false;
}

module.exports = wordSearch;
24 changes: 24 additions & 0 deletions src/word-search/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,28 @@ describe("Word Search", () => {

expect(wordSearch(words, word)).toBe(true);
});

test("You've got the word", () => {
const words = [
["C", "A", "A"],
["A", "A", "A"],
["B", "C", "D"],
];

const word = "AAB";

expect(wordSearch(words, word)).toBe(true);
});

test("Because you can See", () => {
const words = [
["A", "B", "C", "E"],
["S", "F", "C", "S"],
["A", "D", "E", "E"],
];

const word = "SEE";

expect(wordSearch(words, word)).toBe(true);
});
});