Skip to content

Commit 9656e10

Browse files
Merge pull request ignacio-chiazzo#37 from ignacio-chiazzo/tests
Tests
2 parents b7e095a + 03905d4 commit 9656e10

File tree

112 files changed

+981
-957
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

112 files changed

+981
-957
lines changed

LeetcodeProblems/3Sum.js

+1-24
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ A solution set is:
1818
[-1, -1, 2]
1919
]
2020
*/
21-
const assert = require('assert');
2221

2322
/**
2423
* @param {number[]} nums
@@ -49,26 +48,4 @@ var threeSum = function(nums) {
4948
return ret;
5049
};
5150

52-
var main = function() {
53-
test();
54-
}
55-
56-
var test = function () {
57-
assert.deepEqual(threeSum([]), []);
58-
assert.deepEqual(threeSum([0]), []);
59-
assert.deepEqual(threeSum([0, 0]), []);
60-
assert.deepEqual(
61-
threeSum([0, 0, 0]),
62-
[[0, 0, 0]]
63-
);
64-
assert.deepEqual(
65-
threeSum([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]),
66-
[[0, 0, 0]]
67-
);
68-
assert.deepEqual(
69-
threeSum([-1, 0, 1, 2, -1, -4]),
70-
[ [ -1, 2, -1 ], [ 0, 1, -1 ] ]
71-
);
72-
}
73-
74-
module.exports.main = main;
51+
module.exports.threeSum = threeSum;

LeetcodeProblems/Add_Two_Numbers.js

+2-31
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ Explanation: 342 + 465 = 807.
2121
* this.next = null;
2222
* }
2323
*/
24-
const assert = require('assert');
25-
const ListNodeTestHelper = require('../utilsClasses/ListNodeTestHelper');
2624
var ListNode = require('../UtilsClasses/ListNode').ListNode;
27-
25+
2826
/**
2927
* @param {ListNode} l1
3028
* @param {ListNode} l2
@@ -64,31 +62,4 @@ var addTwoNumbers = function(l1, l2) {
6462
return head;
6563
};
6664

67-
var main = function() {
68-
test();
69-
}
70-
71-
function test() {
72-
const list1 = ListNode.linkenList([1,2,3,4]);
73-
const list2 = ListNode.linkenList([1,2,3,4]);
74-
ListNodeTestHelper.assertList(
75-
addTwoNumbers(list1, list2),
76-
[2, 4, 6, 8]
77-
);
78-
79-
const list3 = ListNode.linkenList([1]);
80-
const list4 = ListNode.linkenList([1,2]);
81-
ListNodeTestHelper.assertList(
82-
addTwoNumbers(list3, list4),
83-
[2, 2]
84-
);
85-
86-
const list5 = ListNode.linkenList([]);
87-
const list6 = ListNode.linkenList([1,2]);
88-
ListNodeTestHelper.assertList(
89-
addTwoNumbers(list5, list6),
90-
[1, 2]
91-
);
92-
}
93-
94-
module.exports.main = main;
65+
module.exports.addTwoNumbers = addTwoNumbers;

LeetcodeProblems/Award_Budget_Cuts.js

+1-18
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ Constraints:
2929
3030
[output] double
3131
*/
32-
const assert = require('assert');
33-
3432
var cutAwardBadges = function(nums, newBadge) {
3533
var currentBadge = 0;
3634
for(var i = 0; i < nums.length; i++)
@@ -66,19 +64,4 @@ var findCap = function(nums, currentBadge, newBadge) {
6664
return nums[iter] + (-diff) / iter;
6765
}
6866

69-
var main = function() {
70-
test();
71-
}
72-
73-
function test() {
74-
assert.deepEqual(
75-
cutAwardBadges([2, 100, 50, 120, 1000], 190),
76-
[ 47, 47, 47, 47, 2 ]
77-
);
78-
assert.deepEqual(
79-
cutAwardBadges([2, 100, 50, 120, 1000], 5),
80-
[ 1, 1, 1, 1, 1 ]
81-
);
82-
}
83-
84-
module.exports.main = main;
67+
module.exports.cutAwardBadges = cutAwardBadges;

LeetcodeProblems/Backspace_String_Compare.js

+2-18
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ Follow up:
3131
3232
Can you solve it in O(N) time and O(1) space?
3333
*/
34-
const assert = require('assert');
3534

3635
/**
3736
* @param {string} S
@@ -106,20 +105,5 @@ var backspaceCompare2 = function(S, T) {
106105
return stackS.length === 0 && stackT.length === 0;
107106
};
108107

109-
var main = function() {
110-
test();
111-
}
112-
113-
function test() {
114-
assert.equal(backspaceCompare("ab#c", "ad#c"), true); // true
115-
assert.equal(backspaceCompare("ab##", "c#d#"), true); // true
116-
assert.equal(backspaceCompare("a##c", "#a#c"), true); // true
117-
assert.equal(backspaceCompare("a#c", "b"), false); // false
118-
119-
assert.equal(backspaceCompare2("ab#c", "ad#c"), true); // true
120-
assert.equal(backspaceCompare2("ab##", "c#d#"), true); // true
121-
assert.equal(backspaceCompare2("a##c", "#a#c"), true); // true
122-
assert.equal(backspaceCompare2("a#c", "b"), false); // false
123-
}
124-
125-
module.exports.main = main;
108+
module.exports.backspaceCompare = backspaceCompare;
109+
module.exports.backspaceCompare2 = backspaceCompare2;

LeetcodeProblems/Best_Time_To_Buy_And_Sell_Stock_II.js

+1-11
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ Input: [7,6,4,3,1]
2727
Output: 0
2828
Explanation: In this case, no transaction is done, i.e. max profit = 0.
2929
*/
30-
const assert = require('assert');
3130

3231
/**
3332
* @param {number[]} prices
@@ -56,13 +55,4 @@ var maxProfit = function(prices) {
5655
return profit;
5756
};
5857

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

LeetcodeProblems/Binary_Gap.js

+1-11
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ Explanation:
3636
8 in binary is 0b1000.
3737
There aren't any consecutive pairs of 1's in the binary representation of 8, so we return 0.
3838
*/
39-
const assert = require('assert');
4039

4140
/**
4241
* @param {number} N
@@ -61,13 +60,4 @@ var binaryGap = function(N) {
6160
return maxDist;
6261
};
6362

64-
var main = function() {
65-
test();
66-
}
67-
68-
function test() {
69-
assert.equal(binaryGap(22), 2); // 10110
70-
assert.equal(binaryGap(8), 0); // 1000
71-
}
72-
73-
module.exports.main = main;
63+
module.exports.binaryGap = binaryGap;

LeetcodeProblems/Clone_Graph.js

-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ You don't need to understand the serialization to solve the problem.
4040
* }
4141
*/
4242

43-
4443
// SOLUTION 1 Using DFS
4544
/**
4645
* @param {UndirectedGraphNode} graph

LeetcodeProblems/Coin_Change.js

+1-14
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ Output: -1
1818
Note:
1919
You may assume that you have an infinite number of each kind of coin.
2020
*/
21-
const assert = require('assert');
2221

2322
// Solution 3
2423
var coinChange = function(coins, amount) {
@@ -102,16 +101,4 @@ var min = function(a, b, c) {
102101
return (b < c) ? b : c;
103102
}
104103

105-
function main() {
106-
test();
107-
}
108-
109-
function test() {
110-
assert.equal(coinChange([], 3), -1);
111-
assert.equal(coinChange([2], 3), -1);
112-
assert.equal(coinChange([1, 2, 5], 11), 3);
113-
assert.equal(coinChange([3, 7, 405, 436], 8839), 25);
114-
assert.equal(coinChange([370, 417, 408, 156, 143, 434, 168, 83, 177, 280, 117], 9953), 24);
115-
}
116-
117-
module.exports.main = main;
104+
module.exports.coinChange = coinChange;

LeetcodeProblems/Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal.js

+1-11
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ Return the following binary tree:
2727
* this.left = this.right = null;
2828
* }
2929
*/
30-
const assert = require('assert');
31-
3230
var TreeNode = require('../UtilsClasses/TreeNode').TreeNode;
3331

3432
/**
@@ -59,12 +57,4 @@ var buildTreeAux = function(preorder, pl, ph, inorder, il, ih) {
5957
return ret;
6058
}
6159

62-
var main = function() {
63-
test();
64-
}
65-
66-
function test() {
67-
console.log(buildTree([3,9,20,15,7], [9,3,15,20,7]));
68-
}
69-
70-
module.exports.main = main
60+
module.exports.buildTree = buildTree

LeetcodeProblems/Deletion_Distance.js

+3-23
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ output: 9
1818
input: str1 = "", str2 = ""
1919
output: 0
2020
*/
21-
const assert = require('assert');
2221

2322
// Solution 3 Using DP
2423
var deletionDistanceDP = function(str1, str2) {
@@ -107,25 +106,6 @@ var min = function(a, b) {
107106
return (a < b) ? a : b;
108107
}
109108

110-
function main() {
111-
test();
112-
}
113-
114-
function test() {
115-
assert.equal(deletionDistance("dog", "frog"), 3);
116-
assert.equal(deletionDistance("some", "some"), 0);
117-
assert.equal(deletionDistance("some", "thing"), 9);
118-
assert.equal(deletionDistance("", ""), 0);
119-
120-
assert.equal(deletionDistance2("dog", "frog"), 3);
121-
assert.equal(deletionDistance2("some", "some"), 0);
122-
assert.equal(deletionDistance2("some", "thing"), 9);
123-
assert.equal(deletionDistance2("", ""), 0);
124-
125-
assert.equal(deletionDistanceDP("dog", "frog"), 3);
126-
assert.equal(deletionDistanceDP("some", "some"), 0);
127-
assert.equal(deletionDistanceDP("some", "thing"), 9);
128-
assert.equal(deletionDistanceDP("", ""), 0);
129-
}
130-
131-
module.exports.main = main
109+
module.exports.deletionDistance = deletionDistance;
110+
module.exports.deletionDistance2 = deletionDistance2;
111+
module.exports.deletionDistanceDP = deletionDistanceDP;

LeetcodeProblems/Design_Circular_Deque.js

+1-19
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ All values will be in the range of [0, 1000].
3535
The number of operations will be in the range of [1, 1000].
3636
Please do not use the built-in Deque library.
3737
*/
38-
const assert = require('assert');
3938

4039
/**
4140
* Initialize your data structure here. Set the size of the deque to be k.
@@ -132,21 +131,4 @@ MyCircularDeque.prototype.isFull = function() {
132131
return this.queue.length === this.maxSize;
133132
};
134133

135-
var main = function(){
136-
test();
137-
};
138-
139-
var test = function() {
140-
const obj = new MyCircularDeque(3);
141-
assert.equal(obj.insertLast(1), true);
142-
assert.equal(obj.insertLast(2), true);
143-
assert.equal(obj.insertFront(3), true);
144-
assert.equal(obj.insertFront(4), false);
145-
assert.equal(obj.getRear(), 2);
146-
assert.equal(obj.isFull(), true);
147-
assert.equal(obj.deleteLast(), true);
148-
assert.equal(obj.insertFront(4), true);
149-
assert.equal(obj.getFront(), 4);
150-
}
151-
152-
module.exports.main = main;
134+
module.exports.MyCircularDeque = MyCircularDeque;

LeetcodeProblems/Edit_Distance.js

+2-14
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ enention -> exention (replace 'n' with 'x')
2828
exention -> exection (replace 'n' with 'c')
2929
exection -> execution (insert 'u')
3030
*/
31-
const assert = require('assert');
3231

3332
// Optimal solution
3433
var minDistance = function(word1, word2) {
@@ -98,16 +97,5 @@ var min = function(a, b, c) {
9897
return (b < c) ? b : c;
9998
}
10099

101-
var main = function() {
102-
test();
103-
}
104-
105-
function test() {
106-
assert.equal(minDistance("ros", "horse"), 3);
107-
assert.equal(minDistance("intention", "execution"), 5);
108-
109-
assert.equal(minDistance2("ros", "horse"), 3);
110-
assert.equal(minDistance2("intention", "execution"), 5);
111-
}
112-
113-
module.exports.main = main;
100+
module.exports.minDistance = minDistance;
101+
module.exports.minDistance2 = minDistance2;

LeetcodeProblems/Escape_The_Ghosts.js

+1-12
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ Note:
3636
All points have coordinates with absolute value <= 10000.
3737
The number of ghosts will not exceed 100.
3838
*/
39-
const assert = require('assert');
4039

4140
/**
4241
* @param {number[][]} ghosts
@@ -60,14 +59,4 @@ var getDistance = function(a, b) {
6059
return horizontalMoves + verticalMoves;
6160
}
6261

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

LeetcodeProblems/Flood_Fill.js

+1-13
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ The length of image and image[0] will be in the range [1, 50].
2828
The given starting pixel will satisfy 0 <= sr < image.length and 0 <= sc < image[0].length.
2929
The value of each color in image[i][j] and newColor will be an integer in [0, 65535].
3030
*/
31-
const assert = require('assert');
3231

3332
var floodFill = function(image, sr, sc, newColor) {
3433
var oldColor = image[sr][sc];
@@ -53,15 +52,4 @@ var floodFill = function(image, sr, sc, newColor) {
5352
return image;
5453
};
5554

56-
function main() {
57-
test();
58-
}
59-
60-
function test() {
61-
assert.deepEqual(
62-
[ [ 2, 2, 2 ], [ 2, 2, 0 ], [ 2, 0, 1 ] ],
63-
floodFill([[1,1,1],[1,1,0],[1,0,1]], 1, 1, 2)
64-
);
65-
}
66-
67-
module.exports.main = main;
55+
module.exports.floodFill = floodFill;

0 commit comments

Comments
 (0)