From ddb733f2cf06be3e1e2d882c0e0a409e70cc107f Mon Sep 17 00:00:00 2001 From: Richard Kojedzinszky Date: Tue, 14 Apr 2015 15:15:57 +0200 Subject: [PATCH] uwc: js implementation --- uwc.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 uwc.js diff --git a/uwc.js b/uwc.js new file mode 100644 index 0000000..bc09721 --- /dev/null +++ b/uwc.js @@ -0,0 +1,43 @@ + +process.stdin.setEncoding('utf8'); +var stream = process.stdin.pipe(require('split')()); +var hash = { }; +var pattern = new RegExp("[a-z']+", "gi"); + +stream.on('data', function(line) { + var matches = ('' + line).match(pattern); + if (!matches) + return; + + for (var i = 0; i < matches.length; ++i) { + var w = matches[i]; + if (hash.hasOwnProperty(w)) { + hash[w]++; + } else { + hash[w] = 1; + } + } + }); + +stream.on('end', function() { + var words = []; + for (w in hash) { + words.push(w); + } + words.sort(function(a, b) { + var d = hash[a] - hash[b]; + if (d != 0) + return d; + + if (a < b) { + return -1; + } else { + return 1; + } + }); + + for (var i = 0; i < words.length; ++i) { + var w = words[i]; + process.stdout.write(w + " " + hash[w] + "\n"); + } +}); -- 2.1.4