From 3b30e2352ff28f479648444c2b77687f75a9eaf7 Mon Sep 17 00:00:00 2001 From: Hardey18 <35535871+Hardey18@users.noreply.github.com> Date: Tue, 16 Jun 2020 19:32:45 -0700 Subject: [PATCH 1/6] looped through the array and points to another function --- src/word-search/index.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/word-search/index.js b/src/word-search/index.js index 2e385e5..7bf6a26 100644 --- a/src/word-search/index.js +++ b/src/word-search/index.js @@ -1,3 +1,18 @@ -function wordSearch(words, word) {} +function wordSearch(words, word) { + if(word === "") return true + + for(let row = 0; row < words.length; row++) { + for(let col = 0; col < words[row].length; col++) { + if(words[row][col] === word[0]) { + if(dfs(0, row, col)) return true + } + } + } + return false + + function dfs(index, x, y) { + + } +} module.exports = wordSearch; From 3c3f2ae081fa531989f602911f2c1f15fda03d92 Mon Sep 17 00:00:00 2001 From: Hardey18 <35535871+Hardey18@users.noreply.github.com> Date: Tue, 16 Jun 2020 19:51:41 -0700 Subject: [PATCH 2/6] word search algorithm now working --- src/word-search/index.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/word-search/index.js b/src/word-search/index.js index 7bf6a26..7a7f740 100644 --- a/src/word-search/index.js +++ b/src/word-search/index.js @@ -4,14 +4,26 @@ function wordSearch(words, word) { for(let row = 0; row < words.length; row++) { for(let col = 0; col < words[row].length; col++) { if(words[row][col] === word[0]) { - if(dfs(0, row, col)) return true + if(search(0, row, col)) return true } } } return false - function dfs(index, x, y) { - + function search(index, x, y) { + if(index === word.length) return true + if(!words[x] || !words[x][y]) return false + if(words[x][y] !== "#" && words[x][y] === word[index]) { + let ch = words[x][y] + words[x][y] = "#" + + if(search(index + 1, x - 1, y)) return true + if(search(index + 1, x + 1, y)) return true + if(search(index + 1, x, y - 1)) return true + if(search(index + 1, x, y + 1)) return true + words[x][y] = ch + } + return false } } From 290b8f16c53f7a813c55388d6de52e9c46c1035c Mon Sep 17 00:00:00 2001 From: Hardey18 <35535871+Hardey18@users.noreply.github.com> Date: Tue, 16 Jun 2020 21:17:00 -0700 Subject: [PATCH 3/6] looping through the array and calling the search function --- src/count-islands/index.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/count-islands/index.js b/src/count-islands/index.js index ec88f6b..491c9a9 100644 --- a/src/count-islands/index.js +++ b/src/count-islands/index.js @@ -1,3 +1,13 @@ -function countIslands(grid) {} +function countIslands(grid) { + let count = 0 + for(let row = 0; row < grid.length; row++) { + for(let col = 0; col < grid[row].length; col++) { + if(!visited[row][col] && grid[row][col] === "1") { + count++ + markIsland(grid, row, col) + } + } + } +} module.exports = countIslands; From c2d1c6bcc4585e83e19ff63536bb68af92f03b68 Mon Sep 17 00:00:00 2001 From: Hardey18 <35535871+Hardey18@users.noreply.github.com> Date: Tue, 16 Jun 2020 23:51:43 -0700 Subject: [PATCH 4/6] completed --- src/count-islands/index.js | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/count-islands/index.js b/src/count-islands/index.js index 491c9a9..f1752e8 100644 --- a/src/count-islands/index.js +++ b/src/count-islands/index.js @@ -1,13 +1,33 @@ function countIslands(grid) { + let search = function(grid, row, col) { + if(grid[row] === undefined || grid[row][col] === undefined || grid[row][col] === 0) { + return 0 + } + + grid[row][col] = 0 + + search(grid, row - 1, col) + search(grid, row + 1, col) + search(grid, row, col - 1) + search(grid, row, col + 1) + + return + } + + if(grid.length === 0 || grid === null) { + return 0 + } + let count = 0 for(let row = 0; row < grid.length; row++) { for(let col = 0; col < grid[row].length; col++) { - if(!visited[row][col] && grid[row][col] === "1") { + if(grid[row][col] === 1) { count++ - markIsland(grid, row, col) + search(grid, row, col) } } } + return count } module.exports = countIslands; From 0fd666b690bc7a76ba2399443459554e2be2f05b Mon Sep 17 00:00:00 2001 From: Hardey18 <35535871+Hardey18@users.noreply.github.com> Date: Wed, 17 Jun 2020 00:03:41 -0700 Subject: [PATCH 5/6] .. --- src/word-search/index.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/word-search/index.js b/src/word-search/index.js index 7a7f740..fb682e3 100644 --- a/src/word-search/index.js +++ b/src/word-search/index.js @@ -14,14 +14,12 @@ function wordSearch(words, word) { if(index === word.length) return true if(!words[x] || !words[x][y]) return false if(words[x][y] !== "#" && words[x][y] === word[index]) { - let ch = words[x][y] words[x][y] = "#" if(search(index + 1, x - 1, y)) return true if(search(index + 1, x + 1, y)) return true if(search(index + 1, x, y - 1)) return true if(search(index + 1, x, y + 1)) return true - words[x][y] = ch } return false } From 60daea30899cd4a4de13a013b1f6734e4397828c Mon Sep 17 00:00:00 2001 From: Hardey18 <35535871+Hardey18@users.noreply.github.com> Date: Thu, 18 Jun 2020 18:25:56 -0700 Subject: [PATCH 6/6] all tests now passing --- src/count-islands/index.js | 33 +++++++++++++++------------------ src/count-islands/test.js | 10 ++++------ src/word-search/index.js | 16 ++++++++-------- 3 files changed, 27 insertions(+), 32 deletions(-) diff --git a/src/count-islands/index.js b/src/count-islands/index.js index f1752e8..2a132b0 100644 --- a/src/count-islands/index.js +++ b/src/count-islands/index.js @@ -1,26 +1,12 @@ function countIslands(grid) { - let search = function(grid, row, col) { - if(grid[row] === undefined || grid[row][col] === undefined || grid[row][col] === 0) { - return 0 - } - - grid[row][col] = 0 - - search(grid, row - 1, col) - search(grid, row + 1, col) - search(grid, row, col - 1) - search(grid, row, col + 1) - - return - } - - if(grid.length === 0 || grid === null) { - return 0 - } + // if(grid.length === 0 || grid === null) { + // return 0 + // } let count = 0 for(let row = 0; row < grid.length; row++) { for(let col = 0; col < grid[row].length; col++) { + debugger; if(grid[row][col] === 1) { count++ search(grid, row, col) @@ -28,6 +14,17 @@ function countIslands(grid) { } } return count + + function search(grid, row, col) { + if(grid[row] === undefined || grid[row][col] === undefined || grid[row][col] === 0) return + + grid[row][col] = 0 + + search(grid, row - 1, col) + search(grid, row + 1, col) + search(grid, row, col - 1) + search(grid, row, col + 1) + } } module.exports = countIslands; 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); diff --git a/src/word-search/index.js b/src/word-search/index.js index fb682e3..7c5d946 100644 --- a/src/word-search/index.js +++ b/src/word-search/index.js @@ -10,16 +10,16 @@ function wordSearch(words, word) { } return false - function search(index, x, y) { + function search(index, row1, col1) { if(index === word.length) return true - if(!words[x] || !words[x][y]) return false - if(words[x][y] !== "#" && words[x][y] === word[index]) { - words[x][y] = "#" + if(!words[row1] || !words[row1][col1]) return false + if(words[row1][col1] !== "#" && words[row1][col1] === word[index]) { + words[row1][col1] = "#" - if(search(index + 1, x - 1, y)) return true - if(search(index + 1, x + 1, y)) return true - if(search(index + 1, x, y - 1)) return true - if(search(index + 1, x, y + 1)) return true + if(search(index + 1, row1 - 1, col1)) return true + if(search(index + 1, row1 + 1, col1)) return true + if(search(index + 1, row1, col1 - 1)) return true + if(search(index + 1, row1, col1 + 1)) return true } return false }