Skip to content

Commit e400414

Browse files
Move tests to its own files Round 2
1 parent 4a82a5b commit e400414

Some content is hidden

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

45 files changed

+68
-2871
lines changed

LeetcodeProblemsTests/Edit_Distance_Test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ function test() {
99
assert.equal(minDistance2("ros", "horse"), 3);
1010
assert.equal(minDistance2("intention", "execution"), 5);
1111
}
12-
test();
12+
1313
module.exports.test = test;
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,11 @@
1-
/*
2-
Escape The Ghosts
3-
https://leetcode.com/problems/escape-the-ghosts/description/
41

5-
You are playing a simplified Pacman game. You start at the point (0, 0), and your destination is (target[0], target[1]). There are several ghosts on the map, the i-th ghost starts at (ghosts[i][0], ghosts[i][1]).
6-
7-
Each turn, you and all ghosts simultaneously *may* move in one of 4 cardinal directions: north, east, west, or south, going from the previous point to a new point 1 unit of distance away.
8-
9-
You escape if and only if you can reach the target before any ghost reaches you (for any given moves the ghosts may take.) If you reach any square (including the target) at the same time as a ghost, it doesn't count as an escape.
10-
11-
Return True if and only if it is possible to escape.
12-
13-
Example 1:
14-
Input:
15-
ghosts = [[1, 0], [0, 3]]
16-
target = [0, 1]
17-
Output: true
18-
Explanation:
19-
You can directly reach the destination (0, 1) at time 1, while the ghosts located at (1, 0) or (0, 3) have no way to catch up with you.
20-
Example 2:
21-
Input:
22-
ghosts = [[1, 0]]
23-
target = [2, 0]
24-
Output: false
25-
Explanation:
26-
You need to reach the destination (2, 0), but the ghost at (1, 0) lies between you and the destination.
27-
Example 3:
28-
Input:
29-
ghosts = [[2, 0]]
30-
target = [1, 0]
31-
Output: false
32-
Explanation:
33-
The ghost can reach the target at the same time as you.
34-
Note:
35-
36-
All points have coordinates with absolute value <= 10000.
37-
The number of ghosts will not exceed 100.
38-
*/
392
const assert = require('assert');
40-
41-
/**
42-
* @param {number[][]} ghosts
43-
* @param {number[]} target
44-
* @return {boolean}
45-
*/
46-
var escapeGhosts = function(ghosts, target) {
47-
var distancePacman = getDistance([0,0], target);
48-
for(ghost in ghosts) {
49-
const distanceGhost = getDistance(ghosts[ghost], target);
50-
if(distancePacman > distanceGhost)
51-
return false
52-
}
53-
54-
return true;
55-
};
56-
57-
var getDistance = function(a, b) {
58-
const horizontalMoves = Math.abs(a[0] - b[0]);
59-
const verticalMoves = Math.abs(a[1] - b[1]);
60-
return horizontalMoves + verticalMoves;
61-
}
62-
63-
var main = function() {
64-
test();
65-
}
3+
var escapeGhosts = require('../LeetcodeProblems/Escape_The_Ghosts').escapeGhosts;
664

675
function test() {
686
assert.equal(escapeGhosts([[1, 0], [0, 3]], [0, 1]), true);
697
assert.equal(escapeGhosts([[1, 0]], [2, 0]), false);
708
assert.equal(escapeGhosts([[2, 0]], [1, 0]), true);
719
}
7210

73-
module.exports.main = main
11+
module.exports.test = test;
+1-58
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,10 @@
1-
/*
2-
Flood Fill
3-
https://leetcode.com/problems/flood-fill/description/
4-
5-
An image is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535).
6-
7-
Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image.
8-
9-
To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel,
10-
plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on.
11-
Replace the color of all of the aforementioned pixels with the newColor.
12-
13-
At the end, return the modified image.
14-
15-
Example 1:
16-
Input:
17-
image = [[1,1,1],[1,1,0],[1,0,1]]
18-
sr = 1, sc = 1, newColor = 2
19-
Output: [[2,2,2],[2,2,0],[2,0,1]]
20-
Explanation:
21-
From the center of the image (with position (sr, sc) = (1, 1)), all pixels connected
22-
by a path of the same color as the starting pixel are colored with the new color.
23-
Note the bottom corner is not colored 2, because it is not 4-directionally connected
24-
to the starting pixel.
25-
Note:
26-
27-
The length of image and image[0] will be in the range [1, 50].
28-
The given starting pixel will satisfy 0 <= sr < image.length and 0 <= sc < image[0].length.
29-
The value of each color in image[i][j] and newColor will be an integer in [0, 65535].
30-
*/
311
const assert = require('assert');
322

33-
var floodFill = function(image, sr, sc, newColor) {
34-
var oldColor = image[sr][sc];
35-
36-
if(newColor == oldColor)
37-
return image;
38-
39-
image[sr][sc] = newColor;
40-
41-
if(sr > 0 && image[sr - 1][sc] == oldColor)
42-
floodFill(image, sr - 1, sc, newColor); //Left
43-
44-
if(sc > 0 && image[sr][sc - 1] == oldColor)
45-
floodFill(image, sr, sc - 1, newColor); //Up
46-
47-
if(sr < image.length - 1 && image[sr + 1][sc] == oldColor)
48-
floodFill(image, sr + 1, sc, newColor); //Down
49-
50-
if(sc < image[0].length - 1 && image[sr][sc + 1] == oldColor)
51-
floodFill(image, sr, sc + 1, newColor); // Right
52-
53-
return image;
54-
};
55-
56-
function main() {
57-
test();
58-
}
59-
603
function test() {
614
assert.deepEqual(
625
[ [ 2, 2, 2 ], [ 2, 2, 0 ], [ 2, 0, 1 ] ],
636
floodFill([[1,1,1],[1,1,0],[1,0,1]], 1, 1, 2)
647
);
658
}
669

