import random def fix_overlaps(l): """ Fixes the overlaps Note: Needs to be a list of pairs (start, end) sorted by start """ res = list() if len(l) == 0: return res cur = l[0] for pair in l: if cur[1] > pair[0]: if cur[1] < pair[1]: cur = (cur[0], pair[1]) else: res.append(cur) cur = pair res.append(cur) return res def random_integer(a, b): return random.randint(a, b) def random_range_list(count, a, b): res = list() for i in range(count): start = random_integer(a, b - 1) # Obviously a real value goes here end = random_integer(start, b) # Obviously a real value goes here pair = (start, end) res.append(pair) return res busy_times = random_range_list(10, 0, 1000) print "Random: ", busy_times sorted_times = sorted(busy_times, key= lambda pair: pair[0]) print "Sorted: ", sorted_times unioned_times = fix_overlaps(sorted_times) print "Unioned: ", unioned_times
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question