Skip to content

Commit addd55b

Browse files
Move to tests - Round 5
1 parent 9b91c32 commit addd55b

14 files changed

+141
-67
lines changed

LeetcodeProblems/Implement_stack_using_queues.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ You must use only standard operations of a queue -- which means only push to bac
2323
Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
2424
You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
2525
*/
26-
26+
const assert = require('assert');
2727
class MyStack {
2828
constructor() {
2929
this.q1 = [];
@@ -78,19 +78,22 @@ class MyStack {
7878
}
7979

8080
var main = function() {
81+
test();
82+
}
8183

82-
var myStack = new MyStack();
84+
var test = function () {
85+
var myStack = new MyStack();
8386
myStack.push(4);
8487
myStack.push(3);
8588
myStack.push(2);
8689
myStack.push(1);
87-
console.log(myStack.pop());
88-
console.log(myStack.top());
89-
console.log(myStack.push(1));
90-
console.log(myStack.top());
91-
console.log(myStack.pop());
92-
console.log(myStack.pop());
93-
console.log(myStack.pop());
90+
assert.equal(myStack.pop(), 1);
91+
assert.equal(myStack.top(), 2);
92+
myStack.push(1);
93+
assert.equal(myStack.top(), 1);
94+
assert.equal(myStack.pop(), 1);
95+
assert.equal(myStack.pop(), 2);
96+
assert.equal(myStack.pop(), 3);
9497
}
9598

9699
module.exports.main = main;

LeetcodeProblems/Kth_Largest_Element_in_an_Array.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Output: 4
1515
Note:
1616
You may assume k is always valid, 1 ≤ k ≤ array's length.
1717
*/
18+
const assert = require('assert');
1819

1920
/**
2021
* @param {number[]} nums
@@ -57,10 +58,14 @@ var swap = function(nums, a, b) {
5758
}
5859

5960
var main = function(nums) {
60-
console.log(findKthLargest([3,2,1,5,6,4], 2));
61-
console.log(findKthLargest([3,2,3,1,2,4,5,5,6], 4));
62-
console.log(findKthLargest([0], 1));
63-
console.log(findKthLargest([], 1));
61+
test();
62+
}
63+
64+
function test() {
65+
assert.equal(findKthLargest([3,2,1,5,6,4], 2), 5);
66+
assert.equal(findKthLargest([3,2,3,1,2,4,5,5,6], 4), 4);
67+
assert.equal(findKthLargest([0], 1), 0);
68+
assert.equal(findKthLargest([], 1), undefined);
6469
}
6570

6671
module.exports.main = main;

LeetcodeProblems/Linked_List_Cycle_II.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Note: Do not modify the linked list.
99
Follow up:
1010
Can you solve it without using extra space?
1111
*/
12-
12+
const assert = require('assert');
1313
var ListNode = require('../UtilsClasses/ListNode').ListNode;
1414

1515
// Optimal solution
@@ -58,8 +58,14 @@ var detectCycle2 = function(head) {
5858
};
5959

6060
var main = function() {
61-
const head = buildCycle();
62-
console.log(detectCycle(head));
61+
test();
62+
}
63+
64+
var test = function() {
65+
const cycle = buildCycle();
66+
var list = cycle.list;
67+
var nodeCycle = cycle.nodeCycle;
68+
assert.equal(detectCycle(list), nodeCycle);
6369
}
6470

6571
function buildCycle() {
@@ -73,8 +79,12 @@ function buildCycle() {
7379
node2.next = node3;
7480
node3.next = node4;
7581
node4.next = node5;
76-
node5.next = node2;
77-
return node1;
82+
node5.next = node2;
83+
84+
return {
85+
list: node1,
86+
nodeCycle: node2,
87+
};
7888
}
7989

8090
module.exports.main = main;

LeetcodeProblems/Longest_Consecutive_Sequence.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Input: [100, 4, 200, 1, 3, 2]
1212
Output: 4
1313
Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.
1414
*/
15+
const assert = require('assert');
1516

1617
/**
1718
* @param {number[]} nums
@@ -54,11 +55,15 @@ var longestConsecutive = function(nums) {
5455
};
5556

5657
var main = function() {
57-
console.log(longestConsecutive([100, 1, 200, 3, 2, 400, 201]));
58-
console.log(longestConsecutive([1,2,3,4, 100, 1, 200, 3, 2, 400, 201]));
59-
console.log(longestConsecutive([1, 400, 201, 403, 398]));
60-
console.log(longestConsecutive([]));
61-
console.log(longestConsecutive([2]));
58+
test();
59+
}
60+
61+
function test() {
62+
assert.equal(longestConsecutive([100, 1, 200, 3, 2, 400, 201]), 3);
63+
assert.equal(longestConsecutive([1,2,3,4, 100, 1, 200, 3, 2, 400, 201]), 4);
64+
assert.equal(longestConsecutive([1, 400, 201, 403, 398]), 1);
65+
assert.equal(longestConsecutive([]), 0);
66+
assert.equal(longestConsecutive([2]), 1);
6267
}
6368

6469
module.exports.main

LeetcodeProblems/Longest_Palindromic_Substring.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Example 2:
1414
Input: "cbbd"
1515
Output: "bb"
1616
*/
17+
const assert = require('assert');
1718

1819
/**
1920
* @param {string} s
@@ -64,12 +65,16 @@ var longestPalindrome = function(str) {
6465
}
6566

6667
var main = function() {
67-
console.log(longestPalindrome("pabcdcbte"));
68-
console.log(longestPalindrome("bb"));
69-
console.log(longestPalindrome(""));
70-
console.log(longestPalindrome("bbb"));
71-
console.log(longestPalindrome("bbbb"));
72-
console.log(longestPalindrome("ptabbbbat"));
68+
test();
69+
}
70+
71+
function test() {
72+
assert.equal(longestPalindrome("pabcdcbte"), "bcdcb");
73+
assert.equal(longestPalindrome("bb"), "bb");
74+
assert.equal(longestPalindrome(""), "");
75+
assert.equal(longestPalindrome("bbb"), "bbb");
76+
assert.equal(longestPalindrome("bbbb"), "bbbb");
77+
assert.equal(longestPalindrome("ptabbbbat"), "tabbbbat");
7378
}
7479

7580
module.exports.main = main

LeetcodeProblems/Lowest_Common_Ancestor_of_a_Binary_Tree.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Note:
3333
All of the nodes' values will be unique.
3434
p and q are different and both values will exist in the binary tree.
3535
*/
36+
const assert = require('assert');
3637

3738
var TreeNode = require('../UtilsClasses/TreeNode').TreeNode;
3839

LeetcodeProblems/Majority_Element.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Output: 2
1717
1818
Note: You should have a better solution than O(N)
1919
*/
20+
const assert = require('assert');
2021

2122
/**
2223
* @param {number[]} nums
@@ -45,9 +46,13 @@ var majorityElement = function(nums) {
4546
};
4647

4748
var main = function() {
48-
console.log(majorityElement([2,2,3]));
49-
console.log(majorityElement([2,3,2]));
50-
console.log(majorityElement([1,1,1,2,3,45,1,2,4,1,1]));
49+
test();
50+
}
51+
52+
function test() {
53+
assert.equal(majorityElement([2,2,3]), 2);
54+
assert.equal(majorityElement([2,3,2]), 2);
55+
assert.equal(majorityElement([1,1,1,2,3,45,1,2,4,1,1]), 1);
5156
}
5257

5358
module.exports.main = main

LeetcodeProblems/Maximal_Square.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Input:
1515
1616
Output: 4
1717
*/
18+
const assert = require('assert');
1819

1920
/**
2021
* @param {character[][]} matrix
@@ -65,11 +66,16 @@ var getCurrentMaxSideLength = function(matrix, i, j) {
6566
}
6667

6768
var main = function() {
68-
console.log(maximalSquare([["1","0"]]));
69-
console.log(maximalSquare([["1"]]));
70-
console.log(maximalSquare([["0"]]));
71-
console.log(
72-
maximalSquare([["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]])
69+
test();
70+
}
71+
72+
function test() {
73+
assert.equal(maximalSquare([["1","0"]]), 1);
74+
assert.equal(maximalSquare([["1"]]), 1);
75+
assert.equal(maximalSquare([["0"]]), 0);
76+
assert.equal(
77+
maximalSquare([["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]),
78+
4
7379
);
7480
}
7581

LeetcodeProblems/Maximun_Subarray.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Explanation: [4,-1,2,1] has the largest sum = 6.
1313
Follow up:
1414
1515
*/
16+
const assert = require('assert');
1617

1718
var maxSubArray = function(nums) {
1819
if(nums.length == 0)
@@ -34,10 +35,14 @@ var max = function(i, j) {
3435
}
3536

3637
var main = function() {
37-
console.log(maxSubArray([]));
38-
console.log(maxSubArray([-4]));
39-
console.log(maxSubArray([2]));
40-
console.log(maxSubArray([4,1,-1,4,5,6,7,-200]));
38+
test();
39+
}
40+
41+
function test() {
42+
assert.equal(maxSubArray([]), 0);
43+
assert.equal(maxSubArray([-4]), -4);
44+
assert.equal(maxSubArray([2]), 2);
45+
assert.equal(maxSubArray([4,1,-1,4,5,6,7,-200]), 26);
4146
}
4247

4348
module.exports.main = main;

LeetcodeProblems/Min_Stack.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ minStack.pop();
1919
minStack.top(); --> Returns 0.
2020
minStack.getMin(); --> Returns -2.
2121
*/
22+
const assert = require('assert');
2223

2324
class MinStack {
2425
constructor() {
@@ -74,14 +75,18 @@ class MinStack {
7475
}
7576

7677
var main = function() {
78+
test();
79+
}
80+
81+
function test() {
7782
var minStack = new MinStack();
7883
minStack.push(-2);
7984
minStack.push(0);
8085
minStack.push(-3);
81-
console.log(minStack.getMin());
82-
console.log(minStack.pop());
83-
console.log(minStack.top());
84-
console.log(minStack.getMin());
86+
assert.equal(minStack.getMin(), -3);
87+
assert.equal(minStack.pop(), -3);
88+
assert.equal(minStack.top(), 0);
89+
assert.equal(minStack.getMin(), -2);
8590
}
8691

8792
module.exports.main = main;

0 commit comments

Comments
 (0)