forked from UCL-RITS/rse-classwork-2020
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
65 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"python.pythonPath": "C:\\Users\\umitozm\\AppData\\Local\\Programs\\Python\\Python37-32\\python.exe" | ||
} |
Binary file modified
BIN
-990 Bytes
(56%)
week05-testing/__pycache__/test_times.cpython-38-pytest-6.1.2.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
- generic: | ||
time_range_1: ["2010-01-12 10:00:00", "2010-01-12 12:00:00"] | ||
time_range_2: ["2010-01-12 10:30:00", "2010-01-12 10:45:00", 2, 60] | ||
expected: | ||
- ["2010-01-12 10:30:00","2010-01-12 10:37:00"] | ||
- ["2010-01-12 10:38:00", "2010-01-12 10:45:00"] | ||
- no_overlap: | ||
time_range_1: ["2010-01-12 10:00:00", "2010-01-12 11:00:00"] | ||
time_range_2: ["2010-01-12 12:30:00", "2010-01-12 12:45:00", 2, 60] | ||
expected: [] | ||
- multiple_overlaps: | ||
time_range_1: ["2010-01-12 10:00:00", "2010-01-12 13:00:00", 3, 900] | ||
time_range_2: ["2010-01-12 10:40:00", "2010-01-12 11:20:00", 2, 120] | ||
expected: | ||
- ["2010-01-12 10:40:00","2010-01-12 10:50:00"] | ||
- ["2010-01-12 11:05:00", "2010-01-12 11:20:00"] | ||
- touching: | ||
time_range_1: ["2010-01-12 10:00:00", "2010-01-12 11:00:00"] | ||
time_range_2: ["2010-01-12 11:00:00", "2010-01-12 12:45:00"] | ||
expected: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,34 @@ | ||
import datetime | ||
|
||
|
||
def time_range(start_time, end_time, number_of_intervals=1, gap_between_intervals_s=0): | ||
start_time_s = datetime.datetime.strptime(start_time, "%Y-%m-%d %H:%M:%S") | ||
end_time_s = datetime.datetime.strptime(end_time, "%Y-%m-%d %H:%M:%S") | ||
|
||
if start_time_s > end_time_s: | ||
raise ValueError("Start time is later than end time") | ||
|
||
if(start_time_s>=end_time_s): | ||
raise ValueError('The end of the time range has to come strictly after its start.') | ||
d = (end_time_s - start_time_s).total_seconds() / number_of_intervals + gap_between_intervals_s * (1 / number_of_intervals - 1) | ||
sec_range = [(start_time_s + datetime.timedelta(seconds=i * d + i * gap_between_intervals_s), | ||
start_time_s + datetime.timedelta(seconds=(i + 1) * d + i * gap_between_intervals_s)) | ||
for i in range(number_of_intervals)] | ||
return [(ta.strftime("%Y-%m-%d %H:%M:%S"), tb.strftime("%Y-%m-%d %H:%M:%S")) for ta, tb in sec_range] | ||
|
||
def compute_overlap_helper(tuple1, tuple2): | ||
start1, end1 = tuple1 | ||
start2, end2 = tuple2 | ||
if(start1 >= end2 or start2 >= end1): | ||
return tuple() | ||
else: | ||
low = max(start1, start2) | ||
high = min(end1, end2) | ||
return((low, high)) | ||
|
||
def compute_overlap_time(range1, range2): | ||
overlap_time = [] | ||
for tuple1 in range1: | ||
for tuple2 in range2: | ||
if(overlap := compute_overlap_helper(tuple1, tuple2)): | ||
overlap_time.append((overlap)) | ||
return(overlap_time) | ||
for start1, end1 in range1: | ||
for start2, end2 in range2: | ||
# both ranges need to start before the other ends, otherwise there is no overlap! | ||
if start1 <= end2 and start2 <= end1: | ||
low = max(start1, start2) | ||
high = min(end1, end2) | ||
if high == low: | ||
continue | ||
overlap_time.append((low, high)) | ||
return overlap_time | ||
|
||
""" | ||
if __name__ == "__main__": | ||
large = time_range("2010-01-12 10:00:00", "2010-01-12 12:00:00") | ||
short = time_range("2010-01-12 10:30:00", "2010-01-12 10:45:00", 2, 60) | ||
print(compute_overlap_time(large, short)) | ||
""" |