Skip to content

Commit e3bd8df

Browse files
Move to tests - Round 6
1 parent addd55b commit e3bd8df

10 files changed

+98
-49
lines changed

LeetcodeProblems/Best_Time_To_Buy_And_Sell_Stock_II.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ 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');
3031

3132
/**
3233
* @param {number[]} prices
@@ -56,8 +57,12 @@ var maxProfit = function(prices) {
5657
};
5758

5859
var main = function() {
59-
console.log(maxProfit([7,1,5,3,6,4]));
60-
console.log(maxProfit([7,1,5,3320,6,4]));
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);
6166
}
6267

6368
module.exports.main = main;

LeetcodeProblems/Binary_Gap.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ 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');
3940

4041
/**
4142
* @param {number} N
@@ -61,8 +62,12 @@ var binaryGap = function(N) {
6162
};
6263

6364
var main = function() {
64-
console.log(binaryGap(22)); // 10110
65-
console.log(binaryGap(8)); // 1000
65+
test();
66+
}
67+
68+
function test() {
69+
assert.equal(binaryGap(22), 2); // 10110
70+
assert.equal(binaryGap(8), 0); // 1000
6671
}
6772

6873
module.exports.main = main;

LeetcodeProblems/Coin_Change.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ 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');
2122

2223
// Solution 3
2324
var coinChange = function(coins, amount) {
@@ -102,13 +103,15 @@ var min = function(a, b, c) {
102103
}
103104

104105
function main() {
105-
console.log("-------------");
106-
console.log("Solution Optimal")
107-
console.log(coinChange([], 3));
108-
console.log(coinChange([2], 3));
109-
console.log(coinChange([1, 2, 5], 11));
110-
console.log(coinChange([3, 7, 405, 436], 8839));
111-
console.log(coinChange([370, 417, 408, 156, 143, 434, 168, 83, 177, 280, 117], 9953));
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);
112115
}
113116

114117
module.exports.main = main;

LeetcodeProblems/Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Return the following binary tree:
2727
* this.left = this.right = null;
2828
* }
2929
*/
30+
const assert = require('assert');
3031

3132
var TreeNode = require('../UtilsClasses/TreeNode').TreeNode;
3233

@@ -59,6 +60,10 @@ var buildTreeAux = function(preorder, pl, ph, inorder, il, ih) {
5960
}
6061

6162
var main = function() {
63+
test();
64+
}
65+
66+
function test() {
6267
console.log(buildTree([3,9,20,15,7], [9,3,15,20,7]));
6368
}
6469

LeetcodeProblems/Deletion_Distance.js

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ output: 9
1818
input: str1 = "", str2 = ""
1919
output: 0
2020
*/
21+
const assert = require('assert');
2122

2223
// Solution 3 Using DP
2324
var deletionDistanceDP = function(str1, str2) {
@@ -107,20 +108,24 @@ var min = function(a, b) {
107108
}
108109

109110
function main() {
110-
console.log(deletionDistance("dog", "frog")); //output: 3
111-
console.log(deletionDistance("some", "some")); //output: 0
112-
console.log(deletionDistance("some", "thing")); //output: 9
113-
console.log(deletionDistance("", "")); // = 0
114-
115-
console.log(deletionDistance2("dog", "frog")); //output: 3
116-
console.log(deletionDistance2("some", "some")); //output: 0
117-
console.log(deletionDistance2("some", "thing")); //output: 9
118-
console.log(deletionDistance2("", "")); // = 0
119-
120-
console.log(deletionDistanceDP("dog", "frog")); //output: 3
121-
console.log(deletionDistanceDP("some", "some")); //output: 0
122-
console.log(deletionDistanceDP("some", "thing")); //output: 9
123-
console.log(deletionDistanceDP("", "")); // = 0
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);
124129
}
125130

126131
module.exports.main = main

LeetcodeProblems/Design_Circular_Deque.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ 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-
38+
const assert = require('assert');
3939

4040
/**
4141
* Initialize your data structure here. Set the size of the deque to be k.
@@ -133,16 +133,20 @@ MyCircularDeque.prototype.isFull = function() {
133133
};
134134

135135
var main = function(){
136+
test();
137+
};
138+
139+
var test = function() {
136140
const obj = new MyCircularDeque(3);
137-
console.log(obj.insertLast(1));
138-
console.log(obj.insertLast(2));
139-
console.log(obj.insertFront(3));
140-
console.log(obj.insertFront(4));
141-
console.log(obj.getRear());
142-
console.log(obj.isFull());
143-
console.log(obj.deleteLast());
144-
console.log(obj.insertFront(4));
145-
console.log(obj.getFront());
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);
146150
}
147151

148152
module.exports.main = main;

LeetcodeProblems/Edit_Distance.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ enention -> exention (replace 'n' with 'x')
2828
exention -> exection (replace 'n' with 'c')
2929
exection -> execution (insert 'u')
3030
*/
31+
const assert = require('assert');
3132

3233
// Optimal solution
3334
var minDistance = function(word1, word2) {
@@ -98,15 +99,15 @@ var min = function(a, b, c) {
9899
}
99100

100101
var main = function() {
101-
console.log("-------------");
102-
console.log("Approach 1");
103-
console.log(minDistance("ros", "horse"));
104-
console.log(minDistance("intention", "execution"));
102+
test();
103+
}
104+
105+
function test() {
106+
assert.equal(minDistance("ros", "horse"), 3);
107+
assert.equal(minDistance("intention", "execution"), 5);
105108

106-
console.log("-------------");
107-
console.log("Approach 2");
108-
console.log(minDistance2("ros", "horse"));
109-
console.log(minDistance2("intention", "execution"));
109+
assert.equal(minDistance2("ros", "horse"), 3);
110+
assert.equal(minDistance2("intention", "execution"), 5);
110111
}
111112

112113
module.exports.main = main;

LeetcodeProblems/Escape_The_Ghosts.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ 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');
3940

4041
/**
4142
* @param {number[][]} ghosts
@@ -60,9 +61,13 @@ var getDistance = function(a, b) {
6061
}
6162

6263
var main = function() {
63-
console.log(escapeGhosts([[1, 0], [0, 3]], [0, 1]));
64-
console.log(escapeGhosts([[1, 0]], [2, 0]));
65-
console.log(escapeGhosts([[2, 0]], [1, 0]));
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);
6671
}
6772

6873
module.exports.main = main

LeetcodeProblems/Flood_Fill.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ 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');
3132

3233
var floodFill = function(image, sr, sc, newColor) {
3334
var oldColor = image[sr][sc];
@@ -53,7 +54,14 @@ var floodFill = function(image, sr, sc, newColor) {
5354
};
5455

5556
function main() {
56-
console.log(floodFill([[1,1,1],[1,1,0],[1,0,1]], 1, 1, 2))
57+
test();
5758
}
5859

59-
module.exports.main = main;
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;

LeetcodeProblems/Group_Anagrams.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Note:
1818
All inputs will be in lowercase.
1919
The order of your output does not matter.
2020
*/
21+
const assert = require('assert');
2122

2223
var groupAnagrams = function(strs) {
2324
var ret = [];
@@ -48,7 +49,14 @@ var sortString = function(str) {
4849
}
4950

5051
var main = function() {
51-
console.log(groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"]));
52+
test();
53+
}
54+
55+
function test() {
56+
assert.deepEqual(
57+
groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"]),
58+
[ [ 'eat', 'tea', 'ate' ], [ 'tan', 'nat' ], [ 'bat' ] ]
59+
)
5260
}
5361

5462
module.exports.main = main;

0 commit comments

Comments
 (0)