Skip to content

Add Tests #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Aug 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions LeetcodeProblems/3Sum.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ A solution set is:
[-1, -1, 2]
]
*/
const assert = require('assert');

/**
* @param {number[]} nums
Expand Down Expand Up @@ -49,12 +50,25 @@ var threeSum = function(nums) {
};

var main = function() {
console.log(threeSum([]));
console.log(threeSum([-1, 0, 1, 2, -1, -4]));
console.log(threeSum([0]));
console.log(threeSum([0, 0]));
console.log(threeSum([0, 0, 0]));
console.log(threeSum([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]));
test();
}

module.exports.main = main;
var test = function () {
assert.deepEqual(threeSum([]), []);
assert.deepEqual(threeSum([0]), []);
assert.deepEqual(threeSum([0, 0]), []);
assert.deepEqual(
threeSum([0, 0, 0]),
[[0, 0, 0]]
);
assert.deepEqual(
threeSum([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]),
[[0, 0, 0]]
);
assert.deepEqual(
threeSum([-1, 0, 1, 2, -1, -4]),
[ [ -1, 2, -1 ], [ 0, 1, -1 ] ]
);
}

module.exports.main = main;
24 changes: 19 additions & 5 deletions LeetcodeProblems/Add_Two_Numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ Explanation: 342 + 465 = 807.
* this.next = null;
* }
*/

const assert = require('assert');
const ListNodeTestHelper = require('../utilsClasses/ListNodeTestHelper');
var ListNode = require('../UtilsClasses/ListNode').ListNode;

/**
Expand Down Expand Up @@ -64,17 +65,30 @@ var addTwoNumbers = function(l1, l2) {
};

var main = function() {
test();
}

function test() {
const list1 = ListNode.linkenList([1,2,3,4]);
const list2 = ListNode.linkenList([1,2,3,4]);
console.log(addTwoNumbers(list1, list2));
ListNodeTestHelper.assertList(
addTwoNumbers(list1, list2),
[2, 4, 6, 8]
);

const list3 = ListNode.linkenList([1]);
const list4 = ListNode.linkenList([1,2]);
console.log(addTwoNumbers(list3, list4));
ListNodeTestHelper.assertList(
addTwoNumbers(list3, list4),
[2, 2]
);

const list5 = ListNode.linkenList([]);
const list6 = ListNode.linkenList([1,2]);
console.log(addTwoNumbers(list5, list6));
ListNodeTestHelper.assertList(
addTwoNumbers(list5, list6),
[1, 2]
);
}

module.exports.main = main;
module.exports.main = main;
15 changes: 13 additions & 2 deletions LeetcodeProblems/Award_Budget_Cuts.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Constraints:

[output] double
*/
const assert = require('assert');

