-
Notifications
You must be signed in to change notification settings - Fork 16
/
message.js
56 lines (44 loc) · 1.28 KB
/
message.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const _getSortedLetterFrequencies = codes => {
let cols = Array(codes[0].length)
.fill()
.map(c => {
return {};
});
codes.forEach(code => {
let letters = code.split('');
letters.forEach((c, i) => {
if (!cols[i][c]) {
cols[i][c] = 0;
}
cols[i][c] += 1;
});
});
let sorted_entires = cols.map(col => {
let col_array = Object.entries(col);
col_array.sort((a, b) => {
if (a[1] < b[1]) return -1;
else if (a[1] > b[1]) return 1;
else return 0;
});
return col_array;
});
return sorted_entires;
};
const getMostCommonLetterInEachColumn = codes => {
let sorted_entries = _getSortedLetterFrequencies(codes);
let most_common_letters = sorted_entries.map(list => {
return list.pop()[0];
});
return most_common_letters.join('');
};
const getLeastCommonLetterInEachColumn = codes => {
let sorted_entries = _getSortedLetterFrequencies(codes);
let most_common_letters = sorted_entries.map(list => {
return list.shift()[0];
});
return most_common_letters.join('');
};
module.exports = {
getMostCommonLetterInEachColumn,
getLeastCommonLetterInEachColumn,
};