Skip to content

Commit 142a6a4

Browse files
Move tests to its own files Round 7
1 parent d3a1949 commit 142a6a4

33 files changed

+81
-262
lines changed

LeetcodeProblems/Best_Time_To_Buy_And_Sell_Stock_II.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,4 @@ var maxProfit = function(prices) {
5555
return profit;
5656
};
5757

58-
var main = function() {
59-
test();
60-
}
61-
62-
function test() {
63-
assert.equal(maxProfit([7,1,5,3,6,4]), 7);
64-
assert.equal(maxProfit([7,1,5,3320,6,4]), 3319);
65-
}
66-
6758
module.exports.maxProfit = maxProfit;

LeetcodeProblems/Escape_The_Ghosts.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,4 @@ var getDistance = function(a, b) {
5959
return horizontalMoves + verticalMoves;
6060
}
6161

62-
var main = function() {
63-
test();
64-
}
65-
66-
function test() {
67-
assert.equal(escapeGhosts([[1, 0], [0, 3]], [0, 1]), true);
68-
assert.equal(escapeGhosts([[1, 0]], [2, 0]), false);
69-
assert.equal(escapeGhosts([[2, 0]], [1, 0]), true);
70-
}
71-
72-
module.exports.main = main
62+
module.exports.escapeGhosts = escapeGhosts;

LeetcodeProblems/SearchIng_Rotated_Sorted_Array.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,4 @@ var searchAux = function(nums, target, start, end) {
5454
}
5555
}
5656

57-
var main = function(n) {
58-
test();
59-
}
60-
61-
var test = function() {
62-
assert.equal(search([4,5,6,7,0,1,2], 5), 1);
63-
}
64-
main()
65-
module.exports.main = main;
57+
module.exports.search = search;

LeetcodeProblems/Search_a_2D_Matrix.js

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ target = 13
2828
Output: false
2929
*/
3030

31-
3231
/**
3332
* @param {number[][]} matrix
3433
* @param {number} target
@@ -58,17 +57,5 @@ var searchMatrixAux = function(matrix, firstRow, lastRow, target) {
5857

5958
return false;
6059
};
61-
62-
var main = function(){
63-
test();
64-
}
65-
66-
var test = function() {
67-
assert.equal(searchMatrix([], 0), false);
68-
assert.equal(searchMatrix([[1], [3]], 3), true);
69-
assert.equal(searchMatrix([[1], [3]], 1), true);
70-
const matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,50]];
71-
assert.equal(searchMatrix(matrix, 3), true);
72-
}
7360

74-
module.exports.main = main;
61+
module.exports.searchMatrix = searchMatrix;

LeetcodeProblems/Search_a_2D_Matrix_II.js

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ Given target = 5, return true.
1818
Given target = 20, return false.
1919
*/
2020

21-
2221
/**
2322
* @param {number[][]} matrix
2423
* @param {number} target
@@ -43,22 +42,4 @@ var searchMatrix = function(matrix, target) {
4342
return false;
4443
};
4544

46-
const matrix1 = [
47-
[1,4,7, 11,15],
48-
[2,5,8, 12,19],
49-
[3,6,9, 16,22],
50-
[10,13,14, 17,24],
51-
[18,21,23, 26,30]
52-
];
53-
54-
var main = function(n) {
55-
test();
56-
}
57-
58-
var test = function() {
59-
assert.equal(searchMatrix(matrix1, 5), true);
60-
assert.equal(searchMatrix(matrix1, 0), false);
61-
assert.equal(searchMatrix(matrix1, 15), true);
62-
}
63-
64-
module.exports.main = main;
45+
module.exports.searchMatrix = searchMatrix;

LeetcodeProblems/Set_Matrix_Zeroes.js

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ A simple improvement uses O(m + n) space, but still not the best solution.
3939
Could you devise a constant space solution?
4040
*/
4141

42-
43-
4442
/**
4543
* @param {number[][]} matrix
4644
* @return {void} Do not return anything, modify matrix in-place instead.
@@ -110,15 +108,4 @@ var fillCol = function(matrix, col) {
110108
matrix[i][col] = 0;
111109
}
112110

113-
var main = function() {
114-
test();
115-
}
116-
117-
var test = function() {
118-
assert.deepEqual(
119-
setZeroes([[1,1,1],[1,0,1],[1,1,1]]),
120-
[[1, 0, 1], [0, 0, 0], [1, 0, 1]]
121-
);
122-
}
123-
124-
module.exports.main = main;
111+
module.exports.setZeroes = setZeroes;

LeetcodeProblems/Simplify_Path.js

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,4 @@ var simplifyPath = function(path) {
6060
return (ret.length == 0) ? "/" : ret;
6161
};
6262

63-
var main = function(){
64-
test();
65-
}
66-
67-
var test = function() {
68-
assert.equal(simplifyPath("/../c"), "/c");
69-
assert.equal(simplifyPath("/.."), "/");
70-
assert.equal(simplifyPath("/home/"), "/home"); // => "/home"
71-
assert.equal(simplifyPath("/a/./b/../../c/"), "/c"); // => "/c"
72-
assert.equal(simplifyPath("/a/../../b/../c//.//"), "/c"); // => "/c"
73-
assert.equal(simplifyPath("/a//b////c/d//././/.."), "/a/b/c") // => "/a/b/c"
74-
}
75-
76-
module.exports.main = main
63+
module.exports.simplifyPath = simplifyPath;

LeetcodeProblems/Spiral_Matrix.js

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,4 @@ var printRect = function(matrix, i, rowLength, colLength, retArray) {
6767
}
6868
}
6969

70-
var main = function() {
71-
const matrix = [
72-
[ 1, 2, 3 ],
73-
[ 4, 5, 6 ],
74-
[ 7, 8, 9 ]
75-
]
76-
77-
assert.deepEqual(
78-
spiralOrder(matrix),
79-
[1, 2, 3, 6, 9, 8, 7, 4, 5]
80-
)
81-
}
82-
83-
module.exports.main = main;
70+
module.exports.spiralOrder = spiralOrder;

LeetcodeProblems/Subarray_Sum_Equals_K.js

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,4 @@ var subarraySum2 = function(nums, k) {
6060
return ret;
6161
};
6262

63-
var main = function() {
64-
test();
65-
}
66-
67-
var test = function() {
68-
assert.strictEqual(subarraySum([1,1,1], 2), 2);
69-
assert.strictEqual(subarraySum([1], 0), 0);
70-
assert.strictEqual(subarraySum([0], 0), 1);
71-
assert.strictEqual(subarraySum([0,0,0,0,0], 0), 15);
72-
}
73-
74-
module.exports.main = main;
63+
module.exports.subarraySum = subarraySum;

LeetcodeProblems/Subsets.js

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,4 @@ var subsets = function(nums) {
3838
return subsetByPosition(nums, 0, []);
3939
};
4040

41-
function main() {
42-
test();
43-
}
44-
45-
function test() {
46-
assert.deepEqual(subsets([]), [[]]);
47-
assert.deepEqual(subsets([1]), [[1], []]);
48-
assert.deepEqual(
49-
subsets([1,2]),
50-
[[1, 2], [1], [2], []]
51-
);
52-
assert.deepEqual(
53-
subsets([1, 2, 3]),
54-
[[1, 2, 3], [1, 2], [1, 3], [1], [2, 3], [2], [3], []]
55-
);
56-
}
57-
58-
module.exports.main = main;
41+
module.exports.subsets = subsets;

LeetcodeProblems/Sum_Of_Square_Numbers.js

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ Input: 3
1414
Output: False
1515
*/
1616

17-
18-
1917
/**
2018
* @param {number} c
2119
* @return {boolean}
@@ -35,18 +33,4 @@ var judgeSquareSum = function(c) {
3533
return false;
3634
};
3735

38-
var main = function() {
39-
test();
40-
}
41-
42-
var test = function() {
43-
assert.strictEqual(judgeSquareSum(0), true);
44-
assert.strictEqual(judgeSquareSum(1), true);
45-
assert.strictEqual(judgeSquareSum(5), true);
46-
assert.strictEqual(judgeSquareSum(16), true);
47-
assert.strictEqual(judgeSquareSum(24), false);
48-
assert.strictEqual(judgeSquareSum(25), true);
49-
}
50-
51-
module.exports.main = main;
52-
36+
module.exports.judgeSquareSum = judgeSquareSum;

LeetcodeProblems/Swap_Nodes_In_Pairs.js

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ Your algorithm should use only constant extra space.
1313
You may not modify the values in the list's nodes, only nodes itself may be changed.
1414
*/
1515

