diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..7f94b97 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,15 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "node", + "request": "launch", + "name": "Launch Program", + "program": "${workspaceFolder}/Main.js", + "console": "integratedTerminal", + } + ] +} \ No newline at end of file diff --git a/Main.js b/Main.js new file mode 100644 index 0000000..b9011fd --- /dev/null +++ b/Main.js @@ -0,0 +1,10 @@ +// Import +var permutationWithoutDuplicates = require("./PermutationsWithoutDuplicates.js"); +var permutationWithDuplicates = require("./PermutationsWithDuplicates.js"); +var subsets = require("./Subsets.js"); + +// Invocation + +// permutationWithoutDuplicates.main(); +// permutationWithDuplicates.main(); +// subsets.main(); \ No newline at end of file diff --git a/PermutationsWithDuplicates.js b/PermutationsWithDuplicates.js new file mode 100644 index 0000000..9854ec8 --- /dev/null +++ b/PermutationsWithDuplicates.js @@ -0,0 +1,36 @@ +// https://leetcode.com/problems/permutations-ii/description/ + +// Permutations without +var subsetWithoutDuplicates = function(nums) { + if(nums.lenght == 0){ + return; + } + var solution = []; + subsetWithoutDuplicatesAux(nums, [], solution); + console.log(solution); +} + +var subsetWithoutDuplicatesAux = function(nums, current, sol) { + if(nums.length == 0){ + sol.push(current); + } + + var setNums = new Set(); + + nums.forEach((value, index) => { + if(setNums.has(value)) { + return; + } + setNums.add(value); + var newCurrent = [...current, value] + + var newNum = nums.filter(function(num, idx) { return index !== idx}); + subsetWithoutDuplicatesAux(newNum, newCurrent, sol); + }) +} + +function main() { + subsetWithoutDuplicates([1,1,2,3]); +} + +module.exports.main = main; \ No newline at end of file diff --git a/PermutationsWithoutDuplicates.js b/PermutationsWithoutDuplicates.js new file mode 100644 index 0000000..4a9c26d --- /dev/null +++ b/PermutationsWithoutDuplicates.js @@ -0,0 +1,30 @@ +//https://leetcode.com/problems/permutations/description/ + +// Permutations wihto +var subsetWithDuplicates = function(nums) { + if(nums.lenght == 0){ + return; + } + var solution = []; + subsetWithDuplicatesAux(nums, [], solution); + console.log(solution); +} + +var subsetWithDuplicatesAux = function(nums, current, sol) { + if(nums.length == 0){ + sol.push(current); + } + + for(var i = 0; i < nums.length; i++) { + var newCurrent = [...current, nums[i]] + + var newNum = nums.filter(function(num, index) { return index !== i}); + subsetWithDuplicatesAux(newNum, newCurrent, sol); + } +} + +function main() { + subsetWithDuplicates([1,2,3]) +} + +module.exports.main = main; \ No newline at end of file diff --git a/Subsets.js b/Subsets.js new file mode 100644 index 0000000..06f2366 --- /dev/null +++ b/Subsets.js @@ -0,0 +1,20 @@ +var subsets = function(nums) { + var ret = []; + + subsetByPosition = function (nums, position, current) { + console.log(current); + if(position == nums.length) { + return [current]; + } + var currentRight = current.slice().concat([nums[position]]); + return subsetByPosition(nums, position + 1, currentRight).concat(subsetByPosition(nums, position + 1, current)); + } + + return subsetByPosition(nums, 0, []); +}; + +function main() { + console.log(subsets([1,2,3])); +} + +module.exports.main = main; diff --git a/TicTacToe.js b/TicTacToe.js new file mode 100644 index 0000000..8b99a71 --- /dev/null +++ b/TicTacToe.js @@ -0,0 +1,95 @@ + +class TicTacToe { + constructor() { + this.matrix = []; + for(var i = 0; i < 3; i++) { + this.matrix[i] = []; + for(var j = 0; j < 3; j++) { + this.matrix[i][j] = "-"; + } + } + }; + + validToken(token) { + if (token == "X" || token == "0" || token == "-") { + return true; + } + console.log("Token is invalid"); + return false; + } + + addToken(x, y, token) { + // Check valid positions + if(this.validToken(token)) { + this.matrix[x][y] = token; + } + }; + + printBoard() { + for(var row = 0; row < 3; row ++) { + console.log(this.matrix[row][0] + "|" + + this.matrix[row][1] + "|" + + this.matrix[row][2]); // Check new line; + } + } + + isBoardFull() { + for(var row = 0; row < 3; row ++) { + for(var col = 0; col < 3; col ++) { + if(this.matrix[row][col] === "-") { + console.log("Is not full"); + return false; + } + } + } + console.log("Is full"); + return true; + } + + makeMove() { + if(this.isBoardFull()) { + throw "Error Board is Full"; + } + for(var row = 0; row < 3; row ++) { + for(var col = 0; col < 3; col ++) { + if(this.matrix[row][col] === "-") { + this.addToken(row, col, "0"); + } + } + } + } +} + +ticTacToe = new TicTacToe(); +ticTacToe.isBoardFull(); +ticTacToe.addToken(0,1,"X"); +ticTacToe.printBoard(); +var iter = 0; +while(iter < 8) { + ticTacToe.makeMove(); + iter++; +} + +console.log("after 8 moves"); +ticTacToe.isBoardFull(); +ticTacToe.printBoard(); +ticTacToe.makeMove(); + +// ticTacToe.printBoard(); +// ticTacToe.addToken(0,0,"X"); +// ticTacToe.printBoard(); + + + +// // var readline = require('readline') + +// // const rl = readline.createInterface({ +// // input: process.stdin, +// // output: process.stdout +// // }); + +// // var response = rl.question('Whats your name : ', answer) + +// // function answer(response) { +// // console.log(response) +// // } \ No newline at end of file