From f487669419231fd6d4906d2b83f9df0934eb4891 Mon Sep 17 00:00:00 2001 From: Ignacio Chiazzo Date: Sat, 29 Aug 2020 20:10:57 -0400 Subject: [PATCH 01/10] Update README --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 2af2bde..162bfd1 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,13 @@ Solutions of algorithm problems using Javascript. https://ignacio-chiazzo.github.io/Algorithms-Leetcode-Javascript/ +### Run Scripts + +Each problem has a main function exported which prints some cases (Tests are going to be added soon πŸ˜‰)). +To run a specific problem in your console run `node ` (e.g. `node LeetcodeProblems/Lowest_Common_Ancestor_of_a_Binary_Tree.js`). + +You can also run all the problems by running the `Main.js` file. + ### Leetcode Problems | Name | Level | Link | From 8adc481b7504dcb3903283d83ffa58210776e103 Mon Sep 17 00:00:00 2001 From: Ignacio Chiazzo Date: Sat, 29 Aug 2020 20:34:37 -0400 Subject: [PATCH 02/10] Move to tests - Round 1 --- ...Lowest_Common_Ancestor_of_a_Binary_Tree.js | 7 ++-- .../Unique_Binary_Search_Trees.js | 35 +++++++++++-------- LeetcodeProblems/Unique_Paths.js | 10 ++++-- LeetcodeProblems/Valid_Parentheses.js | 16 ++++++--- ...Preorder_Serialization_of_a_Binary_Tree.js | 18 ++++++---- 5 files changed, 55 insertions(+), 31 deletions(-) diff --git a/LeetcodeProblems/Lowest_Common_Ancestor_of_a_Binary_Tree.js b/LeetcodeProblems/Lowest_Common_Ancestor_of_a_Binary_Tree.js index 60fb52a..f78e4d7 100644 --- a/LeetcodeProblems/Lowest_Common_Ancestor_of_a_Binary_Tree.js +++ b/LeetcodeProblems/Lowest_Common_Ancestor_of_a_Binary_Tree.js @@ -4,7 +4,7 @@ https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. -According to the definition of LCA on Wikipedia: +According to the definition of LCA on Wikipedia: β€œThe lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).” @@ -92,7 +92,6 @@ var pathTo = function(root, value) { return []; } - var main = function() { var root = new TreeNode(3); @@ -126,4 +125,6 @@ var main = function() { console.log(lowestCommonAncestor2(root, left, right)); } -module.exports.main = main; \ No newline at end of file +module.exports.main = main; + +main(); \ No newline at end of file diff --git a/LeetcodeProblems/Unique_Binary_Search_Trees.js b/LeetcodeProblems/Unique_Binary_Search_Trees.js index 7e156a3..d75c871 100644 --- a/LeetcodeProblems/Unique_Binary_Search_Trees.js +++ b/LeetcodeProblems/Unique_Binary_Search_Trees.js @@ -22,6 +22,8 @@ Given n = 3, there are a total of 5 unique BST's: DP Solution: https://www.programcreek.com/2014/05/leetcode-unique-binary-search-trees-java/ */ +var assert = require('assert'); + // Solution 3 using DP var numTrees3 = function (n) { if (n == 0) @@ -108,20 +110,25 @@ var numTreesAux1 = function(leftMin, leftMax) { } var main = function() { - console.log(numTrees1(1)); - console.log(numTrees1(2)); - console.log(numTrees1(3)); - console.log(numTrees1(5)); - - console.log(numTrees2(1)); - console.log(numTrees2(2)); - console.log(numTrees2(3)); - console.log(numTrees2(5)); - - console.log(numTrees3(1)); - console.log(numTrees3(2)); - console.log(numTrees3(3)); - console.log(numTrees3(5)); + test(); +} + +var test = function () { + assert.strictEqual(numTrees1(1), 1); + assert.strictEqual(numTrees1(2), 2); + assert.strictEqual(numTrees1(3), 5); + assert.strictEqual(numTrees1(5), 42); + + assert.strictEqual(numTrees2(1), 1); + assert.strictEqual(numTrees2(2), 2); + assert.strictEqual(numTrees2(3), 5); + assert.strictEqual(numTrees2(5), 42); + + assert.strictEqual(numTrees3(1), 1); + assert.strictEqual(numTrees3(2), 2); + assert.strictEqual(numTrees3(3), 5); + assert.strictEqual(numTrees3(5), 42); } module.exports.main = main +main(); \ No newline at end of file diff --git a/LeetcodeProblems/Unique_Paths.js b/LeetcodeProblems/Unique_Paths.js index 01f8be9..f93aed1 100644 --- a/LeetcodeProblems/Unique_Paths.js +++ b/LeetcodeProblems/Unique_Paths.js @@ -26,6 +26,7 @@ Output: 28 // Solution 1 // This solution is a naive solution implementing a binary tree and visiting each node. +var assert = require('assert'); var uniquePaths1 = function(m, n) { return uniquePathsAux(0, 0, m, n) @@ -96,9 +97,12 @@ var uniquePaths3 = function(m, n) { }; var main = function() { - console.log(uniquePaths1(10,4)); - console.log(uniquePaths2(10,4)); - console.log(uniquePaths3(10,4)); + test(); +} + +var test = function() { + assert.strictEqual(uniquePaths1(10,4), 220); + assert.strictEqual(uniquePaths1(3,2), 3); } module.exports.main = main; \ No newline at end of file diff --git a/LeetcodeProblems/Valid_Parentheses.js b/LeetcodeProblems/Valid_Parentheses.js index fd8684b..072e4d0 100644 --- a/LeetcodeProblems/Valid_Parentheses.js +++ b/LeetcodeProblems/Valid_Parentheses.js @@ -32,6 +32,8 @@ Input: "{[]}" Output: true */ +var assert = require('assert'); + var isValid = function(s) { var stack = []; for(var i = 0; i < s.length; i++) { @@ -57,11 +59,15 @@ var valid = function(parOpen, parClose) { } var main = function(){ - console.log(isValid("")); - console.log(isValid("()")); - console.log(isValid("([)]")); - console.log(isValid("{[()]}{[()]}")); - console.log(isValid("{[())()]}")); + test(); +} + +var test = function () { + assert.strictEqual(isValid(""), true); + assert.strictEqual(isValid("()"), true); + assert.strictEqual(isValid("([)]"), false); + assert.strictEqual(isValid("{[()]}{[()]}"), true); + assert.strictEqual(isValid("{[())()]}"), false); } module.exports.main = main diff --git a/LeetcodeProblems/Verify_Preorder_Serialization_of_a_Binary_Tree.js b/LeetcodeProblems/Verify_Preorder_Serialization_of_a_Binary_Tree.js index 707611e..cea7121 100644 --- a/LeetcodeProblems/Verify_Preorder_Serialization_of_a_Binary_Tree.js +++ b/LeetcodeProblems/Verify_Preorder_Serialization_of_a_Binary_Tree.js @@ -31,6 +31,7 @@ Example 3: Input: "9,#,#,1" Output: false */ +var assert = require('assert'); /** * @param {string} preorder @@ -40,7 +41,7 @@ var isValidSerialization = function(preorder) { if(preorder.length === 0) return true; - if(preorder.charAt(0) === "#") + if(preorder.charAt(0) === "#") return preorder.length === 1; var countP = 2; @@ -64,11 +65,16 @@ var isValidSerialization = function(preorder) { }; var main = function() { - console.log(isValidSerialization("")); - console.log(isValidSerialization("#")); - console.log(isValidSerialization("9,3,4,#,#,1,#,#,2,#,6,#,#")); - console.log(isValidSerialization("9,#,92,#,#")); - console.log(isValidSerialization("9,3,4,#,#,1,#,#,#,2,#,6,#,#")); + test(); } +var test = function() { + assert.strictEqual(isValidSerialization(""), true); + assert.strictEqual(isValidSerialization(""), true); + assert.strictEqual(isValidSerialization("#"), true); + assert.strictEqual(isValidSerialization("9,3,4,#,#,1,#,#,2,#,6,#,#"), true); + assert.strictEqual(isValidSerialization("9,#,92,#,#"), true); + assert.strictEqual(isValidSerialization("9,3,4,#,#,1,#,#,#,2,#,6,#,#"), false); +}; + module.exports.main = main; From 961a21c7923d177b4bd22016de9d153df95548fd Mon Sep 17 00:00:00 2001 From: Ignacio Chiazzo Date: Sun, 30 Aug 2020 12:17:20 -0400 Subject: [PATCH 03/10] On it --- LeetcodeProblems/Swap_Nodes_In_Pairs.js | 20 ++++++--- LeetcodeProblems/Tic_Tac_Toe.js | 45 +++++++------------ .../Unique_Binary_Search_Trees.js | 2 +- LeetcodeProblems/Unique_Paths.js | 2 +- LeetcodeProblems/Valid_Parentheses.js | 2 +- ...Preorder_Serialization_of_a_Binary_Tree.js | 2 +- utilsClasses/ListNode.js | 13 +++++- utilsClasses/ListNodeTestHelper.js | 13 ++++++ 8 files changed, 58 insertions(+), 41 deletions(-) create mode 100644 utilsClasses/ListNodeTestHelper.js diff --git a/LeetcodeProblems/Swap_Nodes_In_Pairs.js b/LeetcodeProblems/Swap_Nodes_In_Pairs.js index 7ba45f1..d0ddf60 100644 --- a/LeetcodeProblems/Swap_Nodes_In_Pairs.js +++ b/LeetcodeProblems/Swap_Nodes_In_Pairs.js @@ -12,8 +12,9 @@ Note: Your algorithm should use only constant extra space. You may not modify the values in the list's nodes, only nodes itself may be changed. */ - -var ListNode = require('../UtilsClasses/ListNode').ListNode; +const assert = require('assert'); +const ListNode = require('../UtilsClasses/ListNode').ListNode; +const ListNodeTestHelper = require('../utilsClasses/ListNodeTestHelper'); /** * Definition for singly-linked list. @@ -49,10 +50,17 @@ var swapPairs = function(head) { }; var main = function() { - console.log(swapPairs(ListNode.linkenList([1,2,3,4]))); - console.log(swapPairs(ListNode.linkenList([]))); - console.log(swapPairs(ListNode.linkenList([1]))); - console.log(swapPairs(ListNode.linkenList([1,2]))); + + ListNodeTestHelper.assertList(swapPairs(ListNode.linkenList([2])), [2, 3]); + test(); +} + +var test = function () { + // TODOOOOOOOOOO + // ListNodeTestHelper.assertList(swapPairs(ListNode.linkenList([1,2,3,4])), [2,1,4,3]); + // ListNodeTestHelper.assertList(swapPairs(ListNode.linkenList([])), []); + // ListNodeTestHelper.assertList(swapPairs(ListNode.linkenList([1])), [1]); + ListNodeTestHelper.assertList(swapPairs(ListNode.linkenList([1,2])), [2, 3]); } module.exports.main = main; diff --git a/LeetcodeProblems/Tic_Tac_Toe.js b/LeetcodeProblems/Tic_Tac_Toe.js index 2b7e19e..363a14a 100644 --- a/LeetcodeProblems/Tic_Tac_Toe.js +++ b/LeetcodeProblems/Tic_Tac_Toe.js @@ -67,35 +67,20 @@ var main = function() { module.exports.main = main; ticTacToe = new TicTacToe(); -// ticTacToe.isBoardFull(); -// ticTacToe.addToken(0,1,"X"); -// ticTacToe.printBoard(); -// var iter = 0; -// while(iter < 8) { -// ticTacToe.makeMove(); -// iter++; -// } - -// console.log("after 8 moves"); -// ticTacToe.isBoardFull(); -// ticTacToe.printBoard(); -// ticTacToe.makeMove(); - -// ticTacToe.printBoard(); -// ticTacToe.addToken(0,0,"X"); -// ticTacToe.printBoard(); - - - -// // var readline = require('readline') - -// // const rl = readline.createInterface({ -// // input: process.stdin, -// // output: process.stdout -// // }); +ticTacToe.isBoardFull(); +ticTacToe.addToken(0,1,"X"); +ticTacToe.printBoard(); +var iter = 0; +while(iter < 8) { + ticTacToe.makeMove(); + iter++; +} -// // var response = rl.question('Whats your name : ', answer) +console.log("after 8 moves"); +ticTacToe.isBoardFull(); +ticTacToe.printBoard(); +ticTacToe.makeMove(); -// // function answer(response) { -// // console.log(response) -// // } \ No newline at end of file +ticTacToe.printBoard(); +ticTacToe.addToken(0,0,"X"); +ticTacToe.printBoard(); diff --git a/LeetcodeProblems/Unique_Binary_Search_Trees.js b/LeetcodeProblems/Unique_Binary_Search_Trees.js index d75c871..8794bc9 100644 --- a/LeetcodeProblems/Unique_Binary_Search_Trees.js +++ b/LeetcodeProblems/Unique_Binary_Search_Trees.js @@ -22,7 +22,7 @@ Given n = 3, there are a total of 5 unique BST's: DP Solution: https://www.programcreek.com/2014/05/leetcode-unique-binary-search-trees-java/ */ -var assert = require('assert'); +const assert = require('assert'); // Solution 3 using DP var numTrees3 = function (n) { diff --git a/LeetcodeProblems/Unique_Paths.js b/LeetcodeProblems/Unique_Paths.js index f93aed1..0a0092e 100644 --- a/LeetcodeProblems/Unique_Paths.js +++ b/LeetcodeProblems/Unique_Paths.js @@ -26,7 +26,7 @@ Output: 28 // Solution 1 // This solution is a naive solution implementing a binary tree and visiting each node. -var assert = require('assert'); +const assert = require('assert'); var uniquePaths1 = function(m, n) { return uniquePathsAux(0, 0, m, n) diff --git a/LeetcodeProblems/Valid_Parentheses.js b/LeetcodeProblems/Valid_Parentheses.js index 072e4d0..f4d5bc0 100644 --- a/LeetcodeProblems/Valid_Parentheses.js +++ b/LeetcodeProblems/Valid_Parentheses.js @@ -32,7 +32,7 @@ Input: "{[]}" Output: true */ -var assert = require('assert'); +const assert = require('assert'); var isValid = function(s) { var stack = []; diff --git a/LeetcodeProblems/Verify_Preorder_Serialization_of_a_Binary_Tree.js b/LeetcodeProblems/Verify_Preorder_Serialization_of_a_Binary_Tree.js index cea7121..4576c3c 100644 --- a/LeetcodeProblems/Verify_Preorder_Serialization_of_a_Binary_Tree.js +++ b/LeetcodeProblems/Verify_Preorder_Serialization_of_a_Binary_Tree.js @@ -31,7 +31,7 @@ Example 3: Input: "9,#,#,1" Output: false */ -var assert = require('assert'); +const assert = require('assert'); /** * @param {string} preorder diff --git a/utilsClasses/ListNode.js b/utilsClasses/ListNode.js index e9b433b..4c99fce 100644 --- a/utilsClasses/ListNode.js +++ b/utilsClasses/ListNode.js @@ -10,13 +10,24 @@ class ListNode { var first = new ListNode(arr[0]); var head = first; - for(var i = 1; i <= arr.length; i++) { + for(var i = 1; i < arr.length; i++) { head.next = new ListNode(arr[i]); head = head.next; } return first; } + + length() { + var list = this; + var size = 0; + while(list) { + list = list.next; + size++; + } + + return size; + } } module.exports.ListNode = ListNode; \ No newline at end of file diff --git a/utilsClasses/ListNodeTestHelper.js b/utilsClasses/ListNodeTestHelper.js new file mode 100644 index 0000000..5091714 --- /dev/null +++ b/utilsClasses/ListNodeTestHelper.js @@ -0,0 +1,13 @@ +const assert = require('assert'); + +var assertList = function(list, expectedArr) { + assert.strictEqual(list.length(), expectedArr.length); + for(var i = 0; i < expectedArr.length; i++) { + assert.strictEqual(list.val, expectedArr[i]); + list = list.next; + } + + assert(list) +} + +module.exports.assertList = assertList; \ No newline at end of file From d4c42abf8922971ae891f175c7d109278e78dd68 Mon Sep 17 00:00:00 2001 From: Ignacio Chiazzo Date: Sun, 30 Aug 2020 19:12:28 -0400 Subject: [PATCH 04/10] Added ListNodeTestHelper class --- utilsClasses/ListNodeTestHelper.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/utilsClasses/ListNodeTestHelper.js b/utilsClasses/ListNodeTestHelper.js index 5091714..2e18a18 100644 --- a/utilsClasses/ListNodeTestHelper.js +++ b/utilsClasses/ListNodeTestHelper.js @@ -1,13 +1,12 @@ const assert = require('assert'); var assertList = function(list, expectedArr) { - assert.strictEqual(list.length(), expectedArr.length); + const listlength = list ? list.length() : 0; + assert.strictEqual(listlength, expectedArr.length); for(var i = 0; i < expectedArr.length; i++) { assert.strictEqual(list.val, expectedArr[i]); list = list.next; } - - assert(list) } module.exports.assertList = assertList; \ No newline at end of file From 8c45ad547169a34ee61eafb6daa51f6cc618fd74 Mon Sep 17 00:00:00 2001 From: Ignacio Chiazzo Date: Sun, 30 Aug 2020 19:12:36 -0400 Subject: [PATCH 05/10] Move to tests - Round 2 --- ...Lowest_Common_Ancestor_of_a_Binary_Tree.js | 2 -- .../Regular_Expression_Matching.js | 1 - LeetcodeProblems/Simplify_Path.js | 2 ++ LeetcodeProblems/Spiral_Matrix.js | 8 ++++++-- LeetcodeProblems/Subarray_Sum_Equals_K.js | 15 +++++++++------ LeetcodeProblems/Subsets.js | 19 +++++++++++++++++-- LeetcodeProblems/Sum_Of_Square_Numbers.js | 19 +++++++++++++------ LeetcodeProblems/Swap_Nodes_In_Pairs.js | 11 ++++------- .../Unique_Binary_Search_Trees.js | 3 +-- 9 files changed, 52 insertions(+), 28 deletions(-) diff --git a/LeetcodeProblems/Lowest_Common_Ancestor_of_a_Binary_Tree.js b/LeetcodeProblems/Lowest_Common_Ancestor_of_a_Binary_Tree.js index f78e4d7..b9eace7 100644 --- a/LeetcodeProblems/Lowest_Common_Ancestor_of_a_Binary_Tree.js +++ b/LeetcodeProblems/Lowest_Common_Ancestor_of_a_Binary_Tree.js @@ -126,5 +126,3 @@ var main = function() { } module.exports.main = main; - -main(); \ No newline at end of file diff --git a/LeetcodeProblems/Regular_Expression_Matching.js b/LeetcodeProblems/Regular_Expression_Matching.js index d9a2115..a8dcaa6 100644 --- a/LeetcodeProblems/Regular_Expression_Matching.js +++ b/LeetcodeProblems/Regular_Expression_Matching.js @@ -98,5 +98,4 @@ var main = function(){ console.log(isMatch("mississippi", "mis*is*p*.")); } -main(); module.exports.main = main; diff --git a/LeetcodeProblems/Simplify_Path.js b/LeetcodeProblems/Simplify_Path.js index 61d1208..6754154 100644 --- a/LeetcodeProblems/Simplify_Path.js +++ b/LeetcodeProblems/Simplify_Path.js @@ -19,6 +19,7 @@ In this case, you should return "/". Another corner case is the path might contain multiple slashes '/' together, such as "/home//foo/". In this case, you should ignore redundant slashes and return "/home/foo". */ +const assert = require('assert'); var simplifyPath = function(path) { var queue = []; @@ -66,6 +67,7 @@ var main = function(){ console.log(simplifyPath("/a/./b/../../c/")); // => "/c" console.log(simplifyPath("/a/../../b/../c//.//")); // => "/c" console.log(simplifyPath("/a//b////c/d//././/..")) // => "/a/b/c" + } module.exports.main = main diff --git a/LeetcodeProblems/Spiral_Matrix.js b/LeetcodeProblems/Spiral_Matrix.js index f11c512..b3e293a 100644 --- a/LeetcodeProblems/Spiral_Matrix.js +++ b/LeetcodeProblems/Spiral_Matrix.js @@ -23,6 +23,7 @@ Input: ] Output: [1,2,3,4,8,12,11,10,9,5,6,7] */ +const assert = require('assert'); /** * @param {number[][]} matrix @@ -72,8 +73,11 @@ var main = function() { [ 4, 5, 6 ], [ 7, 8, 9 ] ] - - console.log(spiralOrder(matrix)); + + assert.deepEqual( + spiralOrder(matrix), + [1, 2, 3, 6, 9, 8, 7, 4, 5] + ) } module.exports.main = main; diff --git a/LeetcodeProblems/Subarray_Sum_Equals_K.js b/LeetcodeProblems/Subarray_Sum_Equals_K.js index 73221f4..0f75e13 100644 --- a/LeetcodeProblems/Subarray_Sum_Equals_K.js +++ b/LeetcodeProblems/Subarray_Sum_Equals_K.js @@ -12,7 +12,7 @@ Note: The length of the array is in range [1, 20,000]. The range of numbers in the array is [-1000, 1000] and the range of the integer k is [-1e7, 1e7]. */ - +const assert = require('assert'); /** * @param {number[]} nums @@ -61,11 +61,14 @@ var subarraySum2 = function(nums, k) { }; var main = function() { - console.log(subarraySum([1,1,1], 2)); - console.log(subarraySum([1], 0)); - console.log(subarraySum([0], 0)); - console.log(subarraySum([0,0,0,0,0], 0)); + test(); } -module.exports.main = main; +var test = function() { + assert.strictEqual(subarraySum([1,1,1], 2), 2); + assert.strictEqual(subarraySum([1], 0), 0); + assert.strictEqual(subarraySum([0], 0), 1); + assert.strictEqual(subarraySum([0,0,0,0,0], 0), 15); +} +module.exports.main = main; diff --git a/LeetcodeProblems/Subsets.js b/LeetcodeProblems/Subsets.js index 0e65d00..014ab43 100644 --- a/LeetcodeProblems/Subsets.js +++ b/LeetcodeProblems/Subsets.js @@ -22,6 +22,8 @@ Output: ] */ +const assert = require('assert'); + var subsets = function(nums) { var ret = []; @@ -37,7 +39,20 @@ var subsets = function(nums) { }; function main() { - console.log(subsets([1,2,3])); + test(); } - + +function test() { + assert.deepEqual(subsets([]), [[]]); + assert.deepEqual(subsets([1]), [[1], []]); + assert.deepEqual( + subsets([1,2]), + [[1, 2], [1], [2], []] + ); + assert.deepEqual( + subsets([1, 2, 3]), + [[1, 2, 3], [1, 2], [1, 3], [1], [2, 3], [2], [3], []] + ); +} + module.exports.main = main; diff --git a/LeetcodeProblems/Sum_Of_Square_Numbers.js b/LeetcodeProblems/Sum_Of_Square_Numbers.js index 28459ba..2146a45 100644 --- a/LeetcodeProblems/Sum_Of_Square_Numbers.js +++ b/LeetcodeProblems/Sum_Of_Square_Numbers.js @@ -14,6 +14,8 @@ Input: 3 Output: False */ +const assert = require('assert'); + /** * @param {number} c * @return {boolean} @@ -34,12 +36,17 @@ var judgeSquareSum = function(c) { }; var main = function() { - console.log(judgeSquareSum(0)); - console.log(judgeSquareSum(1)); - console.log(judgeSquareSum(5)); - console.log(judgeSquareSum(16)); - console.log(judgeSquareSum(24)); - console.log(judgeSquareSum(25)); + test(); +} + +var test = function() { + assert.strictEqual(judgeSquareSum(0), true); + assert.strictEqual(judgeSquareSum(1), true); + assert.strictEqual(judgeSquareSum(5), true); + assert.strictEqual(judgeSquareSum(16), true); + assert.strictEqual(judgeSquareSum(24), false); + assert.strictEqual(judgeSquareSum(25), true); } module.exports.main = main; + diff --git a/LeetcodeProblems/Swap_Nodes_In_Pairs.js b/LeetcodeProblems/Swap_Nodes_In_Pairs.js index d0ddf60..54c1faf 100644 --- a/LeetcodeProblems/Swap_Nodes_In_Pairs.js +++ b/LeetcodeProblems/Swap_Nodes_In_Pairs.js @@ -50,17 +50,14 @@ var swapPairs = function(head) { }; var main = function() { - - ListNodeTestHelper.assertList(swapPairs(ListNode.linkenList([2])), [2, 3]); test(); } var test = function () { - // TODOOOOOOOOOO - // ListNodeTestHelper.assertList(swapPairs(ListNode.linkenList([1,2,3,4])), [2,1,4,3]); - // ListNodeTestHelper.assertList(swapPairs(ListNode.linkenList([])), []); - // ListNodeTestHelper.assertList(swapPairs(ListNode.linkenList([1])), [1]); - ListNodeTestHelper.assertList(swapPairs(ListNode.linkenList([1,2])), [2, 3]); + ListNodeTestHelper.assertList(swapPairs(ListNode.linkenList([1,2,3,4])), [2,1,4,3]); + ListNodeTestHelper.assertList(swapPairs(ListNode.linkenList([])), []); + ListNodeTestHelper.assertList(swapPairs(ListNode.linkenList([1])), [1]); + ListNodeTestHelper.assertList(swapPairs(ListNode.linkenList([1,2])), [2, 1]); } module.exports.main = main; diff --git a/LeetcodeProblems/Unique_Binary_Search_Trees.js b/LeetcodeProblems/Unique_Binary_Search_Trees.js index 8794bc9..3bf1c2c 100644 --- a/LeetcodeProblems/Unique_Binary_Search_Trees.js +++ b/LeetcodeProblems/Unique_Binary_Search_Trees.js @@ -130,5 +130,4 @@ var test = function () { assert.strictEqual(numTrees3(5), 42); } -module.exports.main = main -main(); \ No newline at end of file +module.exports.main = main \ No newline at end of file From b12ad937eec715c5f52c63f25d1ba43b7d997bea Mon Sep 17 00:00:00 2001 From: Ignacio Chiazzo Date: Sun, 30 Aug 2020 19:19:19 -0400 Subject: [PATCH 06/10] Move to tests - Round 3 --- .../SearchIng_Rotated_Sorted_Array.js | 9 +++++++-- LeetcodeProblems/Set_Matrix_Zeroes.js | 13 ++++++++++++- LeetcodeProblems/Simplify_Path.js | 17 ++++++++++------- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/LeetcodeProblems/SearchIng_Rotated_Sorted_Array.js b/LeetcodeProblems/SearchIng_Rotated_Sorted_Array.js index 90600fb..ddcc22d 100644 --- a/LeetcodeProblems/SearchIng_Rotated_Sorted_Array.js +++ b/LeetcodeProblems/SearchIng_Rotated_Sorted_Array.js @@ -21,6 +21,7 @@ Input: nums = [4,5,6,7,0,1,2], target = 3 Output: -1 */ +const assert = require('assert'); /** * @param {number[]} nums @@ -54,7 +55,11 @@ var searchAux = function(nums, target, start, end) { } var main = function(n) { - console.log(search([4,5,6,7,0,1,2], 5)); + test(); } -module.exports.main = main; \ No newline at end of file +var test = function(n) { + assert.equal(search([4,5,6,7,0,1,2], 5), 1); +} + +module.exports.main = main; diff --git a/LeetcodeProblems/Set_Matrix_Zeroes.js b/LeetcodeProblems/Set_Matrix_Zeroes.js index a13df28..d92d8eb 100644 --- a/LeetcodeProblems/Set_Matrix_Zeroes.js +++ b/LeetcodeProblems/Set_Matrix_Zeroes.js @@ -39,6 +39,8 @@ A simple improvement uses O(m + n) space, but still not the best solution. Could you devise a constant space solution? */ +const assert = require('assert'); + /** * @param {number[][]} matrix * @return {void} Do not return anything, modify matrix in-place instead. @@ -94,6 +96,8 @@ var setZeroes = function(matrix) { fillCol(matrix, pivotCol); fillRow(matrix, pivotRow); + + return matrix; }; var fillRow = function(matrix, row) { @@ -107,7 +111,14 @@ var fillCol = function(matrix, col) { } var main = function() { - console.log(setZeroes([[1,1,1],[1,0,1],[1,1,1]])); + test(); +} + +var test = function() { + assert.deepEqual( + setZeroes([[1,1,1],[1,0,1],[1,1,1]]), + [[1, 0, 1], [0, 0, 0], [1, 0, 1]] + ); } module.exports.main = main; diff --git a/LeetcodeProblems/Simplify_Path.js b/LeetcodeProblems/Simplify_Path.js index 6754154..fdb6d2f 100644 --- a/LeetcodeProblems/Simplify_Path.js +++ b/LeetcodeProblems/Simplify_Path.js @@ -61,13 +61,16 @@ var simplifyPath = function(path) { }; var main = function(){ - console.log(simplifyPath("/../c")); - console.log(simplifyPath("/..")); - console.log(simplifyPath("/home/")); // => "/home" - console.log(simplifyPath("/a/./b/../../c/")); // => "/c" - console.log(simplifyPath("/a/../../b/../c//.//")); // => "/c" - console.log(simplifyPath("/a//b////c/d//././/..")) // => "/a/b/c" - + test(); +} + +var test = function() { + assert.equal(simplifyPath("/../c"), "/c"); + assert.equal(simplifyPath("/.."), "/"); + assert.equal(simplifyPath("/home/"), "/home"); // => "/home" + assert.equal(simplifyPath("/a/./b/../../c/"), "/c"); // => "/c" + assert.equal(simplifyPath("/a/../../b/../c//.//"), "/c"); // => "/c" + assert.equal(simplifyPath("/a//b////c/d//././/.."), "/a/b/c") // => "/a/b/c" } module.exports.main = main From 9b91c3228038762199a0f754b3c43f3744c20f14 Mon Sep 17 00:00:00 2001 From: Ignacio Chiazzo Date: Sun, 30 Aug 2020 19:45:45 -0400 Subject: [PATCH 07/10] Move to tests - Round 4 --- .../Number_of_Segments_in_a_String.js | 17 ++++++---- LeetcodeProblems/Permutations.js | 24 +++++++++++--- LeetcodeProblems/Permutations_II.js | 33 ++++++++++++++++--- .../Permutations_With_Duplicates.js | 26 +++++++++++++-- .../Permutations_Without_Duplicates.js | 19 +++++++++-- .../Regular_Expression_Matching.js | 17 ++++++---- .../Remove_Invalid_Parentheses.js | 13 +++++--- LeetcodeProblems/Restore_IP_Addresses.js | 9 +++-- LeetcodeProblems/Reverse_String_II.js | 14 +++++--- .../SearchIng_Rotated_Sorted_Array.js | 4 +-- LeetcodeProblems/Search_a_2D_Matrix.js | 13 +++++--- LeetcodeProblems/Search_a_2D_Matrix_II.js | 11 +++++-- 12 files changed, 156 insertions(+), 44 deletions(-) diff --git a/LeetcodeProblems/Number_of_Segments_in_a_String.js b/LeetcodeProblems/Number_of_Segments_in_a_String.js index af10a49..a6c367a 100644 --- a/LeetcodeProblems/Number_of_Segments_in_a_String.js +++ b/LeetcodeProblems/Number_of_Segments_in_a_String.js @@ -11,6 +11,7 @@ Example: Input: "Hello, my name is John" Output: 5 */ +const assert = require('assert'); /** * @param {string} s @@ -33,12 +34,16 @@ var countSegments = function(s) { }; function main() { - console.log(countSegments(" ")); - console.log(countSegments(" ")); - console.log(countSegments("ab cd ef")); - console.log(countSegments(" ab cd ef")); - console.log(countSegments("ab cd ef ")); - console.log(countSegments(" ab cd ef ")); + test(); +} + +function test() { + assert.equal(countSegments(" "), 0); + assert.equal(countSegments(" "), 0); + assert.equal(countSegments("ab cd ef"), 3); + assert.equal(countSegments(" ab cd ef"), 3); + assert.equal(countSegments("ab cd ef "), 3); + assert.equal(countSegments(" ab cd ef "), 3); } module.exports.main = main diff --git a/LeetcodeProblems/Permutations.js b/LeetcodeProblems/Permutations.js index de86a56..f04f128 100644 --- a/LeetcodeProblems/Permutations.js +++ b/LeetcodeProblems/Permutations.js @@ -17,6 +17,7 @@ Output: [3,2,1] ] */ +const assert = require('assert'); var permute = function(nums) { return permuteAux(nums, 0, [], new Set()); @@ -38,11 +39,24 @@ var permuteAux = function(nums, pos, currentSol, set) { return ret; } -var main = function() { - console.log(permute([])); - console.log(permute([1])); - console.log(permute([1,2,3])); - console.log(permute([1,2,3,4,5,6])); +var main = function() { test(); +} + +function test() { + // assert.deepEqual( + assert.deepEqual(permute([]), [ [] ]); + assert.deepEqual(permute([1]), [ [ 1 ] ]); + assert.deepEqual( + permute([1,2,3]), + [ + [ 1, 2, 3 ], + [ 1, 3, 2 ], + [ 2, 1, 3 ], + [ 2, 3, 1 ], + [ 3, 1, 2 ], + [ 3, 2, 1 ] + ] + ); } module.exports.main = main; diff --git a/LeetcodeProblems/Permutations_II.js b/LeetcodeProblems/Permutations_II.js index 5fef9b8..22093fe 100644 --- a/LeetcodeProblems/Permutations_II.js +++ b/LeetcodeProblems/Permutations_II.js @@ -14,6 +14,7 @@ Output: [2,1,1] ] */ +const assert = require('assert'); var permuteUnique = function(nums) { var map = {}; @@ -46,10 +47,34 @@ var permuteUniqueAux = function(n, map, currentSol) { }; var main = function() { - console.log(permuteUnique([1,1,2])); - console.log(permuteUnique([1,3,2,1])); - console.log(permuteUnique([])); - console.log(permuteUnique([1,1])); + test(); +} + +function test() { + assert.deepEqual( + permuteUnique([1,1,2]), + [ [ '1', '1', '2' ], [ '1', '2', '1' ], [ '2', '1', '1' ] ] + ); + assert.deepEqual( + permuteUnique([1,3,2,1]), + [ + [ '1', '1', '2', '3' ], + [ '1', '1', '3', '2' ], + [ '1', '2', '1', '3' ], + [ '1', '2', '3', '1' ], + [ '1', '3', '1', '2' ], + [ '1', '3', '2', '1' ], + [ '2', '1', '1', '3' ], + [ '2', '1', '3', '1' ], + [ '2', '3', '1', '1' ], + [ '3', '1', '1', '2' ], + [ '3', '1', '2', '1' ], + [ '3', '2', '1', '1' ] + ] + ); + assert.deepEqual(permuteUnique([]), [ [] ]); + + assert.deepEqual(permuteUnique([1,1]), [ [ '1', '1' ] ]); } module.exports.main = main; \ No newline at end of file diff --git a/LeetcodeProblems/Permutations_With_Duplicates.js b/LeetcodeProblems/Permutations_With_Duplicates.js index 021fead..e9fccc5 100644 --- a/LeetcodeProblems/Permutations_With_Duplicates.js +++ b/LeetcodeProblems/Permutations_With_Duplicates.js @@ -1,3 +1,5 @@ +const assert = require('assert'); + // Permutations without var subsetWithoutDuplicates = function(nums) { if(nums.lenght == 0){ @@ -5,7 +7,7 @@ var subsetWithoutDuplicates = function(nums) { } var solution = []; subsetWithoutDuplicatesAux(nums, [], solution); - console.log(solution); + return solution; } var subsetWithoutDuplicatesAux = function(nums, current, sol) { @@ -28,7 +30,27 @@ var subsetWithoutDuplicatesAux = function(nums, current, sol) { } function main() { - subsetWithoutDuplicates([1,1,2,3]); + test(); } +var test = function() { + assert.deepEqual( + subsetWithoutDuplicates([1,1,2,3]), + [ + [ 1, 1, 2, 3 ], + [ 1, 1, 3, 2 ], + [ 1, 2, 1, 3 ], + [ 1, 2, 3, 1 ], + [ 1, 3, 1, 2 ], + [ 1, 3, 2, 1 ], + [ 2, 1, 1, 3 ], + [ 2, 1, 3, 1 ], + [ 2, 3, 1, 1 ], + [ 3, 1, 1, 2 ], + [ 3, 1, 2, 1 ], + [ 3, 2, 1, 1 ] + ] + ); +} +main(); module.exports.main = main; \ No newline at end of file diff --git a/LeetcodeProblems/Permutations_Without_Duplicates.js b/LeetcodeProblems/Permutations_Without_Duplicates.js index dadc798..b5b0a1b 100644 --- a/LeetcodeProblems/Permutations_Without_Duplicates.js +++ b/LeetcodeProblems/Permutations_Without_Duplicates.js @@ -17,6 +17,7 @@ Output: [3,2,1] ] */ +const assert = require('assert'); // Permutations wihto var subsetWithDuplicates = function(nums) { @@ -25,7 +26,7 @@ var subsetWithDuplicates = function(nums) { } var solution = []; subsetWithDuplicatesAux(nums, [], solution); - console.log(solution); + return solution; } var subsetWithDuplicatesAux = function(nums, current, sol) { @@ -41,7 +42,21 @@ var subsetWithDuplicatesAux = function(nums, current, sol) { } function main() { - subsetWithDuplicates([1,2,3]) + test(); +} + +var test = function() { + assert.deepEqual( + subsetWithDuplicates([1,2,3]), + [ + [ 1, 2, 3 ], + [ 1, 3, 2 ], + [ 2, 1, 3 ], + [ 2, 3, 1 ], + [ 3, 1, 2 ], + [ 3, 2, 1 ] + ] + ); } module.exports.main = main; \ No newline at end of file diff --git a/LeetcodeProblems/Regular_Expression_Matching.js b/LeetcodeProblems/Regular_Expression_Matching.js index a8dcaa6..3aaedf0 100644 --- a/LeetcodeProblems/Regular_Expression_Matching.js +++ b/LeetcodeProblems/Regular_Expression_Matching.js @@ -50,6 +50,7 @@ Output: false * @param {*} s * @param {*} p */ +const assert = require('assert'); var isMatch = function(s, p) { return isMatchAux(s, p, 0, 0); @@ -90,12 +91,16 @@ var canBeZero = function(pattern, posPat) { } var main = function(){ - console.log(isMatch("aa", "a")); - console.log(isMatch("aa", "a*")); - console.log(isMatch("a","ab*")); - console.log(isMatch("ab", ".*")); - console.log(isMatch("aab", "c*a*b")); - console.log(isMatch("mississippi", "mis*is*p*.")); + test(); +} + +var test = function(n) { + assert.equal(isMatch("aa", "a"), false); + assert.equal(isMatch("aa", "a*"), true); + assert.equal(isMatch("a","ab*"), true); + assert.equal(isMatch("ab", ".*"), true); + assert.equal(isMatch("aab", "c*a*b"), true); + assert.equal(isMatch("mississippi", "mis*is*p*."), false); } module.exports.main = main; diff --git a/LeetcodeProblems/Remove_Invalid_Parentheses.js b/LeetcodeProblems/Remove_Invalid_Parentheses.js index 0788026..fdb4fab 100644 --- a/LeetcodeProblems/Remove_Invalid_Parentheses.js +++ b/LeetcodeProblems/Remove_Invalid_Parentheses.js @@ -19,6 +19,7 @@ Example 3: Input: ")(" Output: [""] */ +const assert = require('assert'); /** * @param {string} s @@ -70,10 +71,14 @@ var isValid = function(s) { } var main = function() { - console.log(removeInvalidParentheses("))))(()")); - console.log(removeInvalidParentheses("(()")); - console.log(removeInvalidParentheses("(d))()")); - console.log(removeInvalidParentheses("(())")) + test(); +} + +var test = function(n) { + assert.equal(removeInvalidParentheses("))))(()"), "()"); + assert.equal(removeInvalidParentheses("(()"), "()"); + assert.equal(removeInvalidParentheses("(d))()"), "(d)()"); + assert.equal(removeInvalidParentheses("(())"), "(())"); } module.exports.main = main; \ No newline at end of file diff --git a/LeetcodeProblems/Restore_IP_Addresses.js b/LeetcodeProblems/Restore_IP_Addresses.js index 2b938bc..2394963 100644 --- a/LeetcodeProblems/Restore_IP_Addresses.js +++ b/LeetcodeProblems/Restore_IP_Addresses.js @@ -9,6 +9,7 @@ Example: Input: "25525511135" Output: ["255.255.11.135", "255.255.111.35"] */ +const assert = require('assert'); var restoreIpAddresses = function(s) { var restore = restoreInputBits("", s, 4); @@ -41,8 +42,12 @@ var restoreInputBits = function(partial, s, num) { } var main = function() { - console.log(restoreIpAddresses("010010")); - console.log(restoreIpAddresses("25525511135")); + test(); +} + +var test = function(n) { + assert.deepEqual(restoreIpAddresses("010010"), [ '0.10.0.10', '0.100.1.0']); + assert.deepEqual(restoreIpAddresses("25525511135"), [ '255.255.11.135', '255.255.111.35' ]); } module.exports.main = main diff --git a/LeetcodeProblems/Reverse_String_II.js b/LeetcodeProblems/Reverse_String_II.js index d17b02b..d277483 100644 --- a/LeetcodeProblems/Reverse_String_II.js +++ b/LeetcodeProblems/Reverse_String_II.js @@ -10,6 +10,8 @@ Restrictions: The string consists of lower English letters only. Length of the given string and k will in the range [1, 10000] */ +const assert = require('assert'); + var reverseStr = function(s, k) { if(k <= 1) return s; @@ -38,10 +40,14 @@ var reverse = function(s, start, end) { } var main = function(){ - console.log(reverseStr("abcdefg", 2)); - console.log(reverseStr("abcdefg", 3)); - console.log(reverseStr("abcdefg", 1)); - console.log(reverseStr("abcdefg", 0)); + test(); +} + +var test = function(n) { + assert.equal(reverseStr("abcdefg", 2), "bacdfeg"); + assert.equal(reverseStr("abcdefg", 3), "cbadefg"); + assert.equal(reverseStr("abcdefg", 1), "abcdefg"); + assert.equal(reverseStr("abcdefg", 0), "abcdefg"); } module.exports.main = main diff --git a/LeetcodeProblems/SearchIng_Rotated_Sorted_Array.js b/LeetcodeProblems/SearchIng_Rotated_Sorted_Array.js index ddcc22d..d095215 100644 --- a/LeetcodeProblems/SearchIng_Rotated_Sorted_Array.js +++ b/LeetcodeProblems/SearchIng_Rotated_Sorted_Array.js @@ -58,8 +58,8 @@ var main = function(n) { test(); } -var test = function(n) { +var test = function() { assert.equal(search([4,5,6,7,0,1,2], 5), 1); } - +main() module.exports.main = main; diff --git a/LeetcodeProblems/Search_a_2D_Matrix.js b/LeetcodeProblems/Search_a_2D_Matrix.js index 70a5897..0a0b835 100644 --- a/LeetcodeProblems/Search_a_2D_Matrix.js +++ b/LeetcodeProblems/Search_a_2D_Matrix.js @@ -27,6 +27,7 @@ matrix = [ target = 13 Output: false */ +const assert = require('assert'); /** * @param {number[][]} matrix @@ -59,11 +60,15 @@ var searchMatrixAux = function(matrix, firstRow, lastRow, target) { }; var main = function(){ - console.log(searchMatrix([], 0)); - console.log(searchMatrix([[1], [3]], 3)); - console.log(searchMatrix([[1], [3]], 1)); + test(); +} + +var test = function(n) { + assert.equal(searchMatrix([], 0), false); + assert.equal(searchMatrix([[1], [3]], 3), true); + assert.equal(searchMatrix([[1], [3]], 1), true); const matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,50]]; - console.log(searchMatrix(matrix, 3)); + assert.equal(searchMatrix(matrix, 3), true); } module.exports.main = main; diff --git a/LeetcodeProblems/Search_a_2D_Matrix_II.js b/LeetcodeProblems/Search_a_2D_Matrix_II.js index e34a2f5..7085dc3 100644 --- a/LeetcodeProblems/Search_a_2D_Matrix_II.js +++ b/LeetcodeProblems/Search_a_2D_Matrix_II.js @@ -17,6 +17,7 @@ Example: Given target = 5, return true. Given target = 20, return false. */ +const assert = require('assert'); /** * @param {number[][]} matrix @@ -51,9 +52,13 @@ const matrix1 = [ ]; var main = function(n) { - console.log(searchMatrix(matrix1, 5)); - console.log(searchMatrix(matrix1, 0)); - console.log(searchMatrix(matrix1, 15)); + test(); +} + +var test = function(n) { + assert.equal(searchMatrix(matrix1, 5), true); + assert.equal(searchMatrix(matrix1, 0), false); + assert.equal(searchMatrix(matrix1, 15), true); } module.exports.main = main; From addd55b50ae43e0d087d0bc3d73535e8a4a8d427 Mon Sep 17 00:00:00 2001 From: Ignacio Chiazzo Date: Sun, 30 Aug 2020 20:11:21 -0400 Subject: [PATCH 08/10] Move to tests - Round 5 --- .../Implement_stack_using_queues.js | 21 ++++++++------ .../Kth_Largest_Element_in_an_Array.js | 13 ++++++--- LeetcodeProblems/Linked_List_Cycle_II.js | 20 +++++++++---- .../Longest_Consecutive_Sequence.js | 15 ++++++---- .../Longest_Palindromic_Substring.js | 17 +++++++---- ...Lowest_Common_Ancestor_of_a_Binary_Tree.js | 1 + LeetcodeProblems/Majority_Element.js | 11 +++++-- LeetcodeProblems/Maximal_Square.js | 16 ++++++---- LeetcodeProblems/Maximun_Subarray.js | 13 ++++++--- LeetcodeProblems/Min_Stack.js | 13 ++++++--- LeetcodeProblems/Minimum_Window_Substring.js | 18 +++++++----- LeetcodeProblems/NQueens.js | 4 +++ LeetcodeProblems/Number_of_Islands.js | 17 +++++++---- LeetcodeProblems/merge_k_sorted_lists.js | 29 ++++++++++++------- 14 files changed, 141 insertions(+), 67 deletions(-) diff --git a/LeetcodeProblems/Implement_stack_using_queues.js b/LeetcodeProblems/Implement_stack_using_queues.js index 0efc9ac..9f33c41 100644 --- a/LeetcodeProblems/Implement_stack_using_queues.js +++ b/LeetcodeProblems/Implement_stack_using_queues.js @@ -23,7 +23,7 @@ You must use only standard operations of a queue -- which means only push to bac 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. You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack). */ - +const assert = require('assert'); class MyStack { constructor() { this.q1 = []; @@ -78,19 +78,22 @@ class MyStack { } var main = function() { + test(); +} -var myStack = new MyStack(); +var test = function () { + var myStack = new MyStack(); myStack.push(4); myStack.push(3); myStack.push(2); myStack.push(1); - console.log(myStack.pop()); - console.log(myStack.top()); - console.log(myStack.push(1)); - console.log(myStack.top()); - console.log(myStack.pop()); - console.log(myStack.pop()); - console.log(myStack.pop()); + assert.equal(myStack.pop(), 1); + assert.equal(myStack.top(), 2); + myStack.push(1); + assert.equal(myStack.top(), 1); + assert.equal(myStack.pop(), 1); + assert.equal(myStack.pop(), 2); + assert.equal(myStack.pop(), 3); } module.exports.main = main; diff --git a/LeetcodeProblems/Kth_Largest_Element_in_an_Array.js b/LeetcodeProblems/Kth_Largest_Element_in_an_Array.js index 52d6784..51ed6cb 100644 --- a/LeetcodeProblems/Kth_Largest_Element_in_an_Array.js +++ b/LeetcodeProblems/Kth_Largest_Element_in_an_Array.js @@ -15,6 +15,7 @@ Output: 4 Note: You may assume k is always valid, 1 ≀ k ≀ array's length. */ +const assert = require('assert'); /** * @param {number[]} nums @@ -57,10 +58,14 @@ var swap = function(nums, a, b) { } var main = function(nums) { - console.log(findKthLargest([3,2,1,5,6,4], 2)); - console.log(findKthLargest([3,2,3,1,2,4,5,5,6], 4)); - console.log(findKthLargest([0], 1)); - console.log(findKthLargest([], 1)); + test(); +} + +function test() { + assert.equal(findKthLargest([3,2,1,5,6,4], 2), 5); + assert.equal(findKthLargest([3,2,3,1,2,4,5,5,6], 4), 4); + assert.equal(findKthLargest([0], 1), 0); + assert.equal(findKthLargest([], 1), undefined); } module.exports.main = main; diff --git a/LeetcodeProblems/Linked_List_Cycle_II.js b/LeetcodeProblems/Linked_List_Cycle_II.js index 2f1c788..9573e63 100644 --- a/LeetcodeProblems/Linked_List_Cycle_II.js +++ b/LeetcodeProblems/Linked_List_Cycle_II.js @@ -9,7 +9,7 @@ Note: Do not modify the linked list. Follow up: Can you solve it without using extra space? */ - +const assert = require('assert'); var ListNode = require('../UtilsClasses/ListNode').ListNode; // Optimal solution @@ -58,8 +58,14 @@ var detectCycle2 = function(head) { }; var main = function() { - const head = buildCycle(); - console.log(detectCycle(head)); + test(); +} + +var test = function() { + const cycle = buildCycle(); + var list = cycle.list; + var nodeCycle = cycle.nodeCycle; + assert.equal(detectCycle(list), nodeCycle); } function buildCycle() { @@ -73,8 +79,12 @@ function buildCycle() { node2.next = node3; node3.next = node4; node4.next = node5; - node5.next = node2; - return node1; + node5.next = node2; + + return { + list: node1, + nodeCycle: node2, + }; } module.exports.main = main; diff --git a/LeetcodeProblems/Longest_Consecutive_Sequence.js b/LeetcodeProblems/Longest_Consecutive_Sequence.js index 887b547..290d9ea 100644 --- a/LeetcodeProblems/Longest_Consecutive_Sequence.js +++ b/LeetcodeProblems/Longest_Consecutive_Sequence.js @@ -12,6 +12,7 @@ Input: [100, 4, 200, 1, 3, 2] Output: 4 Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4. */ +const assert = require('assert'); /** * @param {number[]} nums @@ -54,11 +55,15 @@ var longestConsecutive = function(nums) { }; var main = function() { - console.log(longestConsecutive([100, 1, 200, 3, 2, 400, 201])); - console.log(longestConsecutive([1,2,3,4, 100, 1, 200, 3, 2, 400, 201])); - console.log(longestConsecutive([1, 400, 201, 403, 398])); - console.log(longestConsecutive([])); - console.log(longestConsecutive([2])); + test(); +} + +function test() { + assert.equal(longestConsecutive([100, 1, 200, 3, 2, 400, 201]), 3); + assert.equal(longestConsecutive([1,2,3,4, 100, 1, 200, 3, 2, 400, 201]), 4); + assert.equal(longestConsecutive([1, 400, 201, 403, 398]), 1); + assert.equal(longestConsecutive([]), 0); + assert.equal(longestConsecutive([2]), 1); } module.exports.main diff --git a/LeetcodeProblems/Longest_Palindromic_Substring.js b/LeetcodeProblems/Longest_Palindromic_Substring.js index 31c199e..74a2447 100644 --- a/LeetcodeProblems/Longest_Palindromic_Substring.js +++ b/LeetcodeProblems/Longest_Palindromic_Substring.js @@ -14,6 +14,7 @@ Example 2: Input: "cbbd" Output: "bb" */ +const assert = require('assert'); /** * @param {string} s @@ -64,12 +65,16 @@ var longestPalindrome = function(str) { } var main = function() { - console.log(longestPalindrome("pabcdcbte")); - console.log(longestPalindrome("bb")); - console.log(longestPalindrome("")); - console.log(longestPalindrome("bbb")); - console.log(longestPalindrome("bbbb")); - console.log(longestPalindrome("ptabbbbat")); + test(); +} + +function test() { + assert.equal(longestPalindrome("pabcdcbte"), "bcdcb"); + assert.equal(longestPalindrome("bb"), "bb"); + assert.equal(longestPalindrome(""), ""); + assert.equal(longestPalindrome("bbb"), "bbb"); + assert.equal(longestPalindrome("bbbb"), "bbbb"); + assert.equal(longestPalindrome("ptabbbbat"), "tabbbbat"); } module.exports.main = main diff --git a/LeetcodeProblems/Lowest_Common_Ancestor_of_a_Binary_Tree.js b/LeetcodeProblems/Lowest_Common_Ancestor_of_a_Binary_Tree.js index b9eace7..c4f0168 100644 --- a/LeetcodeProblems/Lowest_Common_Ancestor_of_a_Binary_Tree.js +++ b/LeetcodeProblems/Lowest_Common_Ancestor_of_a_Binary_Tree.js @@ -33,6 +33,7 @@ Note: All of the nodes' values will be unique. p and q are different and both values will exist in the binary tree. */ +const assert = require('assert'); var TreeNode = require('../UtilsClasses/TreeNode').TreeNode; diff --git a/LeetcodeProblems/Majority_Element.js b/LeetcodeProblems/Majority_Element.js index 4e4b026..f1114f8 100644 --- a/LeetcodeProblems/Majority_Element.js +++ b/LeetcodeProblems/Majority_Element.js @@ -17,6 +17,7 @@ Output: 2 Note: You should have a better solution than O(N) */ +const assert = require('assert'); /** * @param {number[]} nums @@ -45,9 +46,13 @@ var majorityElement = function(nums) { }; var main = function() { - console.log(majorityElement([2,2,3])); - console.log(majorityElement([2,3,2])); - console.log(majorityElement([1,1,1,2,3,45,1,2,4,1,1])); + test(); +} + +function test() { + assert.equal(majorityElement([2,2,3]), 2); + assert.equal(majorityElement([2,3,2]), 2); + assert.equal(majorityElement([1,1,1,2,3,45,1,2,4,1,1]), 1); } module.exports.main = main diff --git a/LeetcodeProblems/Maximal_Square.js b/LeetcodeProblems/Maximal_Square.js index de9d9cb..014d4a8 100644 --- a/LeetcodeProblems/Maximal_Square.js +++ b/LeetcodeProblems/Maximal_Square.js @@ -15,6 +15,7 @@ Input: Output: 4 */ +const assert = require('assert'); /** * @param {character[][]} matrix @@ -65,11 +66,16 @@ var getCurrentMaxSideLength = function(matrix, i, j) { } var main = function() { - console.log(maximalSquare([["1","0"]])); - console.log(maximalSquare([["1"]])); - console.log(maximalSquare([["0"]])); - console.log( - maximalSquare([["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]) + test(); +} + +function test() { + assert.equal(maximalSquare([["1","0"]]), 1); + assert.equal(maximalSquare([["1"]]), 1); + assert.equal(maximalSquare([["0"]]), 0); + assert.equal( + maximalSquare([["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]), + 4 ); } diff --git a/LeetcodeProblems/Maximun_Subarray.js b/LeetcodeProblems/Maximun_Subarray.js index f90c31d..c159a43 100644 --- a/LeetcodeProblems/Maximun_Subarray.js +++ b/LeetcodeProblems/Maximun_Subarray.js @@ -13,6 +13,7 @@ Explanation: [4,-1,2,1] has the largest sum = 6. Follow up: */ +const assert = require('assert'); var maxSubArray = function(nums) { if(nums.length == 0) @@ -34,10 +35,14 @@ var max = function(i, j) { } var main = function() { - console.log(maxSubArray([])); - console.log(maxSubArray([-4])); - console.log(maxSubArray([2])); - console.log(maxSubArray([4,1,-1,4,5,6,7,-200])); + test(); +} + +function test() { + assert.equal(maxSubArray([]), 0); + assert.equal(maxSubArray([-4]), -4); + assert.equal(maxSubArray([2]), 2); + assert.equal(maxSubArray([4,1,-1,4,5,6,7,-200]), 26); } module.exports.main = main; \ No newline at end of file diff --git a/LeetcodeProblems/Min_Stack.js b/LeetcodeProblems/Min_Stack.js index 6450837..3ee0f78 100644 --- a/LeetcodeProblems/Min_Stack.js +++ b/LeetcodeProblems/Min_Stack.js @@ -19,6 +19,7 @@ minStack.pop(); minStack.top(); --> Returns 0. minStack.getMin(); --> Returns -2. */ +const assert = require('assert'); class MinStack { constructor() { @@ -74,14 +75,18 @@ class MinStack { } var main = function() { + test(); +} + +function test() { var minStack = new MinStack(); minStack.push(-2); minStack.push(0); minStack.push(-3); - console.log(minStack.getMin()); - console.log(minStack.pop()); - console.log(minStack.top()); - console.log(minStack.getMin()); + assert.equal(minStack.getMin(), -3); + assert.equal(minStack.pop(), -3); + assert.equal(minStack.top(), 0); + assert.equal(minStack.getMin(), -2); } module.exports.main = main; \ No newline at end of file diff --git a/LeetcodeProblems/Minimum_Window_Substring.js b/LeetcodeProblems/Minimum_Window_Substring.js index 6cbb735..2769963 100644 --- a/LeetcodeProblems/Minimum_Window_Substring.js +++ b/LeetcodeProblems/Minimum_Window_Substring.js @@ -14,7 +14,7 @@ Note: If there is no such window in S that covers all characters in T, return the empty string "". If there is such window, you are guaranteed that there will always be only one unique minimum window in S. */ - +const assert = require('assert'); var minWindow = function(s, t) { if(t.length === 0 || s.length < t.length) @@ -65,12 +65,16 @@ var getHash = function(t) { } var main = function() { - console.log(minWindow("ADOBECODEBANC", "ABC")); - console.log(minWindow("caaec", "cae")); - console.log(minWindow("bbacbb", "ab")); - console.log(minWindow("abba", "b")); - console.log(minWindow("abba", "a")); - console.log(minWindow("abba", "")); + test(); +} + +function test() { + assert.equal(minWindow("ADOBECODEBANC", "ABC"), "BANC"); + assert.equal(minWindow("caaec", "cae"), "aec"); + assert.equal(minWindow("bbacbb", "ab"), "ba"); + assert.equal(minWindow("abba", "b"), "b"); + assert.equal(minWindow("abba", "a"), "a"); + assert.equal(minWindow("abba", ""), ""); } module.exports.main diff --git a/LeetcodeProblems/NQueens.js b/LeetcodeProblems/NQueens.js index dfd306d..f78d3b8 100644 --- a/LeetcodeProblems/NQueens.js +++ b/LeetcodeProblems/NQueens.js @@ -18,6 +18,7 @@ Output: [ ] Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above. */ +const assert = require('assert'); /** * @param {number} n @@ -75,6 +76,9 @@ var main = function(n) { printMatrixes(solveNQueens(6), 6); } +var test = function() { +} + var printMatrixes = function(matrixes, n) { console.log("Start solution of n: " + n); for(var i = 0; i < matrixes.length; i++) { diff --git a/LeetcodeProblems/Number_of_Islands.js b/LeetcodeProblems/Number_of_Islands.js index 609d91a..248ea49 100644 --- a/LeetcodeProblems/Number_of_Islands.js +++ b/LeetcodeProblems/Number_of_Islands.js @@ -24,6 +24,7 @@ Input: Output: 3 */ +const assert = require('assert'); /* * @param {character[][]} grid @@ -60,16 +61,22 @@ var colorIsland = function(grid, i, j, rowsCount, columnsCount) { colorIsland(grid, i, j + 1, rowsCount, columnsCount); } -var main = function() { - console.log(numIslands([[1]])); - console.log(numIslands([])); - console.log(numIslands( +var main = function() { + test(); +} + +function test() { + assert.equal(numIslands([[1]]), 1); + assert.equal(numIslands([]), 0); + assert.equal(numIslands( [ ["1","1","1","1","0"], ["1","1","0","1","0"], ["1","1","0","0","0"], ["0","0","0","0","0"] - ]) + ], + ), + 1 ); } diff --git a/LeetcodeProblems/merge_k_sorted_lists.js b/LeetcodeProblems/merge_k_sorted_lists.js index 01e244e..c36fa4d 100644 --- a/LeetcodeProblems/merge_k_sorted_lists.js +++ b/LeetcodeProblems/merge_k_sorted_lists.js @@ -14,6 +14,8 @@ Input: ] Output: 1->1->2->3->4->4->5->6 */ +const assert = require('assert'); +const ListNodeTestHelper = require('../utilsClasses/ListNodeTestHelper'); var ListNode = require('../UtilsClasses/ListNode').ListNode; @@ -75,21 +77,28 @@ var mergeLists = function(list1, list2) { } var main = function() { - console.log(mergeKLists([])); - console.log(mergeKLists( - [null] - )); - console.log(mergeKLists( - [null, null] - )); + test(); +} + +function test() { + assert.deepEqual(mergeKLists([]), null); + assert.deepEqual( + mergeKLists([null]), + null + ); + assert.deepEqual( + mergeKLists([null, null]), + null + ); var list1 = ListNode.linkenList([1,2,3]); var list2 = ListNode.linkenList([2,3,4]); var list3 = ListNode.linkenList([5,6]); var list4 = ListNode.linkenList([1,5]); - console.log(mergeKLists( - [list1, list2, list3, list4] - )); + ListNodeTestHelper.assertList( + mergeKLists([list1, list2, list3, list4]), + [1, 1, 2, 2, 3, 3, 4, 5, 5, 6] + ); } module.exports.main = main; \ No newline at end of file From e3bd8dff6021b3878254999978af47ce6d6afcaa Mon Sep 17 00:00:00 2001 From: Ignacio Chiazzo Date: Sun, 30 Aug 2020 20:49:19 -0400 Subject: [PATCH 09/10] Move to tests - Round 6 --- .../Best_Time_To_Buy_And_Sell_Stock_II.js | 9 +++-- LeetcodeProblems/Binary_Gap.js | 9 +++-- LeetcodeProblems/Coin_Change.js | 17 ++++++---- ...ree_from_Preorder_and_Inorder_Traversal.js | 5 +++ LeetcodeProblems/Deletion_Distance.js | 33 +++++++++++-------- LeetcodeProblems/Design_Circular_Deque.js | 24 ++++++++------ LeetcodeProblems/Edit_Distance.js | 17 +++++----- LeetcodeProblems/Escape_The_Ghosts.js | 11 +++++-- LeetcodeProblems/Flood_Fill.js | 12 +++++-- LeetcodeProblems/Group_Anagrams.js | 10 +++++- 10 files changed, 98 insertions(+), 49 deletions(-) diff --git a/LeetcodeProblems/Best_Time_To_Buy_And_Sell_Stock_II.js b/LeetcodeProblems/Best_Time_To_Buy_And_Sell_Stock_II.js index 4e4133e..1179880 100644 --- a/LeetcodeProblems/Best_Time_To_Buy_And_Sell_Stock_II.js +++ b/LeetcodeProblems/Best_Time_To_Buy_And_Sell_Stock_II.js @@ -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 @@ -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; \ No newline at end of file diff --git a/LeetcodeProblems/Binary_Gap.js b/LeetcodeProblems/Binary_Gap.js index 4f548c0..0385125 100644 --- a/LeetcodeProblems/Binary_Gap.js +++ b/LeetcodeProblems/Binary_Gap.js @@ -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 @@ -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; diff --git a/LeetcodeProblems/Coin_Change.js b/LeetcodeProblems/Coin_Change.js index f3e0799..716465c 100644 --- a/LeetcodeProblems/Coin_Change.js +++ b/LeetcodeProblems/Coin_Change.js @@ -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) { @@ -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; \ No newline at end of file diff --git a/LeetcodeProblems/Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal.js b/LeetcodeProblems/Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal.js index a9d3f18..da702cc 100644 --- a/LeetcodeProblems/Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal.js +++ b/LeetcodeProblems/Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal.js @@ -27,6 +27,7 @@ Return the following binary tree: * this.left = this.right = null; * } */ +const assert = require('assert'); var TreeNode = require('../UtilsClasses/TreeNode').TreeNode; @@ -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])); } diff --git a/LeetcodeProblems/Deletion_Distance.js b/LeetcodeProblems/Deletion_Distance.js index 7d2001d..2714fb3 100644 --- a/LeetcodeProblems/Deletion_Distance.js +++ b/LeetcodeProblems/Deletion_Distance.js @@ -18,6 +18,7 @@ output: 9 input: str1 = "", str2 = "" output: 0 */ +const assert = require('assert'); // Solution 3 Using DP var deletionDistanceDP = function(str1, str2) { @@ -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 diff --git a/LeetcodeProblems/Design_Circular_Deque.js b/LeetcodeProblems/Design_Circular_Deque.js index d6d95ac..60dace9 100644 --- a/LeetcodeProblems/Design_Circular_Deque.js +++ b/LeetcodeProblems/Design_Circular_Deque.js @@ -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. @@ -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; diff --git a/LeetcodeProblems/Edit_Distance.js b/LeetcodeProblems/Edit_Distance.js index 121ae52..c337677 100644 --- a/LeetcodeProblems/Edit_Distance.js +++ b/LeetcodeProblems/Edit_Distance.js @@ -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) { @@ -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; diff --git a/LeetcodeProblems/Escape_The_Ghosts.js b/LeetcodeProblems/Escape_The_Ghosts.js index 3acee7f..3e8ee10 100644 --- a/LeetcodeProblems/Escape_The_Ghosts.js +++ b/LeetcodeProblems/Escape_The_Ghosts.js @@ -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 @@ -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 \ No newline at end of file diff --git a/LeetcodeProblems/Flood_Fill.js b/LeetcodeProblems/Flood_Fill.js index 5a1b9da..14ed4e3 100644 --- a/LeetcodeProblems/Flood_Fill.js +++ b/LeetcodeProblems/Flood_Fill.js @@ -28,6 +28,7 @@ The length of image and image[0] will be in the range [1, 50]. The given starting pixel will satisfy 0 <= sr < image.length and 0 <= sc < image[0].length. The value of each color in image[i][j] and newColor will be an integer in [0, 65535]. */ +const assert = require('assert'); var floodFill = function(image, sr, sc, newColor) { var oldColor = image[sr][sc]; @@ -53,7 +54,14 @@ var floodFill = function(image, sr, sc, newColor) { }; function main() { - console.log(floodFill([[1,1,1],[1,1,0],[1,0,1]], 1, 1, 2)) + test(); } -module.exports.main = main; \ No newline at end of file +function test() { + assert.deepEqual( + [ [ 2, 2, 2 ], [ 2, 2, 0 ], [ 2, 0, 1 ] ], + floodFill([[1,1,1],[1,1,0],[1,0,1]], 1, 1, 2) + ); +} + +module.exports.main = main; diff --git a/LeetcodeProblems/Group_Anagrams.js b/LeetcodeProblems/Group_Anagrams.js index 3376d77..230af77 100644 --- a/LeetcodeProblems/Group_Anagrams.js +++ b/LeetcodeProblems/Group_Anagrams.js @@ -18,6 +18,7 @@ Note: All inputs will be in lowercase. The order of your output does not matter. */ +const assert = require('assert'); var groupAnagrams = function(strs) { var ret = []; @@ -48,7 +49,14 @@ var sortString = function(str) { } var main = function() { - console.log(groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"])); + test(); +} + +function test() { + assert.deepEqual( + groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"]), + [ [ 'eat', 'tea', 'ate' ], [ 'tan', 'nat' ], [ 'bat' ] ] + ) } module.exports.main = main; From bb185008af428ccfad0240ec38b59c13d0aebd7c Mon Sep 17 00:00:00 2001 From: Ignacio Chiazzo Date: Sun, 30 Aug 2020 20:58:58 -0400 Subject: [PATCH 10/10] Move to tests - Round 7 --- LeetcodeProblems/3Sum.js | 28 +++++++++++++++----- LeetcodeProblems/Add_Two_Numbers.js | 24 +++++++++++++---- LeetcodeProblems/Award_Budget_Cuts.js | 15 +++++++++-- LeetcodeProblems/Backspace_String_Compare.js | 23 +++++++++------- 4 files changed, 67 insertions(+), 23 deletions(-) diff --git a/LeetcodeProblems/3Sum.js b/LeetcodeProblems/3Sum.js index c58bb64..e5aee95 100644 --- a/LeetcodeProblems/3Sum.js +++ b/LeetcodeProblems/3Sum.js @@ -18,6 +18,7 @@ A solution set is: [-1, -1, 2] ] */ +const assert = require('assert'); /** * @param {number[]} nums @@ -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; \ No newline at end of file +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; diff --git a/LeetcodeProblems/Add_Two_Numbers.js b/LeetcodeProblems/Add_Two_Numbers.js index c8db229..53a00c1 100644 --- a/LeetcodeProblems/Add_Two_Numbers.js +++ b/LeetcodeProblems/Add_Two_Numbers.js @@ -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; /** @@ -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; \ No newline at end of file +module.exports.main = main; diff --git a/LeetcodeProblems/Award_Budget_Cuts.js b/LeetcodeProblems/Award_Budget_Cuts.js index d26e25b..1a8ad15 100644 --- a/LeetcodeProblems/Award_Budget_Cuts.js +++ b/LeetcodeProblems/Award_Budget_Cuts.js @@ -29,6 +29,7 @@ Constraints: [output] double */ +const assert = require('assert'); var cutAwardBadges = function(nums, newBadge) { var currentBadge = 0; @@ -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; diff --git a/LeetcodeProblems/Backspace_String_Compare.js b/LeetcodeProblems/Backspace_String_Compare.js index db46f7e..67c4126 100644 --- a/LeetcodeProblems/Backspace_String_Compare.js +++ b/LeetcodeProblems/Backspace_String_Compare.js @@ -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 @@ -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; \ No newline at end of file +module.exports.main = main;