16-
const ListNode = require('../UtilsClasses/ListNode').ListNode;
17-
const ListNodeTestHelper = require('../utilsClasses/ListNodeTestHelper');
18-
1916
/**
2017
* Definition for singly-linked list.
2118
* function ListNode(val) {
@@ -49,15 +46,4 @@ var swapPairs = function(head) {
4946
return head;
5047
};
5148

52-
var main = function() {
53-
test();
54-
}
55-
56-
var test = function () {
57-
ListNodeTestHelper.assertList(swapPairs(ListNode.linkenList([1,2,3,4])), [2,1,4,3]);
58-
ListNodeTestHelper.assertList(swapPairs(ListNode.linkenList([])), []);
59-
ListNodeTestHelper.assertList(swapPairs(ListNode.linkenList([1])), [1]);
60-
ListNodeTestHelper.assertList(swapPairs(ListNode.linkenList([1,2])), [2, 1]);
61-
}
62-
63-
module.exports.main = main;
49+
module.exports.swapPairs = swapPairs;

LeetcodeProblems/Tic_Tac_Toe.js

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ class TicTacToe {
2626
};
2727

2828
printBoard() {
29+
console.log("-------");
2930
for(var row = 0; row < 3; row ++) {
3031
console.log(this.matrix[row][0] + "|"
3132
+ this.matrix[row][1] + "|"
3233
+ this.matrix[row][2]); // Check new line;
3334
}
35+
console.log("------- \n");
3436
}
3537

3638
isBoardFull() {
@@ -46,41 +48,19 @@ class TicTacToe {
4648
return true;
4749
}
4850

49-
makeMove() {
51+
makeMove(str) {
5052
if(this.isBoardFull()) {
5153
throw "Error Board is Full";
5254
}
5355
for(var row = 0; row < 3; row ++) {
5456
for(var col = 0; col < 3; col ++) {
5557
if(this.matrix[row][col] === "-") {
56-
this.addToken(row, col, "0");
58+
this.addToken(row, col, str);
59+
return true;
5760
}
58-
}
61+
}
5962
}
6063
}
6164
}
6265

63-
var main = function() {
64-
console.log("TBD");
65-
}
66-
67-
module.exports.main = main;
68-
69-
ticTacToe = new TicTacToe();
70-
ticTacToe.isBoardFull();
71-
ticTacToe.addToken(0,1,"X");
72-
ticTacToe.printBoard();
73-
var iter = 0;
74-
while(iter < 8) {
75-
ticTacToe.makeMove();
76-
iter++;
77-
}
78-
79-
console.log("after 8 moves");
80-
ticTacToe.isBoardFull();
81-
ticTacToe.printBoard();
82-
ticTacToe.makeMove();
83-
84-
ticTacToe.printBoard();
85-
ticTacToe.addToken(0,0,"X");
86-
ticTacToe.printBoard();
66+
module.exports.TicTacToe = TicTacToe;

LeetcodeProblems/Unique_Binary_Search_Trees.js

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ Given n = 3, there are a total of 5 unique BST's:
2222
DP Solution: https://www.programcreek.com/2014/05/leetcode-unique-binary-search-trees-java/
2323
*/
2424

25-
26-
2725
// Solution 3 using DP
2826
var numTrees3 = function (n) {
2927
if (n == 0)
@@ -109,25 +107,6 @@ var numTreesAux1 = function(leftMin, leftMax) {
109107
return count;
110108
}
111109

112-
var main = function() {
113-
test();
114-
}
115-
116-
var test = function () {
117-
assert.strictEqual(numTrees1(1), 1);
118-
assert.strictEqual(numTrees1(2), 2);
119-
assert.strictEqual(numTrees1(3), 5);
120-
assert.strictEqual(numTrees1(5), 42);
121-
122-
assert.strictEqual(numTrees2(1), 1);
123-
assert.strictEqual(numTrees2(2), 2);
124-
assert.strictEqual(numTrees2(3), 5);
125-
assert.strictEqual(numTrees2(5), 42);
126-
127-
assert.strictEqual(numTrees3(1), 1);
128-
assert.strictEqual(numTrees3(2), 2);
129-
assert.strictEqual(numTrees3(3), 5);
130-
assert.strictEqual(numTrees3(5), 42);
131-
}
132-
133-
module.exports.main = main
110+
module.exports.numTrees1 = numTrees1;
111+
module.exports.numTrees2 = numTrees2;
112+
module.exports.numTrees3 = numTrees3;

LeetcodeProblems/Unique_Paths.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ Output: 28
2626

2727
// Solution 1
2828
// This solution is a naive solution implementing a binary tree and visiting each node.
29-
30-
3129
var uniquePaths1 = function(m, n) {
3230
return uniquePathsAux(0, 0, m, n)
3331
};
@@ -96,13 +94,6 @@ var uniquePaths3 = function(m, n) {
9694
return matrix[m - 1][n - 1];
9795
};
9896

99-
var main = function() {
100-
test();
101-
}
102-
103-
var test = function() {
104-
assert.strictEqual(uniquePaths1(10,4), 220);
105-
assert.strictEqual(uniquePaths1(3,2), 3);
106-
}
107-
108-
module.exports.main = main;
97+
module.exports.uniquePaths1 = uniquePaths1;
98+
module.exports.uniquePaths2 = uniquePaths1;
99+
module.exports.uniquePaths3 = uniquePaths3;

0 commit comments

Comments
 (0)