Find Intersection between Times - rust

Hello i'm learning Rust at the college and i'm in trouble with an exercise the professor gave us to learn use iterators better.
The exercise says that: you have 2 files, calendar1 and calendar 2 in this format:
Content of file calendar1:
08:00 20:00
10:00 11:30
14:00 16:00
where the first row is the start hour and the end hour of the workday.
The other rows are the scheduled appointments that are already done for that workday.
Given 2 calendars of the same day, and a number of minutes an appointment has to be taken, you have to write as std output all the possible intervals that are suitable for both the calendars.
Here is an example of output:
Content of file cal1:
08:00 20:00
10:00 11:30
14:00 16:00
Content of file cal2:
09:00 18:00
09:30 12:00
13:30 16:30
cargo run -- cal1 cal2 30
09:00 09:30
12:00 13:30
16:30 18:00
I've already put some codes and read the files properly and putted them in a struct using the NaiveTime Type to handle hours and minutes
#[derive(PartialEq,Clone,Debug)]
pub struct Calendar {
schedule: Vec<(NaiveTime, NaiveTime)>,
bounds: (NaiveTime,NaiveTime)
}
Now i'm in trouble because the exercise says that, in order to solve this problem, you have to use a complexity of O(M+N) where M and N are the length of the two schedule vectors.
Additionally to solve this the hint is: "use two indipendent iterator, for each list of appointments, and everytime you find a correct "hole" save the interval"
I really don't know how to use the iterators separately because i dont understand how can i check that an interval which, for example, is suitable for the first calendar, at the same time is suitable for the second one. Any help? Thank You

Related

Count for every 'x' that falls into an interval within a given time range?

I'm looking to get a headcount of people available every hour within a time frame.
Employee
Start Time
End Time
Emp name
8:30 AM
10:30 AM
Emp name
8:30 AM
10:45 AM
Emp name
8:00 AM
10:15 AM
If I were to take a count of employees available at 9:45 AM, I should get 3. Or maybe I want 15 minute intervals and want to know my headcount at 8:15, which would be 1.
Currently my solution is to make a table for every hour in a day using the following formula to add a tally for every hour an employee is available:
=IF(AND($G9>=R$1,$G9<S$1),1,IF(AND(R$1>$G9,R$1<$H9),1,""))
And i get something that looks like this:
Start Time
End Time
8:00 AM
9:00 AM
10:00 AM
11:00 AM
8:30 AM
10:30 AM
1
1
Finally I can pivot this or countif it to get my total.
Is there a more elegant solution to this? Preferably one that allows me to change the interval but still gives me an accurate count. Even better if I don't need to build out the interval table.
I'm open to using VBA or formulas.
Overall, what is this problem called? I'd like to look up a pythonic solution on my own time for some self study.
If every line represents a different employee, then just use COUNTIFS:
=COUNTIFS(B:B,"<="&E2,C:C,">="&E2)

Specific overtime calculation

I'm working on a simple timesheet that should calculate overtime1 and overtime2, I just cant figure out a good way to fix it.
Normal workday on 8hrs from 07:00 to 17:00 give no overtime (eg 07:00 to 16:00 or 08:00 to 17:00), but when time exceeds 8 hours I would like to get the exceeding hours in a cell.
My business rules are:
1) Any work greater than 8 hours between the hours of 06:00 to 20:00 get paid as overtime1.
2) Any work less than 8 hours will not generate any overtime2 even if after 20:00
3) Any work performed earlier than 06:00 or later than 20:00 get paid at the overtime2 rate.
Example 1: Working from 07:00 to 18:00 would get a value of 3 hours of overtime1
Example 2: Working from 14:00 to 22:00 would generate 0 hours of overtime2.
Example 3: Working 05:00 to 21:00 would give overtime1 6 hours and overtime2 2 hours (1 hour before 06:00, 1 hour after 20:00).
This is a pretty easy problem to solve if your data is laid out smartly. With column A as clock in time and column B as clock out time, use this formula as a helper to determine if you should use the clock in time or your base of 06:00:
=IF(A1-FLOOR(A1,2)<6/24,6/24,A1-FLOOR(A1,2))
Then use this formula to determine if you should use clock out time or 20:00:
=IF(B1-FLOOR(B1,2)>20/24,20/24,B1-FLOOR(B1,2))
Then you subtract the two to get fractions of a day, multiply by 24 to convert to hours, then subtract 8 to get hours of overtime1. Combined in a super formula it looks like this in C1:
OT1: =IF(OR(ISBLANK(A1),ISBLANK(B1)),"",(IF(B1-FLOOR(B1,2)>20/24,20/24,B1-FLOOR(B1,2))-IF(A1-FLOOR(A1,2)<6/24,6/24,A1-FLOOR(A1,2)))*24-8)
Remember, Excel formats dates where 1 = 24 hours. Also, I added in an OR(ISBLANK(A1),ISBLANK(B1)) statement to make sure you get a null string if one of the values is blank.
Starting on the overtime2, you need to split it into two parts: before 06:00 and after 20:00. The first part checks if the clock in time is earlier than 06:00 and if so figures out how many hours. The formula ultimately ends up being:
=IF(IF(A1-FLOOR(A1,2)<6/24,6/24,A1-FLOOR(A1,2))<=6/24,(6/24-(A1-FLOOR(A1,2)))*24,0)
For after 20:00, the same pattern is used. Figure out how many parts of a day were logged after 20:00. The final formula ends up being:
=IF(IF(B1-FLOOR(B1,2)>20/24,20/24,B1-FLOOR(B1,2))>=20/24,((B1-FLOOR(B1,2))-20/24)*24,0)
Finally, to figure out the total number of overtime 2, just add the two formulas together in D1:
OT2: =IF(OR(ISBLANK(A1),ISBLANK(B1)),"",IF(IF(A1-FLOOR(A1,2)<6/24,6/24,A1-FLOOR(A1,2))<=6/24,(6/24-(A1-FLOOR(A1,2)))*24,0)+IF(IF(B1-FLOOR(B1,2)>20/24,20/24,B1-FLOOR(B1,2))>=20/24,((B1-FLOOR(B1,2))-20/24)*24,0))
It's just about getting the logic right and understanding that Excel treats 07:00 as a decimal equal to 7/24, for example.
OT1
=IF(NOT(AND(Sheet1!$A2>=7/24,Sheet1!$B2<=17/24)),MIN(20/24,Sheet1!$B2)-Sheet1!$A2-9/24,0)
OT2
=IF(MIN(20/24,Sheet1!$B2)-Sheet1!$A2-9/24,MAX(B2-20/24,0),0)

