From dee2fcee10cf08652b40e089b9bd9df0dc73b937 Mon Sep 17 00:00:00 2001 From: warami13 Date: Thu, 4 Jun 2020 12:29:42 +0100 Subject: [PATCH 1/2] add count-island solution --- src/count-islands/index.js | 51 ++++++- src/count-islands/test.js | 274 +++++++++++++++++++------------------ src/word-search/index.js | 67 ++++++++- 3 files changed, 253 insertions(+), 139 deletions(-) diff --git a/src/count-islands/index.js b/src/count-islands/index.js index ec88f6b..ce94a3c 100644 --- a/src/count-islands/index.js +++ b/src/count-islands/index.js @@ -1,3 +1,50 @@ -function countIslands(grid) {} +/* +- use recursion method (method that calls itself) +- use depth first search -module.exports = countIslands; +- create a variable to hold the count +- figure out the edge cases + - if there are no islands then return 0; +- the first loop i will be used to keep track of the outer loop i.e. the rows +- the second loop j will be used to keep track of the inner loop i.e. the columns +- if the grid at any point has 1 it should increase the count; + +- figure out how to keep track of the island + - find an island we change it to 0 to mark as done? i.e. modifying +*/ + +function countIslands(grid) { + let islandCount = 0; + let rows = grid.length + let columns = grid[0].length + + if (rows === 0) { + return 0; + } + + for (let i = 0; i < rows; i++) { + for (let j = 0; j < columns; j++) { + if (grid[i][j] === 1) { + checkIsland(i, j, grid) + islandCount++; + console.log(islandCount); + + } + } + } + return islandCount; + + function checkIsland(i, j, grid) { + if ((i < 0 || j < 0) || (i >= rows || j >= columns) || grid[i][j] !== 1) { + return; + } + grid[i][j] = 0; + + checkIsland(i + 1, j, grid) + checkIsland(i - 1, j, grid) + checkIsland(i, j - 1, grid) + checkIsland(i, j + 1, grid) + } +}; + +module.exports = countIslands; \ No newline at end of file diff --git a/src/count-islands/test.js b/src/count-islands/test.js index 4ee76ba..2e94806 100644 --- a/src/count-islands/test.js +++ b/src/count-islands/test.js @@ -1,138 +1,142 @@ const countIslands = require("."); describe("Count Islands", () => { - test("flat island", () => { - const grid = [[1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1]]; - - expect(countIslands(grid)).toBe(3); - }); - - test("no land in flat sight", () => { - const grid = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]; - - expect(countIslands(grid)).toBe(0); - }); - - test("3x3 island", () => { - const grid = [ - [1, 1, 0], - [0, 0, 1], - [1, 0, 1], - ]; - - expect(countIslands(grid)).toBe(3); - }); - - test("4x4 island", () => { - const grid = [ - [ - [1, 1, 0, 0], - [0, 0, 1, 0], - [0, 0, 0, 1], - [0, 1, 0, 0], - ], - ]; - - expect(countIslands(grid)).toBe(4); - }); - - test("5x5 island", () => { - const grid = [ - [1, 1, 0, 0, 0], - [0, 1, 0, 0, 1], - [1, 0, 0, 1, 1], - [0, 0, 0, 0, 0], - [1, 0, 1, 0, 1], - ]; - - expect(countIslands(grid)).toBe(6); - }); - - test("Example island - 1", () => { - const grid = [ - [1, 1, 0, 0, 0], - [1, 1, 0, 0, 0], - [0, 0, 1, 0, 0], - [0, 0, 0, 1, 1], - ]; - - expect(countIslands(grid)).toBe(3); - }); - - test("Example island - 2", () => { - const grid = [ - [1, 1, 1, 1, 0], - [1, 1, 0, 1, 0], - [1, 1, 0, 0, 0], - [0, 0, 0, 0, 0], - ]; - - expect(countIslands(grid)).toBe(1); - }); - - test("large island - 1", () => { - const grid = [ - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [0, 0, 0, 0, 0], - [0, 0, 0, 0, 0], - [1, 0, 0, 0, 1], - [0, 0, 0, 0, 0], - [0, 1, 0, 1, 0], - [0, 0, 0, 0, 0], - [0, 0, 0, 0, 0], - [0, 0, 0, 0, 0], - [0, 0, 1, 0, 0], - [0, 0, 0, 0, 0], - [1, 0, 0, 0, 1], - [0, 0, 0, 0, 0], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - ]; - - expect(countIslands(grid)).toBe(9); - }); - - test("all lands - 5x18", () => { - const grid = [ - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - [1, 1, 1, 1, 1], - ]; - - expect(countIslands(grid)).toBe(1); - }); - - test("large lands - 20x9", () => { - const grid = [ - [1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1], - [1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1], - [1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0], - [1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1], - [1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0], - [1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1], - [1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0], - [1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1], - [1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0], - ]; - - expect(countIslands(grid)).toBe(10); - }); -}); + test("flat island", () => { + const grid = [ + [1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1] + ]; + + expect(countIslands(grid)).toBe(3); + }); + + test("no land in flat sight", () => { + const grid = [ + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] + ]; + + expect(countIslands(grid)).toBe(0); + }); + + test("3x3 island", () => { + const grid = [ + [1, 1, 0], + [0, 0, 1], + [1, 0, 1], + ]; + + expect(countIslands(grid)).toBe(3); + }); + + test("4x4 island", () => { + const grid = [ + + [1, 1, 0, 0], + [0, 0, 1, 0], + [0, 0, 0, 1], + [0, 1, 0, 0], + + ]; + + expect(countIslands(grid)).toBe(4); + }); + + test("5x5 island", () => { + const grid = [ + [1, 1, 0, 0, 0], + [0, 1, 0, 0, 1], + [1, 0, 0, 1, 1], + [0, 0, 0, 0, 0], + [1, 0, 1, 0, 1], + ]; + + expect(countIslands(grid)).toBe(6); + }); + + test("Example island - 1", () => { + const grid = [ + [1, 1, 0, 0, 0], + [1, 1, 0, 0, 0], + [0, 0, 1, 0, 0], + [0, 0, 0, 1, 1], + ]; + + expect(countIslands(grid)).toBe(3); + }); + + test("Example island - 2", () => { + const grid = [ + [1, 1, 1, 1, 0], + [1, 1, 0, 1, 0], + [1, 1, 0, 0, 0], + [0, 0, 0, 0, 0], + ]; + + expect(countIslands(grid)).toBe(1); + }); + + test("large island - 1", () => { + const grid = [ + [1, 1, 1, 1, 1], + [1, 1, 1, 1, 1], + [1, 1, 1, 1, 1], + [0, 0, 0, 0, 0], + [0, 0, 0, 0, 0], + [1, 0, 0, 0, 1], + [0, 0, 0, 0, 0], + [0, 1, 0, 1, 0], + [0, 0, 0, 0, 0], + [0, 0, 0, 0, 0], + [0, 0, 0, 0, 0], + [0, 0, 1, 0, 0], + [0, 0, 0, 0, 0], + [1, 0, 0, 0, 1], + [0, 0, 0, 0, 0], + [1, 1, 1, 1, 1], + [1, 1, 1, 1, 1], + [1, 1, 1, 1, 1], + ]; + + expect(countIslands(grid)).toBe(9); + }); + + test("all lands - 5x18", () => { + const grid = [ + [1, 1, 1, 1, 1], + [1, 1, 1, 1, 1], + [1, 1, 1, 1, 1], + [1, 1, 1, 1, 1], + [1, 1, 1, 1, 1], + [1, 1, 1, 1, 1], + [1, 1, 1, 1, 1], + [1, 1, 1, 1, 1], + [1, 1, 1, 1, 1], + [1, 1, 1, 1, 1], + [1, 1, 1, 1, 1], + [1, 1, 1, 1, 1], + [1, 1, 1, 1, 1], + [1, 1, 1, 1, 1], + [1, 1, 1, 1, 1], + [1, 1, 1, 1, 1], + [1, 1, 1, 1, 1], + [1, 1, 1, 1, 1], + ]; + + expect(countIslands(grid)).toBe(1); + }); + + test("large lands - 20x9", () => { + const grid = [ + [1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1], + [1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1], + [1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0], + [1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1], + [1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0], + [1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1], + [1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0], + [1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1], + [1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0], + ]; + + expect(countIslands(grid)).toBe(10); + }); +}); \ No newline at end of file diff --git a/src/word-search/index.js b/src/word-search/index.js index 2e385e5..19fd192 100644 --- a/src/word-search/index.js +++ b/src/word-search/index.js @@ -1,3 +1,66 @@ -function wordSearch(words, word) {} +/* +- use recursion method (method that calls itself) +- use depth first search -module.exports = wordSearch; + +- if there are no words return false; +- the first loop i will be used to keep track of the outer loop i.e. the rows +- the second loop j will be used to keep track of the inner loop i.e. the columns +- if the grid at any point has the letters it should push to the variable; + +- figure out how to keep track of the word search + - modify? +*/ + +function wordSearch(words, word) { + let rows = words.length + let columns = words[0].length + if (word === '') return false; + + for (let i = 0; i < rows; i++) { + for (let j = 0; j < columns; j++) { + if (words[i][j] === word[i]) { + console.log(word[i]); + if (wordCheck(i, j)) return true; + } + } + } + return false; + + function wordCheck(i, j) { + console.log(i, j); + + if ((i === word.length)) return true; + if (!words[i] || !words[i][j]) return false; + + + if (wordCheck(i + 1, j)) return true; + if (wordCheck(i - 1, j)) return true; + if (wordCheck(i, j - 1)) return true; + if (wordCheck(i, j + 1)) return true; + + return false; + } +} + +// words = [ +// ["C", "D", "Y", "C", "X"], +// ["A", "N", "Y", "Z", "X"], +// ["T", "F", "Z", "A", "T"], +// ["M", "D", "B", "U", "T"], +// ]; + +// word = "CAT"; + +words = [ + ["D", "S", "A", "N", "C"], + ["I", "N", "O", "I", "T"], + ["T", "A", "T", "R", "O"], + ["M", "F", "O", "U", "T"], +]; + +word = "SANCTIONS"; +console.log(wordSearch(words, word)); + + +module.exports = wordSearch; \ No newline at end of file From 963331010b8aad41c91f3a8b8149bb1f371d6b74 Mon Sep 17 00:00:00 2001 From: warami13 Date: Sat, 20 Jun 2020 09:07:44 +0100 Subject: [PATCH 2/2] add solution for word-search --- src/word-search/index.js | 71 ++++++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 32 deletions(-) diff --git a/src/word-search/index.js b/src/word-search/index.js index 19fd192..b458ea1 100644 --- a/src/word-search/index.js +++ b/src/word-search/index.js @@ -2,46 +2,42 @@ - use recursion method (method that calls itself) - use depth first search - -- if there are no words return false; - the first loop i will be used to keep track of the outer loop i.e. the rows - the second loop j will be used to keep track of the inner loop i.e. the columns -- if the grid at any point has the letters it should push to the variable; - -- figure out how to keep track of the word search - - modify? +- if the letters in the words at any point is equal to the word it should return true +- keep track of the word search by marking letters as visited once we've iterated over it + - */ function wordSearch(words, word) { - let rows = words.length - let columns = words[0].length - if (word === '') return false; - - for (let i = 0; i < rows; i++) { - for (let j = 0; j < columns; j++) { - if (words[i][j] === word[i]) { - console.log(word[i]); - if (wordCheck(i, j)) return true; - } + let row = words.length; + let column = words[0].length; + let wordLen = word.length; + + for (let i = 0; i < row; i++) { + for (let j = 0; j < column; j++) { + if (words[i][j] === word[0] && wordCheck(i, j, 0)) return true; } } return false; - function wordCheck(i, j) { - console.log(i, j); + function wordCheck(i, j, wordIndex) { + if (wordIndex === wordLen) return true; + if (i >= row || i < 0 || words[i][j] !== word[wordIndex]) return false; - if ((i === word.length)) return true; - if (!words[i] || !words[i][j]) return false; + words[i][j] = "V"; + if (wordCheck(i - 1, j, wordIndex + 1) || + wordCheck(i + 1, j, wordIndex + 1) || + wordCheck(i, j - 1, wordIndex + 1) || + wordCheck(i, j + 1, wordIndex + 1)) return true; - if (wordCheck(i + 1, j)) return true; - if (wordCheck(i - 1, j)) return true; - if (wordCheck(i, j - 1)) return true; - if (wordCheck(i, j + 1)) return true; - + words[i][j] = word[wordIndex]; return false; } -} + +}; + // words = [ // ["C", "D", "Y", "C", "X"], @@ -52,14 +48,25 @@ function wordSearch(words, word) { // word = "CAT"; -words = [ - ["D", "S", "A", "N", "C"], - ["I", "N", "O", "I", "T"], - ["T", "A", "T", "R", "O"], - ["M", "F", "O", "U", "T"], +// words = [ +// ["D", "S", "A", "N", "C"], +// ["I", "N", "O", "I", "T"], +// ["T", "A", "T", "R", "O"], +// ["M", "F", "O", "U", "T"], +// ]; + +// word = "SANCTIONS"; + +const words = [ + ["G", "A", "T", "J", "M"], + ["R", "G", "R", "G", "N"], + ["A", "M", "A", "K", "E"], + ["N", "M", "A", "E", "U"], + ["D", "L", "R", "R", "Q"], + ["M", "Y", "M", "A", "A"], ]; -word = "SANCTIONS"; +const word = "GRAMMARLY"; console.log(wordSearch(words, word));