67-
module.exports.main = main;
10+
module.exports.test = test;
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,5 @@
1-
/*
2-
Generate Parentheses
3-
https://leetcode.com/problems/generate-parentheses
4-
5-
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
6-
7-
For example, given n = 3, a solution set is:
8-
9-
[
10-
"((()))",
11-
"(()())",
12-
"(())()",
13-
"()(())",
14-
"()()()"
15-
]
16-
*/
17-
18-
// ************************************************ Approach1 ************************************************
19-
var generateParenthesisApproach1 = function(n) {
20-
if(n === 0)
21-
return [];
22-
23-
var str = "(".repeat(n);
24-
sol = [];
25-
26-
genParAux(str, 0, 0, sol)
27-
return sol;
28-
};
29-
30-
var genParAux = function(str, position, leftParentheses, sol) {
31-
if(position === str.length) {
32-
var ret = str + ")".repeat(leftParentheses);
33-
sol.push(ret);
34-
return;
35-
}
36-
37-
genParAux(str, position + 1, leftParentheses + 1, sol); // Don't insert anything
38-
if(leftParentheses === 0)
39-
return;
40-
41-
for(var i = 1; i <= leftParentheses; i++) {
42-
var parString = ")".repeat(i);
43-
var partSol = str.slice(0, position) + parString + str.slice(position); // Insert i parentheses in the position
44-
genParAux(partSol, position + i + 1, leftParentheses - i + 1, sol);
45-
}
46-
}
47-
48-
// ************************************************ Approach2 ************************************************
49-
var generateParenthesisApproach2 = function(n) {
50-
if(n === 0)
51-
return [];
52-
53-
var sol = [];
54-
genParAuxApproach2("", 0, 0, 0, n * 2, sol)
55-
return sol;
56-
}
57-
58-
var genParAuxApproach2 = function(str, leftPar, rightPar, index, totalCharCount, sol) {
59-
if(index === totalCharCount) {
60-
if(rightPar === leftPar)
61-
sol.push(str);
62-
63-
return;
64-
}
65-
66-
var strLeft = insertAt(str, index, "(");
67-
genParAuxApproach2(strLeft, leftPar + 1, rightPar, index + 1, totalCharCount, sol);
68-
69-
if(rightPar === leftPar)
70-
return;
71-
72-
var strRight = insertAt(str, index, ")");
73-
genParAuxApproach2(strRight, leftPar, rightPar + 1, index + 1, totalCharCount, sol);
74-
}
75-
76-
var insertAt = function(str, position, value) {
77-
return str.slice(0, position) + value + str.slice(position);
78-
}
79-
80-
function main() {
81-
console.log("Approach 1");
82-
[0, 1, 2, 3].forEach(function(elem) {
83-
console.log(`${elem}: ${generateParenthesisApproach2(elem)}`);
84-
})
85-
86-
console.log("-------------");
87-
88-
console.log("Approach 2");
89-
[0, 1, 2, 3].forEach(function(elem) {
90-
console.log(`${elem}: ${generateParenthesisApproach2(elem)}`);
91-
})
1+
var test = function() {
2+
// TODO
923
}
934

94-
module.exports.main = main
5+
module.exports.test = test;
+1-53
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,10 @@
1-
/*
2-
Group Anagrams
3-
https://leetcode.com/problems/group-anagrams/description/
4-
5-
Given an array of strings, group anagrams together.
6-
7-
Example:
8-
9-
Input: ["eat", "tea", "tan", "ate", "nat", "bat"],
10-
Output:
11-
[
12-
["ate","eat","tea"],
13-
["nat","tan"],
14-
["bat"]
15-
]
16-
Note:
17-
18-
All inputs will be in lowercase.
19-
The order of your output does not matter.
20-
*/
211
const assert = require('assert');
222

23-
var groupAnagrams = function(strs) {
24-
var ret = [];
25-
var hashMap = {};
26-
for(var i = 0; i < strs.length; i++) {
27-
const elem = strs[i];
28-
const elemSorted = sortString(strs[i]);
29-
30-
if(hashMap[elemSorted]) {
31-
hashMap[elemSorted].push(elem);
32-
} else {
33-
hashMap[elemSorted] = [elem];
34-
}
35-
}
36-
37-
for(key in hashMap)
38-
ret.push(hashMap[key]);
39-
40-
41-
return ret;
42-
};
43-
44-
var sortString = function(str) {
45-
if(str.length === 0)
46-
return str;
47-
48-
return str.split("").sort().join("");
49-
}
50-
51-
var main = function() {
52-
test();
53-
}
54-
553
function test() {
564
assert.deepEqual(
575
groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"]),
586
[ [ 'eat', 'tea', 'ate' ], [ 'tan', 'nat' ], [ 'bat' ] ]
597
)
608
}
619

62-
module.exports.main = main;
10+
module.exports.test = test;

0 commit comments

Comments
 (0)