Created
September 20, 2014 16:26
-
-
Save boyanov83/7786b035f261ac05c7e8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.Scanner; | |
public class LogsAggregator | |
{ | |
public static void main(String[] args) | |
{ | |
Scanner scan = new Scanner(System.in); | |
int linesNum = Integer.parseInt(scan.nextLine()); | |
ArrayList<String> input = new ArrayList<String>(); | |
String[] line = new String[3]; | |
for (int i = 0; i < linesNum; i++) | |
{ | |
line = scan.nextLine().split(" "); | |
input.add(line[0]); | |
input.add(line[1]); | |
input.add(line[2]); | |
} | |
; | |
ArrayList<String> names = new ArrayList<String>(); | |
ArrayList<Integer> times = new ArrayList<Integer>(); | |
ArrayList<String> ips = new ArrayList<String>(); | |
for (int i = 0; i < input.size(); i+=3) | |
{ | |
names.add(input.get(i+1)); | |
} | |
names = removeDuplicatesAndSort(names); //sort names and remove duplicates | |
int currentTime = 0; | |
for (int i = 0; i < names.size(); i++) | |
{ | |
for (int j = 0; j < input.size(); j+=3) | |
{ | |
if (names.get(i).equals(input.get(j+1))) | |
{ | |
currentTime += Integer.parseInt(input.get(j+2)); | |
} | |
} | |
times.add(currentTime); | |
currentTime = 0; | |
} | |
for (int i = 0; i < names.size(); i++) | |
{ | |
System.out.printf("%s: %d ",names.get(i), times.get(i)); | |
for (int j = 0; j < input.size(); j+=3) | |
{ | |
if (names.get(i).equals(input.get(j+1))) | |
{ | |
ips.add(input.get(j)); | |
} | |
} | |
removeDuplicatesAndSort(ips); | |
System.out.println(ips); | |
ips.clear(); | |
} | |
} | |
private static ArrayList<String> removeDuplicatesAndSort(ArrayList<String> names) | |
{ | |
for (int i = 0; i < names.size(); i++) | |
{ | |
for (int j = i+1; j < names.size(); j++) | |
{ | |
if (names.get(i).equals(names.get(j))) | |
{ | |
names.remove(j); | |
j--; | |
} | |
} | |
} | |
Collections.sort(names); | |
return names; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment