TTD-Practise-FrequencyNumber

Frequency Number 題目要求

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


Tasking 分解圖

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 提交記錄

GitLog
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • 首頁 資訊 文章 資源 小組 相親 登錄 注冊 首頁 最新文章 IT 職場 前端 后端 移動端 數(shù)據(jù)庫 運維 其他...
    Helen_Cat閱讀 4,153評論 1 10
  • 第5章 引用類型(返回首頁) 本章內(nèi)容 使用對象 創(chuàng)建并操作數(shù)組 理解基本的JavaScript類型 使用基本類型...
    大學一百閱讀 3,679評論 0 4
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,568評論 19 139
  • 前段時間的推文《你開2000一個月,就會有工作?》貌似有挺多不同的聲音。 包括在前程無憂轉(zhuǎn)載了文章后,也看到了有人...
    撕兄閱讀 1,766評論 3 11
  • 在儀器的叢林里穿梭 從上到下 從里到外 從皮膚到骨骼 從血液到臟腑 每一寸每一分 都可以看個透徹 而唯獨我們的靈魂...
    漫天秋云閱讀 218評論 2 0

友情鏈接更多精彩內(nèi)容