Skip to content

Init #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 14, 2018
Merged
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
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -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",
}
]
}
10 changes: 10 additions & 0 deletions Main.js
Original file line number Diff line number Diff line change
@@ -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();
36 changes: 36 additions & 0 deletions PermutationsWithDuplicates.js
Original file line number Diff line number Diff line change
@@ -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;
30 changes: 30 additions & 0 deletions PermutationsWithoutDuplicates.js
Original file line number Diff line number Diff line change
@@ -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;
20 changes: 20 additions & 0 deletions Subsets.js
Original file line number Diff line number Diff line change
@@ -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;
95 changes: 95 additions & 0 deletions TicTacToe.js
Original file line number Diff line number Diff line change
@@ -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)
// // }