Frequency Number 題目要求
我想要一個 nodejs 小程序,
它可以幫我處理一段字符串信息,這段字符串信息是由英文單詞組成,每兩個單詞之間有空格,處理結(jié)果也為一段字符串,這個字符串應該每行只顯示一個單詞和它的數(shù)量,并且按出現(xiàn)頻率倒序排列。
Tasking 分解圖

題目分解
這道題筆者計劃采用 TTD 模式進行開發(fā)完成,即先寫測試用例,再寫實現(xiàn)代碼。
整體主要按 tasking 分解圖所示,對三個模塊分別進行測試:
** 1. 將輸入的字符串分解為單獨的單詞 **(split)
** 2. 計算每個單詞出現(xiàn)的次數(shù) **(count)
** 3. 根據(jù)每個單詞出現(xiàn)的次數(shù)對其進行排序 **(sort)
測試用例如下:
| input(string) | split | count | sort |
|:------|:-----|:------|:-----|:-----|
|""|[]|null|null|
|" "|[]|null|null|
|"hi"|["hi"]|"hi 1\r\n"|"hi 1\r\n"|
|"hi a"|["hi","a"]|"hi 1\r\na 1\r\n"|"hi 1\r\na 1\r\n"|
|"hi a a"|["hi","a","a"]|"hi 21r\na 2\r\n"|"a 2\r\nhi 1\r\n"|
|"hi a hi one one two"|["hi","a","hi","one","one","two"]|"hi 2\r\a 1\r\none 2\r\n two 1\r\n"|"hi 2\r\none 2\r\na 1\r\n two 1\r\n"|
** 編寫思路 **:
依次編寫三個模塊的代碼,當一個模塊的所有測試通過了,再編寫下一個模塊的測試和實現(xiàn)代碼。
測試及實現(xiàn)代碼
** split 模塊測試代碼 **
var mainTest=require("./FrequencyNumberTest.js");
describe('splitString test',function(){
it('split test1',function(){
expect(mainTest("")).toEqual([]);
});
it('split test2',function(){
expect(mainTest(" ")).toEqual([]);
});
it('split test3',function(){
expect(mainTest("hi")).toEqual(["hi"]);
});
it('split test4',function(){
expect(mainTest("hi a")).toEqual(["hi","a"]);
});
it('split test5',function(){
expect(mainTest("hi a a")).toEqual(["hi","a","a"]);
});
it('split test6',function(){
expect(mainTest("hi a hi one one two")).toEqual(["hi","a","hi","one","one","two"]);
});
});
** split 模塊實現(xiàn)代碼 **
function splitString(string){
if(string === "" || string === " ")
return [];
return string.split(" ");
}
function main(string){
return splitString(string);
}
module.exports=main;
** count 模塊測試代碼 **
var mainTest=require("./FrequencyNumberTest.js");
describe('countWords test',function(){
it('count test1',function(){
expect(mainTest("")).toEqual(null);
});
it('count test2',function(){
expect(mainTest(" ")).toEqual(null);
});
it('count test3',function(){
expect(mainTest("hi")).toEqual("hi 1\r\n");
});
it('count test4',function(){
expect(mainTest("hi a")).toEqual("hi 1\r\na 1\r\n");
});
it('count test5',function(){
expect(mainTest("hi a a")).toEqual("hi 1\r\na 2\r\n");
});
it('count test6',function(){
expect(mainTest("hi a hi one one two")).toEqual("hi 2\r\na 1\r\none 2\r\ntwo 1\r\n");
});
});
** count 模塊實現(xiàn)代碼 **
function count(stringArry){
var elemArry = new Array();
var countArry = new Array();
for(var i=0;i<stringArry.length;i++){
if(i === 0){
elemArry[i]=stringArry[i];
countArry[i]=1;continue;
}
var j=0;
for(;j<stringArry.length;j++){
if(stringArry[i] === elemArry[j]){
countArry[j]++;break;
}
}
if(j === stringArry.length){
elemArry[elemArry.length] = stringArry[i];
countArry[countArry.length] = 1;
}
}
return getResult(elemArry,countArry);
}
** sort 模塊測試代碼 **
var mainTest=require("./FrequencyNumberTest.js");
describe('countWords test',function(){
it('count test1',function(){
expect(mainTest("")).toEqual(null);
});
it('count test2',function(){
expect(mainTest(" ")).toEqual(null);
});
it('count test3',function(){
expect(mainTest("hi")).toEqual("hi 1\r\n");
});
it('count test4',function(){
expect(mainTest("hi a")).toEqual("hi 1\r\na 1\r\n");
});
it('count test5',function(){
expect(mainTest("hi a a")).toEqual("a 2\r\nhi 1\r\n");
});
it('count test6',function(){
expect(mainTest("hi a hi one one two")).toEqual("hi 2\r\none 2\r\na 1\r\ntwo 1\r\n");
});
});
** sort 模塊實現(xiàn)代碼 **
function sort(elemArry,countArry){
for(var i=0;i<elemArry.length;i++){
for(var j=i+1;j<elemArry.length;j++){
if(countArry[i] < countArry[j]){
var n=countArry[i];
countArry[i]=countArry[j];
countArry[j]=n;
var s=elemArry[i];
elemArry[i]=elemArry[j];
elemArry[j]=s;
}
}
}
return getResult(elemArry,countArry);
}
Git 提交記錄
