TTD-基礎(chǔ)編程練習(xí)-FrequencyNumber

練習(xí)題目需求:

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

Example

input:

“it was the age of wisdom it was the age of foolishness it is”

output

it 3
was 2
the 2
age 2
of 2
wisdom 1
foolishness 1
is 1

Tasking 流程圖

流程圖

代碼:

main.js

var format = function (word,count){
return word +" "+ count;
};
var group = function(wordArray){
return wordArray.reduce((array,word) => {
let entry = array.find((e) =>e.word ===word);
if(entry){
entry.count++;
}else{
array.push({word:word,count:1});
}
return array;
},[]);
};
var split = function(words){
return words.split(/\s+/);
};
var sort = function(groupedWords){
groupedWords.sort((s,y)=>y.count - x.count);
};
function main(words){
if(words!== ''){
let wordArray = words.split(/\s+/);
let groupedWords = group(wordArray);
groupedWords.sort((x,y) =>y.count -x.count);
return groupedWords.map((e) => format(e.word,e.count)).join('\r\n');
}
return '';
}
module.exports = main;

Frequency NumberSpec.js

var main = require("./main.js");
describe("Word Frequency",function(){
it("returns empty string given empty string",function(){
var result = main('');

    expect(result).toEqual('');
});
it("returns string given one word",function(){
    var result = main('he');
    expect(result).toEqual('he 1');
});
 it("returns string given two different words",function(){
    var result = main('he is');
    expect(result).toEqual('he 1\r\nis 1');
});
it("returns string given duplicated words",function(){
    var result = main('he is he');
    expect(result).toEqual('he 2\r\nis 1');
});
it("returns string given duplicated words need to be sorted",function(){
    var result = main('he is is');
    expect(result).toEqual('is 2\r\nhe 1');
});
it("returns string given words splited by multiple spaces",function(){
    var result = main('he       is');
    expect(result).toEqual('he 1\r\nis 1');
});
it("returns string given words splited by multiple spaces",function(){
    var result = main('it was the age of wisdom it was the age of foolishness it is');
    expect(result).toEqual('it 3\r\nwas 2\r\nthe 2\r\nage 2\r\nof 2\r\nwisdom 1\r\nfoolishness 1\r\nis 1');
});

});

GIT 截圖

1.png

jasmint截圖

2.png

個人總結(jié)

有難度,第一是安裝git和nodejs,熟悉git和npm的各種操作指令,再有就是JS的語法,完成題目功能的邏輯反倒是比較好想,先傳入字符串,開始按照空格分割,然后計數(shù),排序再輸出,意外經(jīng)朋友提醒看到了教練在B站直播的視頻,其中就有關(guān)于這個題目的講解,認(rèn)真看了,再寫代碼感覺輕松多了,關(guān)于TDD模式的編程也理解得更深深刻,還需要繼續(xù)努力。

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

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

  • 一、Frequency Number處理前準(zhǔn)備 Git準(zhǔn)備通過連接https://git-for-windows....
    WishToWeb閱讀 417評論 0 2
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,086評論 0 23
  • 作業(yè)要求 它可以幫我處理一段字符串信息,這段字符串信息是由英文單詞組成,每兩個單詞之間有空格,處理結(jié)果也為一段字符...
    TW_1024_趙梓君閱讀 535評論 2 3
  • 為什么使用Git: 能夠?qū)ξ募姹究刂坪投嗳藚f(xié)作開發(fā)擁有強(qiáng)大的分支特性,所以能夠靈活地以不同的工作流協(xié)同開發(fā)分布式...
    TW_TOB_陳丹陽閱讀 326評論 0 0
  • 1. 有一天我問南瓜:“快畢業(yè)了,你究竟是怎么想的。” 我的這個問題似乎把南瓜問住了,她思考了很久,定了定神回復(fù)我...
    拂逆之間閱讀 784評論 3 1

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