From 7c5b63f5e1a7ae39c438cdbb526f02bdcce3e2ce Mon Sep 17 00:00:00 2001 From: Bond Date: Wed, 13 May 2020 21:23:05 +0100 Subject: [PATCH 1/4] Fix count-islands test --- src/count-islands/test.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/count-islands/test.js b/src/count-islands/test.js index 4ee76ba..e36a220 100644 --- a/src/count-islands/test.js +++ b/src/count-islands/test.js @@ -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); From 0b7e302cba3ae8d4c2a26b82d4b40472dd08935b Mon Sep 17 00:00:00 2001 From: chisom Date: Fri, 15 May 2020 12:58:11 +0100 Subject: [PATCH 2/4] add more test cases to word search --- src/word-search/test.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/word-search/test.js b/src/word-search/test.js index c5498a3..ee42557 100644 --- a/src/word-search/test.js +++ b/src/word-search/test.js @@ -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 ca See", () => { + const words = [ + ["A", "B", "C", "E"], + ["S", "F", "C", "S"], + ["A", "D", "E", "E"], + ]; + + const word = "SEE"; + + expect(wordSearch(words, word)).toBe(true); + }); }); From 2e62845276a5ee01508ca9a3e62ac20c08291d2f Mon Sep 17 00:00:00 2001 From: chisom Date: Fri, 15 May 2020 13:10:05 +0100 Subject: [PATCH 3/4] fix typo error --- src/word-search/test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/word-search/test.js b/src/word-search/test.js index ee42557..9a83251 100644 --- a/src/word-search/test.js +++ b/src/word-search/test.js @@ -135,7 +135,7 @@ describe("Word Search", () => { expect(wordSearch(words, word)).toBe(true); }); - test("Because you ca See", () => { + test("Because you can See", () => { const words = [ ["A", "B", "C", "E"], ["S", "F", "C", "S"], From a96e31bc53b664f296268cfd56298db736196cc1 Mon Sep 17 00:00:00 2001 From: ezego1 Date: Wed, 17 Jun 2020 21:51:06 +0100 Subject: [PATCH 4/4] Add word-search solution --- src/count-islands/index.js | 36 ++++++++++++++++++++++++++++- src/word-search/index.js | 46 +++++++++++++++++++++++++++++++++++++- 2 files changed, 80 insertions(+), 2 deletions(-) diff --git a/src/count-islands/index.js b/src/count-islands/index.js index ec88f6b..405fed0 100644 --- a/src/count-islands/index.js +++ b/src/count-islands/index.js @@ -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; diff --git a/src/word-search/index.js b/src/word-search/index.js index 2e385e5..391bcb4 100644 --- a/src/word-search/index.js +++ b/src/word-search/index.js @@ -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;