class Buyer(): def __init__(self, start, finish): self.start = start self.finish = finish def __repr__(self): return "(" + str(self.start) + "," + str(self.finish) + ")" def insertionSort(alist): for index in range(1,len(alist)): currentvalue = alist[index] position = index while position>0 and alist[position-1].finish > currentvalue.finish: alist[position]=alist[position-1] position = position-1 alist[position]=currentvalue def intervals_sche(list_buyer): insertionSort(list_buyer) result = [list_buyer[0]] current_selected = 0 for i in range(1, len(list_buyer)): if (list_buyer[i].start >= list_buyer[current_selected].finish): result.append(list_buyer[i]) current_selected = i return result buyer_list = [Buyer(1, 3), Buyer(1, 13) , Buyer(3, 7), Buyer(2, 5), Buyer(7,11)] list_result = intervals_sche(buyer_list) print(list_result)
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