Skip to content

Commit 0a1963b

Browse files
Merge branch 'master' into hackoct_24
2 parents 3df5f0e + c1d7d66 commit 0a1963b

File tree

10 files changed

+124
-73
lines changed

10 files changed

+124
-73
lines changed

Diff for: .eslintrc.json

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"sourceType": "module"
1111
},
1212
"rules": {
13+
"camelcase": "error",
1314
"indent": [
1415
"error",
1516
2

Diff for: DesignDataStructure/designI.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ The data structure should be efficient enough to accommodate the operations acco
2121
Frequency: Least frequent.
2222
*/
2323

24+
/*
25+
example:
2426
2527
class effStructure {
2628
this.maxHeap = [];
@@ -33,4 +35,5 @@ class effStructure {
3335
3) deleteMin(): O(log N)
3436
4) deleteMax(): O(log N)
3537
5) Insert(log N)
36-
6) Delete: O(log N)
38+
6) Delete: O(log N)
39+
*/

Diff for: Maximal_square.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,22 @@ Output: 0
2525
var maximalSquare = function(matrix) {
2626
var m = matrix.length;
2727
var n = (matrix[0] || []).length;
28-
var dp = Array(m).fill(0).map(_ => Array(n));
28+
var dp = Array(m).fill(0).map(() => Array(n));
2929
var max = 0;
3030

3131
for (var k = 0; k < m; k++) {
32-
dp[k][0] = matrix[k][0] === '1' ? 1 : 0;
32+
dp[k][0] = matrix[k][0] === "1" ? 1 : 0;
3333
max = Math.max(max, dp[k][0]);
3434
}
3535

3636
for (var p = 0; p < n; p++) {
37-
dp[0][p] = matrix[0][p] === '1' ? 1 : 0;
37+
dp[0][p] = matrix[0][p] === "1" ? 1 : 0;
3838
max = Math.max(max, dp[0][p]);
3939
}
4040

4141
for (var i = 1; i < m; i++) {
4242
for (var j = 1; j < n; j++) {
43-
if (matrix[i][j] === '1') {
43+
if (matrix[i][j] === "1") {
4444
dp[i][j] = Math.min(dp[i - 1][j - 1], dp[i - 1][j], dp[i][j - 1]) + 1;
4545
max = Math.max(max, dp[i][j]);
4646
} else {
@@ -52,3 +52,5 @@ var maximalSquare = function(matrix) {
5252
return max * max;
5353

5454
};
55+
56+
module.exports.maximalSquare = maximalSquare;

Diff for: Maximal_square_Test.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
const assert = require('assert');
2-
const {Maximalsquare } = require('../LeetcodeProblems/Maximal_square');
1+
const assert = require("assert");
2+
const {Maximalsquare } = require("../LeetcodeProblems/Maximal_square");
33

44
function test1() {
55
var matrix = [
66
[1, 0, 1, 0, 0],
77
[1, 0, 1, 1, 1],
8-
[1, 1, 1, 1 1],
8+
[1, 1, 1, 1, 1],
99
[1, 0, 0, 1, 0],
10-
]
10+
];
1111

1212
assert.strictEqual(Maximalsquare(matrix), 4);
1313
}
@@ -16,16 +16,17 @@ function test2() {
1616
var matrix = [
1717
[0, 1],
1818
[1,0]
19-
]
19+
];
2020

2121
assert.strictEqual(Maximalsquare(matrix), 1);
2222
}
2323

2424
function test3(){
2525
var matrix = [
2626
[0]
27-
]
28-
assert.strictEqual(Maximalsquare(matrix), 0);
27+
];
28+
29+
assert.strictEqual(Maximalsquare(matrix), 0);
2930
}
3031

3132
function test() {
@@ -34,4 +35,4 @@ function test() {
3435
test3();
3536
}
3637

37-
module.exports.test = test
38+
module.exports.test = test;

Diff for: SortingAlgorithms/QuickSort.js

+30-29
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,40 @@
11
function swap(items, leftIndex, rightIndex){
2-
var temp = items[leftIndex];
3-
items[leftIndex] = items[rightIndex];
4-
items[rightIndex] = temp;
2+
var temp = items[leftIndex];
3+
items[leftIndex] = items[rightIndex];
4+
items[rightIndex] = temp;
55
}
66
function partition(items, left, right) {
7-
var pivot = items[Math.floor((right + left) / 2)], //middle element
8-
i = left, //left pointer
9-
j = right; //right pointer
10-
while (i <= j) {
11-
while (items[i] < pivot) {
12-
i++;
13-
}
14-
while (items[j] > pivot) {
15-
j--;
16-
}
17-
if (i <= j) {
18-
swap(items, i, j); //sawpping two elements
19-
i++;
20-
j--;
21-
}
7+
var pivot = items[Math.floor((right + left) / 2)], //middle element
8+
i = left, //left pointer
9+
j = right; //right pointer
10+
while (i <= j) {
11+
while (items[i] < pivot) {
12+
i++;
2213
}
23-
return i;
14+
while (items[j] > pivot) {
15+
j--;
16+
}
17+
if (i <= j) {
18+
swap(items, i, j); //sawpping two elements
19+
i++;
20+
j--;
21+
}
22+
}
23+
return i;
2424
}
2525

2626
function quickSort(items, left, right) {
27-
var index;
28-
if (items.length > 1) {
29-
index = partition(items, left, right); //index returned from partition
30-
if (left < index - 1) { //more elements on the left side of the pivot
31-
quickSort(items, left, index - 1);
32-
}
33-
if (index < right) { //more elements on the right side of the pivot
34-
quickSort(items, index, right);
35-
}
27+
var index;
28+
if (items.length > 1) {
29+
index = partition(items, left, right); //index returned from partition
30+
if (left < index - 1) { //more elements on the left side of the pivot
31+
quickSort(items, left, index - 1);
32+
}
33+
if (index < right) { //more elements on the right side of the pivot
34+
quickSort(items, index, right);
3635
}
37-
return items;
36+
}
37+
return items;
3838
}
3939

40+
module.exports.quickSort = quickSort;

Diff for: SortingAlgorithms/heapSort.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
// Testing Gist
22
var heapSort = function(arr) {
33
var n = arr.length;
4-
for(var i = Math.floor(n/2) - 1; i >= 0; i--)
4+
for(let i = Math.floor(n/2) - 1; i >= 0; i--) {
55
heapify(arr, n, i);
6+
}
67

7-
for(var i = n - 1; i >= 0; i--) {
8+
for(let i = n - 1; i >= 0; i--) {
89
swap(arr, 0, i);
910
heapify(arr, i, 0);
1011
}
1112

1213
return arr;
13-
}
14+
};
1415

1516
var heapify = function(arr, n, i) {
1617
var left = 2 * i + 1;
@@ -32,17 +33,17 @@ var heapify = function(arr, n, i) {
3233
swap(arr, i, right);
3334
heapify(arr, n, right);
3435
}
35-
}
36+
};
3637

3738
var swap = function(arr, a, b) {
3839
var temp = arr[a];
3940
arr[a] = arr[b];
4041
arr[b] = temp;
41-
}
42+
};
4243

4344
console.log(heapSort([14, 1, 10, 2, 3, 5, 6, 4, 7, 11, 12, 13]));
4445
console.log(heapSort([]));
4546
console.log(heapSort([1]));
4647
console.log(heapSort([2, 1]));
47-
console.log(heapSort([1,7,2,3,4,1,10,2,3,4,5]))
48+
console.log(heapSort([1,7,2,3,4,1,10,2,3,4,5]));
4849

Diff for: Test.js

+65-23
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,43 @@
22
/* eslint-disable no-undef */
33
const fs = require("fs");
44

5+
const PROBLEMS_FOLDERS = [
6+
"./LeetcodeProblems/Algorithms/easy/",
7+
"./LeetcodeProblems/Algorithms/medium/",
8+
"./LeetcodeProblems/Algorithms/hard/"
9+
];
10+
511
const TESTS_FOLDERS = [
612
"./LeetcodeProblemsTests/Algorithms/easy/",
713
"./LeetcodeProblemsTests/Algorithms/medium/",
814
"./LeetcodeProblemsTests/Algorithms/hard/"
9-
]
15+
];
1016

1117
const REGEX_PATTERN_HIDDEN_FILES = /(^|\/)\.[^\/\.]/g;
1218

13-
var test_all = async function () {
19+
const getAllTests = async function (paths) {
20+
let problems = [];
21+
for(const i in paths) {
22+
const folder = paths[i];
23+
const newProblems = await loadProblemsFiles(folder); // await
24+
problems = problems.concat(newProblems);
25+
}
26+
return problems;
27+
};
28+
29+
const runAllTests = async function (problems) {
1430
try {
15-
var problems = [];
16-
for(const i in TESTS_FOLDERS) {
17-
var folder = TESTS_FOLDERS[i];
18-
var new_problems = await loadProblemsFiles(folder); // await
19-
problems = problems.concat(new_problems);
20-
};
2131
console.log(problems);
22-
23-
var solvePromises = problems.map(solve);
32+
var solvePromises = problems.map(solveProblem);
2433

25-
await Promise.all(solvePromises)
34+
await Promise.all(solvePromises);
2635
} catch (error) {
2736
console.log(error);
2837
throw new Error(error);
2938
}
3039
};
3140

32-
var solve = (problem) => {
41+
const solveProblem = (problem) => {
3342
try {
3443
console.log("Solving: " + problem);
3544

@@ -47,28 +56,61 @@ var solve = (problem) => {
4756
console.log(error);
4857
throw new Error(error);
4958
}
50-
}
59+
};
5160

52-
var loadProblemsFiles = (folder) => {
61+
const loadProblemsFiles = (folder) => {
5362
return new Promise(function (resolve, reject) {
5463
fs.readdir(folder, (error, files) => {
5564
if (error) {
5665
reject(error);
5766
} else {
5867
console.log(folder);
59-
new_problems = files.filter((item) => !REGEX_PATTERN_HIDDEN_FILES.test(item));
60-
new_problems = new_problems.map((item) => folder + item);
68+
newProblems = files.filter((item) => !REGEX_PATTERN_HIDDEN_FILES.test(item));
69+
newProblems = newProblems.map((item) => folder + item);
6170

62-
resolve(new_problems);
71+
resolve(newProblems);
6372
}
6473
});
6574
});
6675
};
6776

68-
if (process.argv.length > 2) {
69-
const path = process.argv.pop();
70-
solve(path);
71-
} else {
72-
test_all();
77+
const getMissingTests = async function (tests, problems) {
78+
const hasTestStatus = problems.reduce((status, problemPath) => {
79+
const baseIndex = PROBLEMS_FOLDERS.findIndex((basePath) =>
80+
problemPath.startsWith(basePath)
81+
);
82+
83+
let testPath = problemPath
84+
.replace(PROBLEMS_FOLDERS[baseIndex], TESTS_FOLDERS[baseIndex])
85+
.replace(/\.js$/, "_Test.js");
86+
87+
status.push({
88+
problem: problemPath,
89+
hasTest: tests.includes(testPath)
90+
});
91+
92+
return status;
93+
}, []);
94+
const missingTests = hasTestStatus.filter((stat) => !stat.hasTest);
95+
console.log("Total Problems:", problems.length);
96+
console.log("Missing Tests:", missingTests.length);
97+
98+
if(missingTests.length) {
99+
console.table(missingTests);
100+
}
101+
};
102+
103+
async function runScript() {
104+
if (process.argv.length > 2) {
105+
const path = process.argv.pop();
106+
solveProblem(path);
107+
} else {
108+
const problems = await getAllTests(PROBLEMS_FOLDERS);
109+
const tests = await getAllTests(TESTS_FOLDERS);
110+
111+
await runAllTests(tests);
112+
await getMissingTests(tests, problems);
113+
}
73114
}
74-
115+
116+
runScript();

Diff for: utilsClasses/ListNodeTestHelper.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const assert = require('assert');
1+
const assert = require("assert");
22

33
var assertList = function(list, expectedArr) {
44
const listlength = list ? list.length() : 0;
@@ -7,6 +7,6 @@ var assertList = function(list, expectedArr) {
77
assert.strictEqual(list.val, expectedArr[i]);
88
list = list.next;
99
}
10-
}
10+
};
1111

1212
module.exports.assertList = assertList;

0 commit comments

Comments
 (0)