Created
July 17, 2020 13:41
-
-
Save mdamien/d329220f185e64ab5fd199fa00d6cafd to your computer and use it in GitHub Desktop.
This file contains hidden or 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
from pprint import pprint | |
def convert(date): | |
day, hour = date.split(' à ') | |
hour = int(hour.replace('h', '')) | |
return day + '_' + str(hour).rjust(2, '0') | |
def merge(start, end, other_start, other_end): | |
if other_start < start and other_end < start: | |
return None | |
if other_start <= start and start <= other_end <= end: | |
return [other_start, end] | |
if start <= other_start <= end and start <= other_end <= end: | |
return [start, end] | |
intervals = [ | |
('2020-07-17 à 12h', '2020-07-17 à 15h'), | |
('2020-07-17 à 17h', '2020-07-17 à 20h'), | |
('2020-07-17 à 14h', '2020-07-17 à 19h'), | |
] | |
intervals = [(convert(start), convert(end)) for start, end in intervals] | |
while True: | |
new_intervals = [] | |
for i, interval in enumerate(intervals): | |
start, end = interval | |
for j, other_interval in enumerate(intervals[i+1:]): | |
other_start, other_end = other_interval | |
result = merge(other_start, other_end, start, end) | |
if not result: | |
result = merge(start, end, other_start, other_end) | |
if result: | |
print('merge >') | |
print('interval', interval, 'and', other_interval) | |
print('result', result) | |
print() | |
intervals.pop(i) | |
intervals.pop(i+j) | |
intervals.append(result) | |
new_intervals = intervals | |
break | |
if new_intervals: | |
break | |
if not new_intervals: | |
break | |
intervals = new_intervals | |
print('Final result >') | |
pprint(intervals) | |
""" | |
commence à 2020-07-17 à 12h | |
fini à 2020-07-17 à 20h | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment