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 0986ad25084e034e5b7562f38b92c364502beeda Mon Sep 17 00:00:00 2001 From: deyems Date: Sat, 20 Jun 2020 00:02:19 +0100 Subject: [PATCH 4/4] Count_Island and Word_Search algorithm challenge --- src/count-islands/index.js | 50 ++++++- src/word-search/index.js | 289 ++++++++++++++++++++++++++++++++++++- 2 files changed, 336 insertions(+), 3 deletions(-) diff --git a/src/count-islands/index.js b/src/count-islands/index.js index ec88f6b..9b708d1 100644 --- a/src/count-islands/index.js +++ b/src/count-islands/index.js @@ -1,3 +1,51 @@ -function countIslands(grid) {} +function countIslands(grid) { + if(grid === null || grid.length === 0) { + throw TypeError('Invalid parameter given'); + } + // const visited = []; + // grid.map((row,y) => { let newRow = []; + // row.map((el,x) => newRow.push(false) ); + // visited.push(newRow); + // }); + + let islandCounter = 0; + + for(let row = 0; row < grid.length; row++){ + for(let column = 0; column < grid[row].length; column++){ + if(grid[row][column] === 1){ + islandCounter += dfs(grid,row,column); + } + } + } +return islandCounter; + //END OF FUNCTION HERE +} + +function dfs(grid,row,column){ + if(row<0 || row>grid.length-1 || column < 0 || column > grid[row].length-1){ + return; + } + if(grid[row][column] === 0) return; + //Change current Value visited to ZERO + grid[row][column] = 0; + + dfs(grid,row-1,column); + dfs(grid,row+1,column); + dfs(grid,row,column-1); + dfs(grid,row,column+1); + return 1; +} + +// function getAdjacents(rowSize, columnSize, position /* [row, column] */) { +// const [row, column] = position; +// +// const topAdjacent = row === 0 ? false : [row - 1, column]; +// const rightAdjacent = column === columnSize ? false : [row, column + 1]; +// const bottomAdjacent = row === rowSize ? false : [row + 1, column]; +// const leftAdjacent = column === 0 ? false : [row, column -1] +// +// return [topAdjacent, rightAdjacent, bottomAdjacent, leftAdjacent].filter(Boolean) +// } + module.exports = countIslands; diff --git a/src/word-search/index.js b/src/word-search/index.js index 2e385e5..f07c055 100644 --- a/src/word-search/index.js +++ b/src/word-search/index.js @@ -1,3 +1,288 @@ -function wordSearch(words, word) {} +function wordSearch(words, word) { + if(!Array.isArray(words) || typeof word !== 'string'){ + throw TypeError('Invalid Parameters given, You must supply an array and a string'); + } -module.exports = wordSearch; + const depth = words.length-1; + const breadth = words[0].length - 1; + let idxCounter = 0; + let visitedPoints = []; + //cREATE Array To Check points Visited + for(let i = 0; i < words.length; i++){ + visitedPoints.push([]); + } + + function searchIsTrue(row,column,idx){ + //BASE CASES TO CUT THE RECURSION SHORT + if(row < 0 || row > depth || + column < 0 || column > breadth){ + return; + } + + + /*Testing what was happening inside the loop running*/ + // if(row >= 0 || row <= depth || + // column >= 0 || column <= breadth){ + // console.log(words[row][column]); + // } + + if(word[idx] !== words[row][column]) return; + + + if(visitedPoints[row][column] === true){ + return; + } + visitedPoints[row][column] = true; + + if(idx === word.length-1) return true; + + if(searchIsTrue(row-1,column,idx+1) || + searchIsTrue(row+1,column,idx+1) || + searchIsTrue(row,column-1,idx+1) || + searchIsTrue(row,column+1,idx+1)){ + return true + }else { + visitedPoints[row][column] = false; + return; + } + + } + + for(let row = 0; row <= depth; row++){ + for(let column = 0; column <= breadth; column++){ + if(words[row][column] === word[0] ) { + if(searchIsTrue(row,column, 0)) return true; + } + } + } + return false; + + } + module.exports = wordSearch; + +// function wordSearch(words, word) { +// if(!Array.isArray(words) || typeof word !== 'string'){ +// throw TypeError('Invalid Parameters given, You must supply an array and a string'); +// } +// +// const depth = words.length; +// const breadth = words[0].length; +// let idxCounter = 0; +// let visitedPoints = []; let emptyArr = []; +// for(let i = 0; i < words.length; i++){ +// visitedPoints.push([]); +// } +// console.log(visitedPoints); +// +// +// for(let row = 0; row < depth; row++){ +// for(let column = 0; column < breadth; column++){ +// if(words[row][column] === word[0] ) { +// if(searchIsTrue(row,column, 0)) return true; +// } +// } +// } +// return false; +// +// function searchIsTrue(row,column,idx){ +// console.log('is it reaching here?'); +// //BASE CASES TO CUT THE RECURSION SHORT +// +// if(row < 0 || row > words.length-1 || +// column < 0 || column > words[row].length-1){ +// return; +// } +// +// if(visitedPoints[row][column] === true){ +// return; +// } +// +// if(words[row][column] !== word[idx]) return; +// +// +// if(idx === word.length-1) return true; +// +// visitedPoints[row][column] = true; +// +// +// // let ch = words[row][column]; +// // words[row][column] = '#'; +// +// if(searchIsTrue(row-1,column,idx+1) || +// searchIsTrue(row+1,column,idx+1) || +// searchIsTrue(row,column-1,idx+1) || +// searchIsTrue(row,column+1,idx+1)){ +// return true +// }else { +// visitedPoints[row][column] = false; +// return; +// } +// +// } +// +// } +// +// } + + + +// function getAdjacents(rowSize, columnSize, position /* [row, column] */) { +// const [row, column] = position; +// +// const topAdjacent = row === 0 ? false : [row - 1, column]; +// const rightAdjacent = column === columnSize ? false : [row, column + 1]; +// const bottomAdjacent = row === rowSize ? false : [row + 1, column]; +// const leftAdjacent = column === 0 ? false : [row, column -1] +// +// return [topAdjacent, rightAdjacent, bottomAdjacent, leftAdjacent].filter(Boolean) +// } + + //FORM AN ADJACENCY MATRIX + + // let graphBlueprint = new Graph(); + // let adjList = makeAdjacencyList(words,graphBlueprint); + + /******* + function makeAdjacencyList(list,graph){ + //FORM A GRAPH FROM A 2-DIMENSIONAL MATRIX + const depth = list.length; + const breadth = list[0].length; + + for(let row = 0; row < list.length; row++ ){ + for(let col = 0; col < list[row].length; col++){ + graph.addVertex(list[row][col]); + + let neighbours = getAdjacents(depth-1,breadth-1,[row,col]); + // console.log('lump',neighbours); + neighbours.forEach(adjNodeIdx => { + graph.addEdge(list[row][col],list[adjNodeIdx[0]][adjNodeIdx[1]]); + }); + } // INNER FOR LOOP + } //OUTER FOR LOOP + return graph.adjList; + } + ****/ + // console.log(adjList); + + + // const isVisited = {}; + /*************** + function searcher(word,list,matrix){ + word.toUpperCase(); + //WORD is a string. + let visited = {}; + let node, neighbours; + let queue = []; + let depth = matrix.length; + let breadth = matrix[0].length; + let row = 0; + while(row < depth){ + let col = 0; + while(col < breadth){ + queue = []; + let node = matrix[row][col]; + //Condition to Check FOR Starting NODE + let neighbourIdxArr = getAdjacents(depth-1,breadth-1,[row,col]); + // console.log(neighbourIdxArr); + if(!visited[node]){ + visited[node] = true; + neighbourIdxArr.forEach(arrIdx => { + //Add them to the queue + neighbours = matrix[arrIdx[0]][arrIdx[1]]; + console.log(arrIdx[0],arrIdx[1]); + queue.push(neighbours); + }); + } // check if node has been visited + //Condition to Check FOR Starting NODE + col++; + } + row++; + } + + return true; + } + ****************/ + // searcher(word,adjList,words); + //LOGIC + //AS YOU FIND THE CURRENT ELEMENT JUMP TO that + // NODE and search for its neighbours + + +//UNDIRECTED GRAPH +/********** +function Graph(){ + this.adjList = {}; + + this.addVertex = (vertex) => { + let emptyArr = []; + if(!this.adjList[vertex]) this.adjList[vertex] = emptyArr; + return this.adjList; + } + + this.addEdge = (v1,v2) => { + if(!this.adjList[v1] || !this.adjList[v2] ) { + return undefined; + } + if(!this.adjList[v1].includes(v2) && v1 !== v2){ + this.adjList[v1].push(v2); + } + if(!this.adjList[v2].includes(v1) && v1 !== v2){ + this.adjList[v2].push(v1); + } + } + + this.dfsRecursive = (start) => { + let result = []; + let visited = {}; + let list= this.adjList; + // let edge; + function dfs(v){ + if(!v) return null; + visited[v] = true; + result.push(v); + for(let edge of list[v]){ + if(!visited[edge]) dfs(edge); + } + } + dfs(start); + return result; + } + + this.dfsIterative = (start) => { + let results = []; + let visited = {}; + let stack = []; + stack.push(start); + + while(stack.length > 0){ + let vertex = stack.pop(); + if(!visited[vertex]){ + visited[vertex] = true; + results.push(vertex); + stack.push(...this.adjList[vertex]); + } + } + return results; + } + + this.bfsIterative = (start) => { + let visited = {}; + let results = []; + let queue = [start]; + let vertex = queue[0]; + visited[vertex] = true; + while(queue.length){ + vertex = queue.shift(); + results.push(vertex); + for(let neighbour of this.adjList[vertex]){ + if(!visited[neighbour]){ + visited[neighbour] = true; + queue.push(neighbour); + } + } + } + return results; + } + //End of Graph Class + } +************/