Reply Time Issue

I'm from Turkiye and my level is intermediate. I reply mails in 8 hours at work. My question is about time calculation.
Conditions
1. Our work starts at 09:00 and finishes at 18:00.
2. Mails must be replied in 8 hours.
3. Mails must be replied only between 09:00 and 18:00.
4. We don't work between 18:00 and the next day 09:00.
So these period doesn't count in the calculation. This is the most critical part also.
My Excel File. I explained all conditions in the workbook.
So we can break down your all condition to basically two categories. One, where (Mail Received Time + 8 Hours) is less than or equal to 18:00 Hrs, other is where it falls beyond 18:00 Hrs.
Use the formula in the Reply Deadline with formula column and drag it down to get the desired answer
=IF((A7+"8:00"<=TIME(18,0,0)),MAX(A7,"9:00")+"8:00",MIN(A7,"18:00")+"23:00")
Explanation
IF condition checks if the mail received time + 8:00 hrs is less than or equal to 18:00 hrs and returns a TRUE or FALSE value
Output: =IF((FALSE),MAX(A7,"9:00")+"8:00",MIN(A7,"18:00")+"23:00")
If TRUE the time is added by 8:00 hrs to give the deadline. MAX has been used to eliminate any mail received time that is before 9:00 Hrs as in 08:28 Hrs
If FALSE the time is added by 23:00 hrs. This is because 8:00 hrs is the normal deadline and remaining 15:00 hrs is accounted for time we don't work i;e from 18:00 Hrs to next day 9:00 Hrs. MIN is used to consider when the mail received time is less than 18:00 hrs.
Let me know if I can make it more clear.
EDIT
Here is my sample file with solution formulas embedded.
Please try the below formula,
=IF(OR(A2<TIME(9,0,0),A2>TIME(18,0,0)),TIME(17,0,0),IF(A2+TIME(8,0,0)<=TIME(18,0,0),A2+TIME(8,0,0),A2+TIME(23,0,0)))
This formula consists of 2 IF conditions.
The first IF checks if the time falls in non-working hours (between 18:00 and 9:00). If so, the time is calculated as 17:00. If the time falls in the working range, the next IF condition adds 8:00 hours or 23:00 accordingly. (23:00 hours for overlapping period). Hope this helps.
excelevator solved the problem on reddit also.
https://www.reddit.com/r/excel/comments/65l25n/reply_time_calculation_issue/

Excel: auto format time of day when inputting time

I'm inputting time in and time out logs for my business to calculate the total hours worked by individual contractor companies visiting my site. We operate from 6:30am to 5pm. Excel automatically formats the data as AM, except for 12:00-12:59 which it correctly assumes is PM. I need excel to recognize that any time between 12:00 and 5:00 is PM. This will save me from having to type PM for each "time out" which over the course of a year will save me hours.
I'm not really sure how to do this. I'm guessing an if/then macro?
You can change the cell format as shown below:
when you type 17:00 is correctly displayed as 5:00 PM
I just use military time to get around this problem. 5 PM is not 5:00, but rather 17:00. Then you don't need to put in a PM or anything. Note: This is just for inputting the time. You can still display the time as 5:00 PM or whatever by formatting those cells or columns as the 'Time' type.
1 PM = 13:00
2 PM = 14:00
3 PM = 15:00
4 PM = 16:00
5 PM = 17:00
6 PM = 18:00
etc.

Calculating time range in Excel

Assume I have such data:
29.10.2014 19:00 30.10.2014 7:30
29.10.2014 23:00 29.10.2014 18:00
30.10.2014 9:00 30.10.2014 23:15
31.10.2014 18:49 1.11.2014 7:15
How to get that time which is between 22:00 to 6:00. Like first example row 29.10.2014 19:00 30.10.2014 7:30 whole work time is 12:30 (12,5h) and work time between 22-6 is 8:00 (8h). How to get this 8h. Used some searches and find sumproducts, sumifs, countifs but didn't handle them by myself. :/
This article seems to answer your question:
https://office.microsoft.com/en-us/excel-help/calculate-the-difference-between-two-times-HP003056108.aspx
It's easier to calculate the time between 06:00 and 22:00, so assuming start time/date in A2 and end time/date in B2 use this formula to get decimal hours
=(MOD(B2,1)< MOD(A2,1))*(22-6)+MEDIAN(6,22,MOD(B2,1)*24)-MEDIAN(6,22,MOD(A2,1)*24)
That also assumes that end time will either be the same day or the next, is that right?
To get 22-6 time just calculate total time minus the above, so if the above is in C2 use this in D2
=(B2-A2)*24-C2
I assume that your second example is an error because start time/date is after end time/date

Resources