Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
23
Task/Anagrams/JavaScript/anagrams-1.js
Normal file
23
Task/Anagrams/JavaScript/anagrams-1.js
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
var fs = require('fs');
|
||||
var words = fs.readFileSync('unixdict.txt', 'UTF-8').split('\n');
|
||||
|
||||
var i, item, max = 0,
|
||||
anagrams = {};
|
||||
|
||||
for (i = 0; i < words.length; i += 1) {
|
||||
var key = words[i].split('').sort().join('');
|
||||
if (!anagrams.hasOwnProperty(key)) {//check if property exists on current obj only
|
||||
anagrams[key] = [];
|
||||
}
|
||||
var count = anagrams[key].push(words[i]); //push returns new array length
|
||||
max = Math.max(count, max);
|
||||
}
|
||||
|
||||
//note, this returns all arrays that match the maximum length
|
||||
for (item in anagrams) {
|
||||
if (anagrams.hasOwnProperty(item)) {//check if property exists on current obj only
|
||||
if (anagrams[item].length === max) {
|
||||
console.log(anagrams[item].join(' '));
|
||||
}
|
||||
}
|
||||
}
|
||||
22
Task/Anagrams/JavaScript/anagrams-2.js
Normal file
22
Task/Anagrams/JavaScript/anagrams-2.js
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
var fs = require('fs');
|
||||
var dictionary = fs.readFileSync('unixdict.txt', 'UTF-8').split('\n');
|
||||
|
||||
//group anagrams
|
||||
var sortedDict = dictionary.reduce(function (acc, word) {
|
||||
var sortedLetters = word.split('').sort().join('');
|
||||
if (acc[sortedLetters] === undefined) { acc[sortedLetters] = []; }
|
||||
acc[sortedLetters].push(word);
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
//sort list by frequency
|
||||
var keysSortedByFrequency = Object.keys(sortedDict).sort(function (keyA, keyB) {
|
||||
if (sortedDict[keyA].length < sortedDict[keyB].length) { return 1; }
|
||||
if (sortedDict[keyA].length > sortedDict[keyB].length) { return -1; }
|
||||
return 0;
|
||||
});
|
||||
|
||||
//print first 10 anagrams by frequency
|
||||
keysSortedByFrequency.slice(0, 10).forEach(function (key) {
|
||||
console.log(sortedDict[key].join(' '));
|
||||
});
|
||||
|
|
@ -1,20 +0,0 @@
|
|||
var fs = require('fs');
|
||||
|
||||
var anas = {};
|
||||
var words = fs.readFileSync('unixdict.txt', 'UTF-8').split('\n');
|
||||
var max = 0;
|
||||
|
||||
for (var w in words) {
|
||||
var key = words[w].split('').sort().join('');
|
||||
if (!(key in anas)) {
|
||||
anas[key] = [];
|
||||
}
|
||||
var count = anas[key].push(words[w]);
|
||||
max = Math.max(count, max);
|
||||
}
|
||||
|
||||
for (var a in anas) {
|
||||
if (anas[a].length === max) {
|
||||
console.log(anas[a]);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue