diff --git a/package.json b/package.json new file mode 100644 index 00000000..419e1f63 --- /dev/null +++ b/package.json @@ -0,0 +1,17 @@ +{ + "name": "machine-coding-feedback", + "version": "1.0.0", + "description": "Get your [practice machine coding](https://workat.tech/machine-coding/practice) solutions reviewed by [Gaurav Chandak](https://www.linkedin.com/in/gcnit/) and get feedback before your machine coding round.", + "main": "index.js", + "type": "commonjs", + "directories": { + "doc": "docs" + }, + "scripts": { + "snake-ladder": "node snake-ladder/src/main.js", + "split-wise": "node split-wise/src/main.js" + }, + "keywords": [], + "author": "", + "license": "ISC" +} diff --git a/snake-ladder/resources/input.txt b/snake-ladder/resources/input.txt new file mode 100644 index 00000000..5be01773 --- /dev/null +++ b/snake-ladder/resources/input.txt @@ -0,0 +1,22 @@ +9 +62 5 +33 6 +49 9 +88 16 +41 20 +56 53 +98 64 +93 73 +95 75 +8 +2 37 +27 46 +10 32 +51 68 +61 79 +65 84 +71 91 +81 100 +2 +Gaurav +Sagar \ No newline at end of file diff --git a/snake-ladder/src/consts.js b/snake-ladder/src/consts.js new file mode 100644 index 00000000..0a22e95e --- /dev/null +++ b/snake-ladder/src/consts.js @@ -0,0 +1,3 @@ +module.exports = { + endPoint: 100, +} \ No newline at end of file diff --git a/snake-ladder/src/main.js b/snake-ladder/src/main.js new file mode 100644 index 00000000..b4951c01 --- /dev/null +++ b/snake-ladder/src/main.js @@ -0,0 +1,17 @@ +const fs = require('fs'); +const path = require('path'); +const { readInput, isGameOver, getWinningPos, announceWinner, nextMove } = require('./utils/index.js'); + +const inputFilePath = path.resolve(__dirname, '../resources/input.txt'); +const input = fs.readFileSync(inputFilePath, { encoding: 'utf-8'}); +const { snakes, ladders, players: { names, positions } } = readInput(input); +const numPlayers = positions.length; + +let turn = 0; +while(!isGameOver(positions)) { + nextMove(turn, positions, names, snakes, ladders); + turn = (turn+1)%numPlayers +} + +const winInd = getWinningPos(positions); +announceWinner(names[winInd]); \ No newline at end of file diff --git a/snake-ladder/src/utils/announce-winner.js b/snake-ladder/src/utils/announce-winner.js new file mode 100644 index 00000000..32df2ebe --- /dev/null +++ b/snake-ladder/src/utils/announce-winner.js @@ -0,0 +1,3 @@ +exports.default = (name) => { + console.log(`${name} wins the game`); +} \ No newline at end of file diff --git a/snake-ladder/src/utils/get-next-pos.js b/snake-ladder/src/utils/get-next-pos.js new file mode 100644 index 00000000..8ea75eac --- /dev/null +++ b/snake-ladder/src/utils/get-next-pos.js @@ -0,0 +1,9 @@ +const { endPoint } = require('../consts.js'); + +exports.default = (currPosition, snakes, ladders, diceNum) => { + let nextPosition = currPosition + diceNum > endPoint ? currPosition : currPosition + diceNum; + while(snakes[nextPosition] || ladders[nextPosition]) { + nextPosition = snakes[nextPosition] || ladders[nextPosition]; + } + return nextPosition; +} \ No newline at end of file diff --git a/snake-ladder/src/utils/get-winning-pos.js b/snake-ladder/src/utils/get-winning-pos.js new file mode 100644 index 00000000..fa9a413d --- /dev/null +++ b/snake-ladder/src/utils/get-winning-pos.js @@ -0,0 +1,3 @@ +const { endPoint } = require('../consts.js'); + +exports.default = (positions) => positions.findIndex(position => position === endPoint); \ No newline at end of file diff --git a/snake-ladder/src/utils/index.js b/snake-ladder/src/utils/index.js new file mode 100644 index 00000000..09f83df9 --- /dev/null +++ b/snake-ladder/src/utils/index.js @@ -0,0 +1,10 @@ +module.exports = { + readInput: require('./read-input.js').default, + isGameOver: require('./is-game-over.js').default, + rollDice: require('./roll-dice.js').default, + getWinningPos: require('./get-winning-pos.js').default, + getNextPosition: require('./get-next-pos.js').default, + announceWinner: require('./announce-winner.js').default, + logMove: require('./log-move.js').default, + nextMove: require('./next-move.js').default +} diff --git a/snake-ladder/src/utils/is-game-over.js b/snake-ladder/src/utils/is-game-over.js new file mode 100644 index 00000000..d99567b1 --- /dev/null +++ b/snake-ladder/src/utils/is-game-over.js @@ -0,0 +1,3 @@ +const { endPoint } = require('../consts.js'); + +exports.default = (positions) => positions.some(position => position === endPoint); \ No newline at end of file diff --git a/snake-ladder/src/utils/log-move.js b/snake-ladder/src/utils/log-move.js new file mode 100644 index 00000000..c427318b --- /dev/null +++ b/snake-ladder/src/utils/log-move.js @@ -0,0 +1,3 @@ +exports.default = (name, diceValue, currPosition, nextPosition) => { + console.log(`${name} rolled a ${diceValue} and moved from ${currPosition} to ${nextPosition}`); +} \ No newline at end of file diff --git a/snake-ladder/src/utils/next-move.js b/snake-ladder/src/utils/next-move.js new file mode 100644 index 00000000..2b32d61c --- /dev/null +++ b/snake-ladder/src/utils/next-move.js @@ -0,0 +1,12 @@ +const getNextPosition = require('./get-next-pos.js').default; +const rollDice = require('./roll-dice.js').default; +const logMove = require('./log-move.js').default; + +exports.default = (turn, positions, names, snakes, ladders) => { + const currPosition = positions[turn]; + const name = names[turn]; + const diceValue = rollDice(); + const nextPosition = getNextPosition(currPosition, snakes, ladders, diceValue); + logMove(name, diceValue, currPosition, nextPosition); + positions[turn] = nextPosition; +} \ No newline at end of file diff --git a/snake-ladder/src/utils/read-input.js b/snake-ladder/src/utils/read-input.js new file mode 100644 index 00000000..e66bb55a --- /dev/null +++ b/snake-ladder/src/utils/read-input.js @@ -0,0 +1,26 @@ +exports.default = (input) => { + const inputToArr = input.split('\n'); + const numSnake = Number(inputToArr[0]); + const numLadder = Number(inputToArr[numSnake+1]); + const snakes = {}; + const ladders = {}; + const players = { names: [], positions: [] }; + + for(let i=1;i<=numSnake;i++) { + const snake = inputToArr[i].split(' '); + snakes[Number(snake[0])] = Number(snake[1]); + } + + for(let i=numSnake+2;i { + const nextNum = Math.ceil((1+Math.random()*10))%7; + return nextNum === 0 ? 1 : nextNum; +} \ No newline at end of file diff --git a/split-wise/resources/input.txt b/split-wise/resources/input.txt new file mode 100644 index 00000000..3a1cb8a5 --- /dev/null +++ b/split-wise/resources/input.txt @@ -0,0 +1,10 @@ +SHOW +SHOW u1 +EXPENSE u1 1000 4 u1 u2 u3 u4 EQUAL +SHOW u4 +SHOW u1 +EXPENSE u1 1250 2 u2 u3 EXACT 370 880 +SHOW +EXPENSE u4 1200 4 u1 u2 u3 u4 PERCENT 40 20 20 20 +SHOW u1 +SHOW \ No newline at end of file diff --git a/split-wise/src/main.js b/split-wise/src/main.js new file mode 100644 index 00000000..6e32d9e4 --- /dev/null +++ b/split-wise/src/main.js @@ -0,0 +1,48 @@ +const fs = require('fs'); +const path = require('path'); +const { readInput, initializeExpense, showExpense, showExpenseByUser, twoPlaces } = require('./utils/index.js'); + +const inputFilePath = path.resolve(__dirname, '../resources/input.txt'); +const input = fs.readFileSync(inputFilePath, { encoding: 'utf-8' }); +const parsedInput = readInput(input); +const users = ['u1', 'u2', 'u3', 'u4']; +const expense = initializeExpense(users); + +for(let i=0;i acc+num) != 100) throw new Error('Count of all shares do not evaluate to 100 percent for input:', parsedInput[i]); + let amount = 0; + for(let j=1;j acc+num) != totalAmount) throw new Error('Count of all shares do not evaluate to total amount for input:', parsedInput[i]); + for(let j=0;j { + return users.reduce((acc, user) => { + acc[user] = users.reduce((acc1, user1) => { + if(user1 != user) { + acc1[user1] = 0; + } + return acc1 + }, {}); + return acc; + }, {}) +} \ No newline at end of file diff --git a/split-wise/src/utils/read-input.js b/split-wise/src/utils/read-input.js new file mode 100644 index 00000000..d6dfc4ef --- /dev/null +++ b/split-wise/src/utils/read-input.js @@ -0,0 +1,28 @@ +const twoPlaces = require('./two-places').default; + +exports.default = (input) => { + const inputArr = input.split('\n'); + for(let i=0;i twoPlaces(Number(num))); + } + case 'SHOW': + parsedInput.command = currInput[0]; + if(currInput[1]) { + parsedInput.user = currInput[1]; + } + } + inputArr[i] = parsedInput + } + return inputArr; +} \ No newline at end of file diff --git a/split-wise/src/utils/show-expense-user.js b/split-wise/src/utils/show-expense-user.js new file mode 100644 index 00000000..6dff5ba8 --- /dev/null +++ b/split-wise/src/utils/show-expense-user.js @@ -0,0 +1,14 @@ +exports.default = (expense, user) => { + let didShow = false; + Object.keys(expense[user]).map((user1) => { + if(expense[user][user1] > 0) { + console.log(`${user1} owes ${user}: ${expense[user][user1]}`); + } else if(expense[user][user1]<0) { + console.log(`${user} owes ${user1}: ${Math.abs(expense[user][user1])}`); + } + didShow = didShow || expense[user][user1]; + }) + if(!didShow) { + console.log('No balances') + } +} \ No newline at end of file diff --git a/split-wise/src/utils/show-expense.js b/split-wise/src/utils/show-expense.js new file mode 100644 index 00000000..5b2618b6 --- /dev/null +++ b/split-wise/src/utils/show-expense.js @@ -0,0 +1,15 @@ +exports.default = (expense, users) => { + let didShow = false; + users.forEach((user) => { + const userExpense = expense[user]; + Object.keys(userExpense).map(user1 => { + if(userExpense[user1] > 0) { + didShow = true; + console.log(`${user1} owes ${user}: ${userExpense[user1]}`); + } + }) + }) + if(!didShow) { + console.log('No balances') + } +} \ No newline at end of file diff --git a/split-wise/src/utils/two-places.js b/split-wise/src/utils/two-places.js new file mode 100644 index 00000000..d4c49c93 --- /dev/null +++ b/split-wise/src/utils/two-places.js @@ -0,0 +1,3 @@ +exports.default = (num) => { + return parseFloat(num.toFixed(2)); +} \ No newline at end of file