-
Notifications
You must be signed in to change notification settings - Fork 838
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #282 from hotoo/alecgibson-group
Alecgibson group
- Loading branch information
Showing
6 changed files
with
167 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
"use strict"; | ||
|
||
/** | ||
* 组合 2 个拼音数组。 | ||
* @param {Array<String>} a1 第一个数组,形如 ["zhāo", "cháo"] | ||
* @param {Array<String>} a2 字符串型数组。形如 ["yáng"] | ||
* @return {Array<String>} 组合后的一维数组,如上可得 ["zhāoyáng", "cháoyáng"] | ||
*/ | ||
function combo2array(a1, a2) { | ||
const result = []; | ||
if (!a1.length) { | ||
return a2; | ||
} | ||
if (!a2.length) { | ||
return a1; | ||
} | ||
for (let i = 0, l = a1.length; i < l; i++) { | ||
for (let j = 0, m = a2.length; j < m; j++) { | ||
result.push(a1[i] + a2[j]); | ||
} | ||
} | ||
return result; | ||
// arr.forEach(arrItem => { | ||
// resultItem = ''; | ||
// arrItem.forEach(item => { | ||
// resultItem += item; | ||
// }); | ||
// result.push(resultItem); | ||
// }); | ||
} | ||
|
||
/** | ||
* 合并二维元祖。 | ||
* @param {Array<Array<String>>} arr 二维元祖 [["zhāo", "cháo"], ["yáng"], ["dōng"], ["shēng"]] | ||
* @return {Array<String>} 返回二维字符串组合数组。形如 | ||
* [ | ||
* ["zhāoyáng"], ["dōng"], ["shēng"], | ||
* ["cháoyáng"], ["dōng"], ["shēng"] | ||
* ] | ||
*/ | ||
function combo(arr) { | ||
if (arr.length === 0) { | ||
return []; | ||
} | ||
if (arr.length === 1) { | ||
return arr[0]; | ||
} | ||
let result = combo2array(arr[0], arr[1]); | ||
for (let i = 2, l = arr.length; i < l; i++) { | ||
result = combo2array(result, arr[i]); | ||
} | ||
return result; | ||
} | ||
|
||
exports.combo2array = combo2array; | ||
exports.combo = combo; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
"use strict"; | ||
|
||
const expect = require("expect.js"); | ||
const util = require("../lib/util"); | ||
|
||
describe("test/util.test.js", function() { | ||
describe("combo2array", function() { | ||
it("combo2array([], [])", function() { | ||
expect(util.combo2array([], [])).to.eql([]); | ||
}); | ||
|
||
it("combo2array([a], [])", function() { | ||
expect(util.combo2array(["a"], [])).to.eql(["a"]); | ||
}); | ||
|
||
it("combo2array([], [1])", function() { | ||
expect(util.combo2array([], ["1"])).to.eql(["1"]); | ||
}); | ||
|
||
it("combo2array([a], [1])", function() { | ||
expect(util.combo2array(["a"], ["1"])).to.eql(["a1"]); | ||
}); | ||
|
||
it("combo2array([a,b], [1])", function() { | ||
expect(util.combo2array(["a", "b"], ["1"])).to.eql(["a1", "b1"]); | ||
}); | ||
|
||
it("combo2array([a], [1,2])", function() { | ||
expect(util.combo2array(["a"], ["1", "2"])).to.eql(["a1", "a2"]); | ||
}); | ||
|
||
it("combo2array([a,b], [1,2])", function() { | ||
expect(util.combo2array(["a", "b"], ["1", "2"])).to.eql(["a1", "a2", "b1", "b2"]); | ||
}); | ||
|
||
it("combo2array([a,b,c], [1,2,3])", function() { | ||
expect(util.combo2array(["a", "b", "c"], ["1", "2", "3"])).to.eql(["a1", "a2", "a3", "b1", "b2", "b3", "c1", "c2", "c3"]); | ||
}); | ||
}); | ||
|
||
describe("combo", function() { | ||
it("combo([])", function() { | ||
expect(util.combo([])).to.eql([]); | ||
}); | ||
|
||
it("combo([[a]])", function() { | ||
expect(util.combo([["a"]])).to.eql([["a"]]); | ||
}); | ||
|
||
it("combo([[a,b]])", function() { | ||
expect(util.combo([["a", "b"]])).to.eql([["a", "b"]]); | ||
}); | ||
|
||
it("combo([[a,b],[1]])", function() { | ||
expect(util.combo([["a", "b"], ["1"]])).to.eql([["a1", "b1"]]); | ||
}); | ||
|
||
it("combo([[a,b],[1,2]])", function() { | ||
expect(util.combo([["a", "b"], ["1", "2"]])).to.eql([["a1", "a2", "b1", "b2"]]); | ||
}); | ||
|
||
it("combo([[a,b],[1,2],[A]])", function() { | ||
expect(util.combo([["a", "b"], ["1", "2"], ["A"]])).to.eql([["a1A", "a2A", "b1A", "b2A"]]); | ||
}); | ||
|
||
it("combo([[a,b],[1,2],[A,B]])", function() { | ||
expect(util.combo([["a", "b"], ["1", "2"], ["A", "B"]])).to.eql([["a1A", "a1B", "a2A", "a2B", "b1A", "b1B", "b2A", "b2B"]]); | ||
}); | ||
}); | ||
}); |