How to resolve the algorithm Text processing/Max licenses in use step by step in the PHP programming language
How to resolve the algorithm Text processing/Max licenses in use step by step in the PHP programming language
Table of Contents
Problem Statement
A company currently pays a fixed sum for the use of a particular licensed software package. In determining if it has a good deal it decides to calculate its maximum use of the software from its license management log file. Assume the software's licensing daemon faithfully records a checkout event when a copy of the software starts and a checkin event when the software finishes to its log file. An example of checkout and checkin events are:
Save the 10,000 line log file from here into a local file, then write a program to scan the file extracting both the maximum licenses that were out at any time, and the time(s) at which this occurs. Mirror of log file available as a zip here (offsite mirror).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Text processing/Max licenses in use step by step in the PHP programming language
This code reads a text file that has a series of lines with the format:
03:47:11:481 IN
03:47:11:775 OUT
03:47:12:600 IN
03:47:12:705 OUT
03:47:25:582 IN
03:47:25:795 OUT
Each line represents an event, where the first column is the timestamp of the event, and the second column is the type of event (either "IN" or "OUT").
The code reads the file line by line and keeps track of the number of people currently in the room. It also keeps track of the maximum number of people that have been in the room at any given time, and the timestamps of the times when the maximum number of people were in the room.
At the end of the code, it prints out the maximum number of people that were in the room at any given time, and the timestamps of the times when the maximum number of people were in the room.
In this example, the output would be:
3
03:47:11:775
03:47:12:705
03:47:25:795
This indicates that the maximum number of people in the room at any given time was 3, and that this occurred at the timestamps 03:47:11:775, 03:47:12:705, and 03:47:25:795.
Source code in the php programming language
$handle = fopen ("mlijobs.txt", "rb");
$maxcount = 0;
$count = 0;
$times = array();
while (!feof($handle)) {
$buffer = fgets($handle);
$op = trim(substr($buffer,8,3));
switch ($op){
case 'IN':
$count--;
break;
case 'OUT':
$count++;
preg_match('/([\d|\/|_|:]+)/',$buffer,$time);
if($count>$maxcount){
$maxcount = $count;
$times = Array($time[0]);
}elseif($count == $maxcount){
$times[] = $time[0];
}
break;
}
}
fclose ($handle);
echo $maxcount . '<br>';
for($i=0;$i<count($times);$i++){
echo $times[$i] . '<br>';
}
You may also check:How to resolve the algorithm Functional coverage tree step by step in the Julia programming language
You may also check:How to resolve the algorithm Tree traversal step by step in the Smalltalk programming language
You may also check:How to resolve the algorithm Snake step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Scope/Function names and labels step by step in the Factor programming language
You may also check:How to resolve the algorithm Giuga numbers step by step in the XPL0 programming language