I need to place a break that is at least 3 hours from the start of a shift and 3 hours from the end of the shift.
Example:
Start = 17
End = 3
Result = [20,21,22,23,0]
Another example:
Start = 7
End = 19
Result = [10,11,12,13,14,15,16]
I am wondering how I can code the logic for this calculation in Python.
CodePudding user response:
You can just add 3 to the start and subtract 2 from then end. The check to make sure the end is larger (if not add 24). Then mod 24 the hours. You will need to handle cases where the time wraps all the way around and when the shift is empty :
def get_hours(start, end):
if 0 < end - start < 6:
return []
start = 3
end -= 2
return [hour % 24 for hour in range(start, end if end > start else end 24)]
get_hours(17, 3)
# [20, 21, 22, 23, 0]
get_hours(7, 19)
# [10, 11, 12, 13, 14, 15, 16]
get_hours(2, 1) # really long shift
# [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
get_hours(1, 4) # really short shift (not 4am to 1am)
# []
get_hours(1, 7) # short but not empty shift
# [4]
CodePudding user response:
start = 3
end -= 3
if end < start:
end = 24
return [hour % 24 for hour in range(start, end 1)]