Welcome to our new coding practice lesson! We have an interesting problem in this unit that centers around data from a social networking app. The challenge involves processing logs from this app and extracting useful information from them. This task will leverage your skills in string manipulation, working with timestamps, and task subdivision. Let's get started!
Imagine a social networking application that allows users to form groups. Each group has a unique ID ranging from 1 up to n
, the total number of groups. Interestingly, the app keeps track of when a group is created and deleted, logging all these actions in a string.
The task before us is to create a Ruby method named analyze_logs
. This method will take as input a string of logs and output an array of arrays representing the groups with the longest lifetime. Each inner array contains two items: the group ID and the group's lifetime. By 'lifetime,' we mean the duration from when the group was created until its deletion. If a group has been created and deleted multiple times, the lifetime is the total sum of those durations. If multiple groups have the same longest lifetime, the method should return all such groups in ascending order of their IDs.
For example, if we have a log string as follows:
"1 create 09:00, 2 create 10:00, 1 delete 12:00, 3 create 13:00, 2 delete 15:00, 3 delete 16:00"
,
the method will return: [[2, '05:00']]
.
Alternatively, if we have the following log string: "1 create 09:00, 1 delete 11:00, 2 create 10:00, 2 delete 12:00, 3 create 09:30, 3 delete 11:30,"
, the output should be:
[[1, '02:00'], [2, '02:00'], [3, '02:00']]
.
