Hi Could I ask some difficult algorithm question?
109.169.248.247
109.169.248.247
109.169.248.248
109.170.248.248
46.72.177.3
46.72.177.4
From the input above, I would like to print the following, I am using diverse approaches, but I am not good at algorithm, so it is very hard to achieve this, does anyone have solution for this?
109 4
169 3
248 3
247 2
248 1
170 1
248 1
248 1
46 2
72 2
177 2
3 1
4 1
By using this code, I can calculate the number of the whole ip's count, but I can't calculate the number for each ip segment's count. Could you please help me with solving this problem?
function assign(obj, keyPath, value) {
lastKeyIndex = keyPath.length-1;
firstCreated = false;
for (var i = 0; i < lastKeyIndex; ++ i) {
key = keyPath[i];
if (!(key in obj)) {
obj[key] = {};
firstCreated = true;
}
obj = obj[key];
}
if(firstCreated) {
obj[keyPath[lastKeyIndex]] = parseInt(value);
}
else {
obj[keyPath[lastKeyIndex]] = parseInt(obj[keyPath[lastKeyIndex]]) + 1;
}
}