import random
class Element():
def __init__(self, time):
self.time = time
l = [ Element(z*5 - random.randrange(6) + 3) for z in range(21)]
print('list:', [z.time for z in l])
target_time = 72
print('cutoff time:', target_time)
match = False
working_list_from = 0
working_list_to = len(l)
while not match:
mid = (working_list_to - working_list_from) // 2
if l[working_list_from + mid].time < target_time:
working_list_from += mid
else:
working_list_to -= mid
match = (working_list_to - working_list_from) == 1
print(' resulting list', [z.time for z in l[working_list_from+mid:]])
結果:
list: [3, 5, 9, 18, 21, 25, 32, 33, 40, 48, 49, 57, 62, 67, 73, 73, 79, 85, 93, 94, 99]
cutoff time: 72
resulting list [73, 73, 79, 85, 93, 94, 99]