var cutAwardBadges = function(nums, newBadge) {
var currentBadge = 0;
Expand Down Expand Up @@ -66,8 +67,18 @@ var findCap = function(nums, currentBadge, newBadge) {
}

var main = function() {
console.log(cutAwardBadges([2, 100, 50, 120, 1000], 190));
console.log(cutAwardBadges([2, 100, 50, 120, 1000], 5));
test();
}

function test() {
assert.deepEqual(
cutAwardBadges([2, 100, 50, 120, 1000], 190),
[ 47, 47, 47, 47, 2 ]
);
assert.deepEqual(
cutAwardBadges([2, 100, 50, 120, 1000], 5),
[ 1, 1, 1, 1, 1 ]
);
}

module.exports.main = main;
23 changes: 14 additions & 9 deletions LeetcodeProblems/Backspace_String_Compare.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Follow up:

Can you solve it in O(N) time and O(1) space?
*/
const assert = require('assert');

/**
* @param {string} S
Expand Down Expand Up @@ -106,15 +107,19 @@ var backspaceCompare2 = function(S, T) {
};

var main = function() {
console.log(backspaceCompare("ab#c", "ad#c")); // true
console.log(backspaceCompare("ab##", "c#d#")); // true
console.log(backspaceCompare("a##c", "#a#c")); // true
console.log(backspaceCompare("a#c", "b")); // false
test();
}

function test() {
assert.equal(backspaceCompare("ab#c", "ad#c"), true); // true
assert.equal(backspaceCompare("ab##", "c#d#"), true); // true
assert.equal(backspaceCompare("a##c", "#a#c"), true); // true
assert.equal(backspaceCompare("a#c", "b"), false); // false

console.log(backspaceCompare2("ab#c", "ad#c")); // true
console.log(backspaceCompare2("ab##", "c#d#")); // true
console.log(backspaceCompare2("a##c", "#a#c")); // true
console.log(backspaceCompare2("a#c", "b")); // false
assert.equal(backspaceCompare2("ab#c", "ad#c"), true); // true
assert.equal(backspaceCompare2("ab##", "c#d#"), true); // true
assert.equal(backspaceCompare2("a##c", "#a#c"), true); // true
assert.equal(backspaceCompare2("a#c", "b"), false); // false
}

module.exports.main = main;
module.exports.main = main;
9 changes: 7 additions & 2 deletions LeetcodeProblems/Best_Time_To_Buy_And_Sell_Stock_II.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.
*/
const assert = require('assert');

/**
* @param {number[]} prices
Expand Down Expand Up @@ -56,8 +57,12 @@ var maxProfit = function(prices) {
};

var main = function() {
console.log(maxProfit([7,1,5,3,6,4]));
console.log(maxProfit([7,1,5,3320,6,4]));
test();
}

function test() {
assert.equal(maxProfit([7,1,5,3,6,4]), 7);
assert.equal(maxProfit([7,1,5,3320,6,4]), 3319);
}

module.exports.main = main;
9 changes: 7 additions & 2 deletions LeetcodeProblems/Binary_Gap.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Explanation:
8 in binary is 0b1000.
There aren't any consecutive pairs of 1's in the binary representation of 8, so we return 0.
*/
const assert = require('assert');

/**
* @param {number} N
Expand All @@ -61,8 +62,12 @@ var binaryGap = function(N) {
};

var main = function() {
console.log(binaryGap(22)); // 10110
console.log(binaryGap(8)); // 1000
test();
}

function test() {
assert.equal(binaryGap(22), 2); // 10110
assert.equal(binaryGap(8), 0); // 1000
}

module.exports.main = main;
17 changes: 10 additions & 7 deletions LeetcodeProblems/Coin_Change.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Output: -1
Note:
You may assume that you have an infinite number of each kind of coin.
*/
const assert = require('assert');

// Solution 3
var coinChange = function(coins, amount) {
Expand Down Expand Up @@ -102,13 +103,15 @@ var min = function(a, b, c) {
}

function main() {
console.log("-------------");
console.log("Solution Optimal")
console.log(coinChange([], 3));
console.log(coinChange([2], 3));
console.log(coinChange([1, 2, 5], 11));
console.log(coinChange([3, 7, 405, 436], 8839));
console.log(coinChange([370, 417, 408, 156, 143, 434, 168, 83, 177, 280, 117], 9953));
test();
}

function test() {
assert.equal(coinChange([], 3), -1);
assert.equal(coinChange([2], 3), -1);
assert.equal(coinChange([1, 2, 5], 11), 3);
assert.equal(coinChange([3, 7, 405, 436], 8839), 25);
assert.equal(coinChange([370, 417, 408, 156, 143, 434, 168, 83, 177, 280, 117], 9953), 24);
}

module.exports.main = main;
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Return the following binary tree:
* this.left = this.right = null;
* }
*/
const assert = require('assert');

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

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

var main = function() {
test();
}

function test() {
console.log(buildTree([3,9,20,15,7], [9,3,15,20,7]));
}

Expand Down
33 changes: 19 additions & 14 deletions LeetcodeProblems/Deletion_Distance.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ output: 9
input: str1 = "", str2 = ""
output: 0
*/
const assert = require('assert');

// Solution 3 Using DP
var deletionDistanceDP = function(str1, str2) {
Expand Down Expand Up @@ -107,20 +108,24 @@ var min = function(a, b) {
}

function main() {
console.log(deletionDistance("dog", "frog")); //output: 3
console.log(deletionDistance("some", "some")); //output: 0
console.log(deletionDistance("some", "thing")); //output: 9
console.log(deletionDistance("", "")); // = 0

console.log(deletionDistance2("dog", "frog")); //output: 3
console.log(deletionDistance2("some", "some")); //output: 0
console.log(deletionDistance2("some", "thing")); //output: 9
console.log(deletionDistance2("", "")); // = 0

console.log(deletionDistanceDP("dog", "frog")); //output: 3
console.log(deletionDistanceDP("some", "some")); //output: 0
console.log(deletionDistanceDP("some", "thing")); //output: 9
console.log(deletionDistanceDP("", "")); // = 0
test();
}

function test() {
assert.equal(deletionDistance("dog", "frog"), 3);
assert.equal(deletionDistance("some", "some"), 0);
assert.equal(deletionDistance("some", "thing"), 9);
assert.equal(deletionDistance("", ""), 0);

assert.equal(deletionDistance2("dog", "frog"), 3);
assert.equal(deletionDistance2("some", "some"), 0);
assert.equal(deletionDistance2("some", "thing"), 9);
assert.equal(deletionDistance2("", ""), 0);

assert.equal(deletionDistanceDP("dog", "frog"), 3);
assert.equal(deletionDistanceDP("some", "some"), 0);
assert.equal(deletionDistanceDP("some", "thing"), 9);
assert.equal(deletionDistanceDP("", ""), 0);
}

module.exports.main = main
24 changes: 14 additions & 10 deletions LeetcodeProblems/Design_Circular_Deque.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ All values will be in the range of [0, 1000].
The number of operations will be in the range of [1, 1000].
Please do not use the built-in Deque library.
*/

const assert = require('assert');

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

var main = function(){
test();
};

var test = function() {
const obj = new MyCircularDeque(3);
console.log(obj.insertLast(1));
console.log(obj.insertLast(2));
console.log(obj.insertFront(3));
console.log(obj.insertFront(4));
console.log(obj.getRear());
console.log(obj.isFull());
console.log(obj.deleteLast());
console.log(obj.insertFront(4));
console.log(obj.getFront());
assert.equal(obj.insertLast(1), true);
assert.equal(obj.insertLast(2), true);
assert.equal(obj.insertFront(3), true);
assert.equal(obj.insertFront(4), false);
assert.equal(obj.getRear(), 2);
assert.equal(obj.isFull(), true);
assert.equal(obj.deleteLast(), true);
assert.equal(obj.insertFront(4), true);
assert.equal(obj.getFront(), 4);
}

module.exports.main = main;
17 changes: 9 additions & 8 deletions LeetcodeProblems/Edit_Distance.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ enention -> exention (replace 'n' with 'x')
exention -> exection (replace 'n' with 'c')
exection -> execution (insert 'u')
*/
const assert = require('assert');

// Optimal solution
var minDistance = function(word1, word2) {
Expand Down Expand Up @@ -98,15 +99,15 @@ var min = function(a, b, c) {
}

var main = function() {
console.log("-------------");
console.log("Approach 1");
console.log(minDistance("ros", "horse"));
console.log(minDistance("intention", "execution"));
test();
}

function test() {
assert.equal(minDistance("ros", "horse"), 3);
assert.equal(minDistance("intention", "execution"), 5);

console.log("-------------");
console.log("Approach 2");
console.log(minDistance2("ros", "horse"));
console.log(minDistance2("intention", "execution"));
assert.equal(minDistance2("ros", "horse"), 3);
assert.equal(minDistance2("intention", "execution"), 5);
}

module.exports.main = main;
11 changes: 8 additions & 3 deletions LeetcodeProblems/Escape_The_Ghosts.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Note:
All points have coordinates with absolute value <= 10000.
The number of ghosts will not exceed 100.
*/
const assert = require('assert');

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

var main = function() {
console.log(escapeGhosts([[1, 0], [0, 3]], [0, 1]));
console.log(escapeGhosts([[1, 0]], [2, 0]));
console.log(escapeGhosts([[2, 0]], [1, 0]));
test();
}

function test() {
assert.equal(escapeGhosts([[1, 0], [0, 3]], [0, 1]), true);
assert.equal(escapeGhosts([[1, 0]], [2, 0]), false);
assert.equal(escapeGhosts([[2, 0]], [1, 0]), true);
}

module.exports.main = main
Loading