Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* You are given a sequence of access logs in format <IP> <user> <duration>. Write a program to aggregate
- the logs data and print for each user the total duration of his sessions and a list of unique IP addresses
- in format "<user>: <duration> [<IP1>, <IP2>, …]". Order the users alphabetically.
- Order the IPs alphabetically. */
- function aggregator(arr) {
- var countLogs = Number(args[0]);
- var aggregator = {};
- var i;
- for (i = 0; i < countLogs; i += 1) {
- var logLine = args[i + 1].split(/\s+/).filter(Boolean);
- var IPaddress = logLine[0].trim();
- var user = logLine[1].trim();
- var duration = Number(logLine[2]);
- if (!(aggregator.hasOwnProperty(user))) {
- aggregator[user] = { duration: 0, IP_addresses: [] };
- }
- aggregator[user].duration += duration;
- if (aggregator[user].IP_addresses.indexOf(IPaddress) < 0) {
- aggregator[user].IP_addresses.push(IPaddress);
- }
- }
- var sortedNames = Object.keys(aggregator).sort();
- for (i = 0; i < sortedNames.length; i += 1) {
- if (aggregator.hasOwnProperty(sortedNames[i])) {
- aggregator[sortedNames[i]].IP_addresses.sort();
- console.log("%s: %s [%s]", sortedNames[i], aggregator[sortedNames[i]].duration,
- aggregator[sortedNames[i]].IP_addresses.join(', '));
- }
- }
- }
- aggregator(['7',
- '192.168.0.11 peter 33',
- '10.10.17.33 alex 12',
- '10.10.17.35 peter 30',
- '10.10.17.34 peter 120',
- '10.10.17.34 peter 120',
- '212.50.118.81 alex 46',
- '212.50.118.81 alex 4'
- ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement