Last active
November 24, 2021 20:24
-
-
Save mwibutsa/82c3dadba7678bb5253c30813c778bb9 to your computer and use it in GitHub Desktop.
#Cohort3 One work coders programming questions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function solution(board, word) { | |
let count = 0; | |
for (let i = 0; i < board.length; i++) { | |
for (let j = 0; j < board[i].length; j++) { | |
const matches = { | |
h: [], | |
v: [], | |
d: [] | |
}; | |
for (let letter = 0; letter < word.length; letter++) { | |
if (board[i] && board[i][j + letter]) { | |
matches.h.push(board[i][j + letter]); | |
} | |
if (board[i + letter] && board[i + letter][j]) { | |
matches.v.push(board[i + letter][j]); | |
} | |
if (board[i + letter] && board[i + letter][j + letter]) { | |
matches.d.push(board[i + letter][j + letter]); | |
} | |
} | |
Object.keys(matches).forEach(key => { | |
const matchedWord = matches[key].join('') | |
if (matchedWord === word) { | |
count++; | |
} | |
}) | |
} | |
} | |
return count; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const compareFn = (a, b) => { | |
let [aDepName, aSplit] = a.split('-'); | |
let [bDepName, bSplit] = b.split('-'); | |
aSplit = aSplit.split('.') | |
bSplit = bSplit.split('.') | |
const _A = aDepName | |
const _B = bDepName | |
const versionA = aSplit[0] | |
const versionB= bSplit[0] | |
const majorA = aSplit[1] | |
const majorB = bSplit[1] | |
const minorA = aSplit[2] | |
const minorB = bSplit[2] | |
if(_A > _B) { | |
return 1; | |
} else if(_A < _B) { | |
return -1; | |
} else { | |
// check main version | |
if(+versionA > +versionB) { | |
return 1; | |
} else if (+versionA < +versionB) { | |
return -1; | |
} else { | |
if(+majorA > +majorB) { | |
return 1; | |
} else if(+majorA < +majorB) { | |
return -1; | |
} else { | |
if(+minorA > +minorB) { | |
return 1; | |
} else if(+minorA < +minorB) { | |
return -1; | |
} else { | |
return 0; | |
} | |
} | |
} | |
} | |
} | |
function solution(operations) { | |
let installedDeps = []; | |
let addedDeps = []; | |
operations.forEach((operation) => { | |
const [command, dependency, version] = operation; | |
// add library | |
if(command === 'add') { | |
const depIndex = addedDeps.findIndex((deps) => deps.includes(dependency)); | |
if(depIndex !== -1) { | |
addedDeps[depIndex] = `${dependency}-${version}`; | |
} else { | |
addedDeps.push(`${dependency}-${version}`); | |
} | |
// install library | |
} else if (command === 'install') { | |
installedDeps = [...installedDeps, ...addedDeps]; | |
addedDeps = []; | |
} | |
}); | |
return installedDeps.sort(compareFn) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const isHorizontalWinner = (currentPlayer, board) => { | |
return board.some((moves) => moves.every((move) => move === currentPlayer)) | |
} | |
const transposeBoard = (board) => { | |
return board.map((_,index) => board.map((row) => row[index])) | |
} | |
const isVerticalWinner = (currentPlayer, board) => { | |
return transposeBoard(board).some((moves) => moves.every((move) => move === currentPlayer)) | |
} | |
const getDiagonalMoves = (board) => { | |
const diagonalMoves = []; | |
const equalBasedDiagonal = []; | |
const sumBasedDiagonal = []; | |
for(let row = 0; row < board.length; row++){ | |
for (col = 0; col < board.length; col++) { | |
if (row === col) { | |
equalBasedDiagonal.push(board[row][col]) | |
} | |
} | |
} | |
for(let row = 0; row < board.length; row++){ | |
for (col = 0; col < board.length; col++) { | |
if (row + col === board.length -1 ) { | |
sumBasedDiagonal.push(board[row][col]) | |
} | |
} | |
} | |
diagonalMoves.push(equalBasedDiagonal,sumBasedDiagonal); | |
return diagonalMoves; | |
} | |
const isDiagonalWinner = (currentPlayer,board) => { | |
return getDiagonalMoves(board).some((moves) => moves.every((move) => move === currentPlayer)) | |
} | |
const isWinner = (player,board) => isHorizontalWinner(player,board) || isVerticalWinner(player,board) || isDiagonalWinner(player,board) | |
const isGameOver = (board) => board.every((row) => row.every((move) => move !== '.')) | |
function solution(board) { | |
board = board.map(el => el.split('')) | |
if(isGameOver(board) && !isWinner('X', board) && !isWinner('O', board)) { | |
return 'TIE'; | |
} | |
if (isWinner('X',board)) { | |
return 'X WIN'; | |
} else if (isWinner('O', board)){ | |
return 'O WIN'; | |
} else { | |
return 'ONGOING'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment