TTD-Practise-FrequencyNumber
Frequency Number 题目要求
我想要一个 nodejs 小程序,
它可以帮我处理一段字符串信息,这段字符串信息是由英文单词组成,每两个单词之间有空格,处理结果也为一段字符串,这个字符串应该每行只显示一个单词和它的数量,并且按出现频率倒序排列。
Tasking 分解图
Tasking题目分解
这道题笔者计划采用 TTD 模式进行开发完成,即先写测试用例,再写实现代码。
整体主要按 tasking 分解图所示,对三个模块分别进行测试:
** 1. 将输入的字符串分解为单独的单词 **(split)
** 2. 计算每个单词出现的次数 **(count)
** 3. 根据每个单词出现的次数对其进行排序 **(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"|
** 编写思路 **:
依次编写三个模块的代码,当一个模块的所有测试通过了,再编写下一个模块的测试和实现代码。
测试及实现代码
** 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 模块实现代码 **
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 模块实现代码 **
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 模块实现代码 **
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);
}