[No Audio]
We are working on a project with very tight
time schedule, we want our employees to put
some extra effort. To make sure that the
employee spent their time productively, we
have divided their time schedule in chunks
of two hour slots. The first slot is from
8 to 10 on Monday, the next slot is from
10 to 12 on the same day, and this way, we
have numbered all the slots during the week.
After each time slot, we would like the
employee to tell us what he has produced. The
employees may decide to take some rest
for some slots during the week. However, some
of the employees may work too much in
consecutive time slots, which may negatively
affect their overall productivity and well
being. We would like to determine the
employees who has worked non stop for the
longest time after some interval of time, so
that we may ask to him to have some break,
this is necessary for their overall
productivity and well being.
The input will be
in the form of vector containing numbers
indicating the time slots. The numbers are
not necessarily in order. In this case now,
there are four busy periods for the employees
indicated by different colors. The first busy
period is indicated by the black color
containing the slots of 1 and 2. The next
one is indicated by the green color, which has
a length of three slots, and so on. The highest
length of any busy period in this case, is 3.
Let us code this problem now.
The solution will be based on the use of HashSets,
so we will add the HashSet from the standard library.
[No Audio]
We will start by defining the schedule of all
the employees in the form of vector, I will
use a multi dimensional vectors for this
purpose. Let us have some values representing
the slots in which an employee has worked.
[No Audio]
Each inner vector in this case, represents the
time slot in which an employee has worked.
Next we will define a function to implement
the desired functionality.
[No Audio]
The input to the function will be the
schedule vector that we defined in the main,
which contains the working slots, and the
output will be the employee number, which has
worked nonstop for the longest period, which
will be of type u8.
[No Audio]
First I will define an empty vector, which
will store the longest nonstop work of all the employees.
[No Audio]
Next, we will iterate through each employee's
time slot vector one by one.
[No Audio]
Next for each employee, I will compute his respective
longest term period of work. I will compute
this using another function called longest_period,
so let me define it.
[No Audio]
The input to the function will be
vector of the employee, which I will call
working_slots, and the output will
be the longest_period of slots for which the
employee has worked nonstop, which will be of type u8.
[No Audio]
Inside the function, I will
first declare a variable which will be used
to keep track of the longest busy period, and
I will initialize it from 0.
[No Audio]
Next, we will use a special data type called HashSet.
A HashSet is a
type of HashMap, which will only have keys
and not values. This means, we only need to
define the keys and not the values. It is
useful in situations where we just want to
quickly know if something happens to be
inside a collection or not. This is because
looking for a key in a HashMap is very fast.
The specific reason as to why we choose it
here will become evident to us at the
end of the function definition. I will define
a HashSet, and we'll collect all the time slot
numbers of the employees in it.
This will essentially convert all the values of
the vectors as keys of the HashSet. Now we
will iterate through all the keys in the HashSet.
[No Audio]
First I will iterate through all the
slots one by one.
Okay, let us understand now
the logic that we will use for determining
the consecutive time slots. First we will
identify the beginning of the consecutive
time slots. The essential requirement for a
time slot to be the first time slot in a
sequence is that, there should be no value
lesser by 1 than the value of that time slot.
For instance, if we have the sequence
of let's say 2,3,4, so this means that the
time slot of 1, should not be present in the
HashSet, we will check this condition using
an if statement.
[No Audio]
Please note that, we used a reference since
the contains function accept reference to
the keys. Inside the if, we will keep on
iterating as long as there are next time
slots in the HashSet. I will make a couple of variables.
[No Audio]
The variable current_slot will be
incremented by one inside the loop, and will
be initialized from the first slot value in
the sequence. Since the slot variable is a
reference, so therefore, we need to make an
owned copy of that using the to_owned function.
This function creates owned data from
borrowed data usually by cloning.
The variable current_consecutive_slot
will keep track of the number
of consecutive slots that the employee has
worked. Next I will iterate through the
HashSet as long as there are
next slots in the HashSet.
[No Audio]
Inside the loop, we will increment the
current_slot, so that in the next
iteration it can be checked for presence in the HashSet.
[No Audio]
We will also increment current_consecutive_slot by one.
[No Audio]
Once the loop end, we
will see if the current_consecutive_slot
that we have just found, is
greater than the previous longest_buzy_period.
[No Audio]
If it is, then we will update the longest_buzy_period
from the value of current_consecutive_slot.
[No Audio]
When the loop ends, we will return the
longest_buzy_period, which basically stores the
longest busy period for the employee.
[No Audio]
Okay now let us use this function in the
longest_buzy_time function.
I will call this function for each
employee one by one inside the for loop, and
will store the result in the
employee_longest_nonstop_work vector.
[No Audio]
Next, we will display the longest_nonstop_work
of each employee.
[No Audio]
Finally, I will find out the maximum
longest_nonstop_work of any employee using the max function.
[No Audio]
Finally, we will return the employee number
corresponding to the maximum nonstop work,
this means we need to return the index
corresponding to the maximum value in the
vector, this can be done using the position function.
[No Audio]
Please note that the position function needs
a closure, where we will mention some
condition. The condition in this case is to
check and retrieve the index for the value
which is equal to the maximum value.
The maximum value returned from the above line is,
is wrapped inside the option, so therefore,
we first unwrap the max_val, and
then the position is also wrapped by the
option, so another unwrap will return the
final position. Since the return type is u8,
so we will convert, and this is also the
return statement. Finally, we will add a
suitable print statement inside the main
function, where we will display the employee
with the highest nonstop work by calling the
longest_buzy_time.
[No Audio]
Let us cargo run this.
[No Audio]
That is great. It has
displayed to us the nonstop working hours of
each employee, let us confirm. The employee 1
has worked for three consecutive slots, so his
highest nonstop work is 3 slots.
If we look at the first vector, we can confirm that,
yes, it is the slots of 4,5, and 6. The
final result is that of employee 2, because
he has the highest nonstop slots. That is it
for this particular tutorial. In the upcoming
tutorial, we will be looking at an example
where we will be using the box smart
pointer and binary search trees, that will be
fun and interesting. Do come back for learning
that, and until next tutorial, enjoy Rust programming.
[No Audio]