标签:work hashmap color call 时间 min status new mount
time a given player spends actually connected to the network. We keep console logs of various game subsystems for each play session. Each log message has the following format: (MM/dd/yyyy-hh:mm:ss) :: [message logged] Sample log lines: (11/12/2015-02:34:56) :: START (01/02/1990-13:10:00) :: DISCONNECTED (03/13/2018-21:01:01) :: ERROR - File "close.png" not found. Log messages that pertain to network connectivity are as follows: START : Logged when the game starts up. CONNECTED : Logged when a network connection is established. DISCONNECTED : Logged when network connection is lost. SHUTDOWN : Logged when the player is quitting the game. A player‘s session length is the amount of time between the START and SHUTDOWN messages. A player‘s connected time is the amount of time they spend in a connected state. A player starts the game disconnected, becomes connected when we log a CONNECTED message, and becomes disconnected when we log a DISCONNECTED message. You can make the following assumptions: All logs will be properly formatted All dates will be valid No log message will stretch across multiple lines All log files will be ordered chronologically There will always be exactly one START and exactly one SHUTDOWN event logged Input A file containing lines of log messages. Output The connectivity percentage as a string, rounded down to an integer. Examples (01/01/2000-01:00:00) :: START (01/01/2000-01:01:00) :: CONNECTED (01/01/2000-01:21:00) :: DISCONNECTED (01/01/2000-01:50:00) :: SHUTDOWN The player spent 20 minutes out of 50 connected, 20 / 50 = 0.4, output should be "40%" (02/03/2002-14:00:00) :: START (02/03/2002-14:00:00) :: CONNECTED (02/03/2002-14:08:00) :: DISCONNECTED (02/03/2002-14:10:00) :: CONNECTED (02/03/2002-14:15:00) :: SHUTDOWN The player spent 13 minutes out of 15 connected, 13 / 15 = 0.8667, output should be "86%" More sample input can be found in the text files input_1.txt, input_2.txt, and input_3.txt
有事游戏中有Log用于记录各种状态,想知道究竟用户连接的时间是多少。状态除了START, CONNECTED, DISCONNECTED, SHUTDONW外还有ERROR啥的,但是只需要关注前四个。 输入形式: vector<string> lines (11/01/2015-04:00:00) :: START (11/01/2015-04:00:00) :: CONNECTED (11/01/2015-04:30:00) :: DISCONNECTED (11/01/2015-04:45:00) :: CONNECTED (11/01/2015-05:00:00) :: SHUTDOWN
涉及到处理Date, 以及STDIN from a file
处理Date可以用SimpleDateFormat这个class
static Date parseTime(String timeStr) {
Date time = new Date();
DateFormat dft = new SimpleDateFormat("MM/dd/yyyy-hh:mm:ss");
try {
time = dft.parse(timeStr);
}catch (ParseException ignored) {}
return time;
}
1 package pocketGems; 2 3 import java.io.*; 4 import java.util.*; 5 import java.text.DateFormat; 6 import java.text.ParseException; 7 import java.text.SimpleDateFormat; 8 9 public class LogParser2 { 10 public static void main(String[] args) 11 throws FileNotFoundException, IOException { 12 String filename = "C:/Users/yang liu/workspace/Interview2017/src/pocketGems/test1.txt"; 13 if (args.length > 0) { 14 filename = args[0]; 15 } 16 17 18 String answer = parseFile(filename); 19 System.out.println(answer); 20 } 21 22 static String parseFile(String filename) 23 throws FileNotFoundException, IOException{ 24 BufferedReader input = new BufferedReader(new FileReader(filename)); 25 List<String> allLines = new ArrayList<String>(); 26 String line = ""; 27 while ((line = input.readLine()) != null) { 28 allLines.add(line); 29 } 30 input.close(); 31 return parseLines(allLines.toArray(new String[allLines.size()])); 32 } 33 34 static String parseLines(String[] lines) { 35 Map<String, Integer> status = new HashMap<String, Integer>(); 36 37 status.put("START", 0); 38 status.put("CONNECTED", 1); 39 status.put("DISCONNECTED", -1); 40 status.put("SHUTDOWN", -2); 41 42 long totalTime = 0; 43 long connectTime = 0; 44 boolean isConnected = false; 45 Date lastConnectMoment = new Date(); 46 Date startMoment = new Date(); 47 Date shutMoment = new Date(); 48 49 for (String line : lines) { 50 String[] lineSplit = line.split(" :: "); 51 String event = lineSplit[1]; 52 if (!status.containsKey(event)) continue; 53 54 String cur = lineSplit[0]; 55 Date currentTime = parseTime(cur.substring(1, cur.length()-1)); 56 57 58 int eventID = status.get(event); 59 if (eventID > 0) { 60 if (!isConnected) 61 lastConnectMoment = currentTime; 62 isConnected = true; 63 } 64 else if (eventID < 0) { 65 if (isConnected) 66 connectTime += currentTime.getTime() - lastConnectMoment.getTime(); 67 isConnected = false; 68 } 69 if (eventID == 0) startMoment = currentTime; 70 if (eventID == -2) shutMoment = currentTime; 71 } 72 totalTime = shutMoment.getTime() - startMoment.getTime(); 73 74 double ratio = (double)connectTime/totalTime * 100; 75 return String.format("%d%s", (int)ratio, "%"); 76 } 77 78 static Date parseTime(String timeStr) { 79 Date time = new Date(); 80 DateFormat dft = new SimpleDateFormat("MM/dd/yyyy-hh:mm:ss"); 81 try { 82 time = dft.parse(timeStr); 83 }catch (ParseException ignored) {} 84 return time; 85 } 86 }
标签:work hashmap color call 时间 min status new mount
原文地址:http://www.cnblogs.com/EdwardLiu/p/6354194.html