Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 03a79f2

Browse files
Merge pull request #1 from ignacio-chiazzo/test
Init
2 parents 404c21f + c497e7b commit 03a79f2

6 files changed

+206
-0
lines changed

.vscode/launch.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "node",
9+
"request": "launch",
10+
"name": "Launch Program",
11+
"program": "${workspaceFolder}/Main.js",
12+
"console": "integratedTerminal",
13+
}
14+
]
15+
}

Main.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Import
2+
var permutationWithoutDuplicates = require("./PermutationsWithoutDuplicates.js");
3+
var permutationWithDuplicates = require("./PermutationsWithDuplicates.js");
4+
var subsets = require("./Subsets.js");
5+
6+
// Invocation
7+
8+
// permutationWithoutDuplicates.main();
9+
// permutationWithDuplicates.main();
10+
// subsets.main();

PermutationsWithDuplicates.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// https://leetcode.com/problems/permutations-ii/description/
2+
3+
// Permutations without
4+
var subsetWithoutDuplicates = function(nums) {
5+
if(nums.lenght == 0){
6+
return;
7+
}
8+
var solution = [];
9+
subsetWithoutDuplicatesAux(nums, [], solution);
10+
console.log(solution);
11+
}
12+
13+
var subsetWithoutDuplicatesAux = function(nums, current, sol) {
14+
if(nums.length == 0){
15+
sol.push(current);
16+
}
17+
18+
var setNums = new Set();
19+
20+
nums.forEach((value, index) => {
21+
if(setNums.has(value)) {
22+
return;
23+
}
24+
setNums.add(value);
25+
var newCurrent = [...current, value]
26+
27+
var newNum = nums.filter(function(num, idx) { return index !== idx});
28+
subsetWithoutDuplicatesAux(newNum, newCurrent, sol);
29+
})
30+
}
31+
32+
function main() {
33+
subsetWithoutDuplicates([1,1,2,3]);
34+
}
35+
36+
module.exports.main = main;

PermutationsWithoutDuplicates.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//https://leetcode.com/problems/permutations/description/
2+
3+
// Permutations wihto
4+
var subsetWithDuplicates = function(nums) {
5+
if(nums.lenght == 0){
6+
return;
7+
}
8+
var solution = [];
9+
subsetWithDuplicatesAux(nums, [], solution);
10+
console.log(solution);
11+
}
12+
13+
var subsetWithDuplicatesAux = function(nums, current, sol) {
14+
if(nums.length == 0){
15+
sol.push(current);
16+
}
17+
18+
for(var i = 0; i < nums.length; i++) {
19+
var newCurrent = [...current, nums[i]]
20+
21+
var newNum = nums.filter(function(num, index) { return index !== i});
22+
subsetWithDuplicatesAux(newNum, newCurrent, sol);
23+
}
24+
}
25+
26+
function main() {
27+
subsetWithDuplicates([1,2,3])
28+
}
29+
30+
module.exports.main = main;

Subsets.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var subsets = function(nums) {
2+
var ret = [];
3+
4+
subsetByPosition = function (nums, position, current) {
5+
console.log(current);
6+
if(position == nums.length) {
7+
return [current];
8+
}
9+
var currentRight = current.slice().concat([nums[position]]);
10+
return subsetByPosition(nums, position + 1, currentRight).concat(subsetByPosition(nums, position + 1, current));
11+
}
12+
13+
return subsetByPosition(nums, 0, []);
14+
};
15+
16+
function main() {
17+
console.log(subsets([1,2,3]));
18+
}
19+
20+
module.exports.main = main;

TicTacToe.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
2+
class TicTacToe {
3+
constructor() {
4+
this.matrix = [];
5+
for(var i = 0; i < 3; i++) {
6+
this.matrix[i] = [];
7+
for(var j = 0; j < 3; j++) {
8+
this.matrix[i][j] = "-";
9+
}
10+
}
11+
};
12+
13+
validToken(token) {
14+
if (token == "X" || token == "0" || token == "-") {
15+
return true;
16+
}
17+
console.log("Token is invalid");
18+
return false;
19+
}
20+
21+
addToken(x, y, token) {
22+
// Check valid positions
23+
if(this.validToken(token)) {
24+
this.matrix[x][y] = token;
25+
}
26+
};
27+
28+
printBoard() {
29+
for(var row = 0; row < 3; row ++) {
30+
console.log(this.matrix[row][0] + "|"
31+
+ this.matrix[row][1] + "|"
32+
+ this.matrix[row][2]); // Check new line;
33+
}
34+
}
35+
36+
isBoardFull() {
37+
for(var row = 0; row < 3; row ++) {
38+
for(var col = 0; col < 3; col ++) {
39+
if(this.matrix[row][col] === "-") {
40+
console.log("Is not full");
41+
return false;
42+
}
43+
}
44+
}
45+
console.log("Is full");
46+
return true;
47+
}
48+
49+
makeMove() {
50+
if(this.isBoardFull()) {
51+
throw "Error Board is Full";
52+
}
53+
for(var row = 0; row < 3; row ++) {
54+
for(var col = 0; col < 3; col ++) {
55+
if(this.matrix[row][col] === "-") {
56+
this.addToken(row, col, "0");
57+
}
58+
}
59+
}
60+
}
61+
}
62+
63+
ticTacToe = new TicTacToe();
64+
ticTacToe.isBoardFull();
65+
ticTacToe.addToken(0,1,"X");
66+
ticTacToe.printBoard();
67+
var iter = 0;
68+
while(iter < 8) {
69+
ticTacToe.makeMove();
70+
iter++;
71+
}
72+
73+
console.log("after 8 moves");
74+
ticTacToe.isBoardFull();
75+
ticTacToe.printBoard();
76+
ticTacToe.makeMove();
77+
78+
// ticTacToe.printBoard();
79+
// ticTacToe.addToken(0,0,"X");
80+
// ticTacToe.printBoard();
81+
82+
83+
84+
// // var readline = require('readline')
85+
86+
// // const rl = readline.createInterface({
87+
// // input: process.stdin,
88+
// // output: process.stdout
89+
// // });
90+
91+
// // var response = rl.question('Whats your name : ', answer)
92+
93+
// // function answer(response) {
94+
// // console.log(response)
95+
// // }

0 commit comments

Comments
 (0)