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
47 changes: 46 additions & 1 deletion src/count-islands/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,48 @@
function countIslands(grid) {}
function countIslands(grid) {
//This function directs every activities that goes on here. it calls the checkDreyLand function,
//which searches for inland, gives it the axis of the point to start seach from and collects
//feedback from it.
function manager(matrix){
let inlandRecorder=0;
for(let row=0; row<matrix.length; row++){
for(let column=0; column<matrix[row].length; column++){
if(matrix[row][column]==1){
inlandRecorder++;
checkDryLand(matrix,row,column);
}
}
}
return inlandRecorder;
}
//This function searches for island by checking adjacent element(land) and equating it to zero
//if 1 or ignors if zero.
function checkDryLand(matrix,axisX,axisY){
//checking adjacent up
if(axisX+1<=matrix.length && matrix[axisX+1]!==undefined && matrix[axisX+1][axisY]==1){
matrix[axisX+1][axisY]=0;
checkDryLand(matrix,axisX+1,axisY);
}
//checking adjacent down
if(axisX-1>=0 && matrix[axisX-1]!==undefined && matrix[axisX-1][axisY]==1){
matrix[axisX-1][axisY]=0;
checkDryLand(matrix,axisX-1,axisY);
}
//checking adjacent left
if(axisY-1>=0 && matrix[axisX][axisY-1]!=undefined && matrix[axisX][axisY-1]==1){
matrix[axisX][axisY-1]=0;
checkDryLand(matrix,axisX,axisY-1);
}
//checking adjacent right
if(axisY+1<=matrix[0].length && matrix[axisX][axisY+1]!=undefined && matrix[axisX][axisY+1]==1){
matrix[axisX][axisY+1]=0;
checkDryLand(matrix,axisX,axisY+1);
}
return "Thank you";
}
//the manager is done and about to return result to the company owner(i.e countIsland). Always remember
//that the compant owner(countIsland) reports it the creator. Which is YOU...
return manager(grid);
}


module.exports = countIslands;
80 changes: 79 additions & 1 deletion src/word-search/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,81 @@
function wordSearch(words, word) {}
function wordSearch(words, word){

//this function searches for all the index positions(rows and columns) of the first character
//of the "word" and store them as two sets of arrays(rowIndex and colIndex) inside an object
//and return the object. These positions are the starting points to searching from.
function searchIndex(words,word){
let rowIndex=[];
let colIndex=[];

for(let row=0; row<words.length; row++){
for(let col=0; col<words[row].length; col++){
if(words[row][col]==word[0]){
rowIndex.push(row);
colIndex.push(col)
}
}
}
return {"rowPosition":rowIndex, "colPosition":colIndex};
}
//This is the manager, the controller general. As the name implies, it controls every activities
//that is to take place. It calls the searchIndex function, collects feedback, and calls the
//CheckNextChar function. it tells the checkNextChar funtion where to start its search from,
//collects feedback from checkNextChar which it uses to determin the next action. The next
//action could be to make another search using another starting point(that is if the "word" was
//not found) or to return "true"(if the word was found) or to return "false"(if the starting
//points have been used up).
//NOTE THAT THE "manager" REPORTS TO THE COMPANY OWNER WHICH IS THE "wordSearch". ABOVE ALL,
//THEY ALL REPORT TO THEIR GOD WHICH "YOOOU"...

function manager(search){
let indexr=search.rowPosition;
let indexc = search.colPosition;
for(let a=0; a<indexr.length; a++){
board=JSON.parse(JSON.stringify(words));
if(checkNextChar(board,word,indexr[a],indexc[a],1)){
return true;
}
}
return false;
}
//this function does the "word" search. It first collects the first starting point(row and col)
//and makes a search. if the "word" exist, it returns true else, it collects another starting
//point. after using all the starting point and could not find the "word", it returns false.
function checkNextChar(words,word,row,col,indexPositionsOfWord ){
words[row][col]=0
if(indexPositionsOfWord==word.length){
return true;
}

if(row+1<=words.length && words[row+1]!==undefined && words[row+1][col]==word[indexPositionsOfWord]){
words[row+1][col]=0
if(checkNextChar(words,word,(row+1),col,indexPositionsOfWord+1)){
return true
}

}
if(row-1>=0 && words[row-1]!==undefined && words[row-1][col]==word[indexPositionsOfWord]){
words[row-1][col]=0
if(checkNextChar(words,word,(row-1),col,indexPositionsOfWord+1)){
return true;
}

}
if(col-1>=0 && words[row][col-1]!=undefined && words[row][col-1]==word[indexPositionsOfWord]){
words[row][col-1]=0
if(checkNextChar(words,word,(row),col-1,indexPositionsOfWord+1)){
return true;
}

}
if(col+1<=words[0].length && words[row][col+1]!=undefined && words[row][col+1]==word[indexPositionsOfWord]){
words[row][col+1]=0
if(checkNextChar(words,word,(row),col+1,indexPositionsOfWord+1)){
return true;
}
}
return 0;
}
return manager(searchIndex(words,word));
}
module.exports = wordSearch;