As pointed out in other answers, you really don't need to worry if your list is oil_changestoo long. However, as a fan of "streaming" computing, I am interested to note that it itertoolsoffers all the tools necessary to calculate your value next_oilin O (1) space (and O (N) time of course! -) no matter how big N, i.e. e. len(next_oil)gets.
izip , , O (N). O (1) - izip tee - , O (N) , , ! -). :
it = iter(oil_changes)
a, b = itertools.tee(it)
b.next()
thesum = 0
for thelen, (i, j) in enumerate(itertools.izip(a, b)):
thesum += j - i
last_one = j
next_oil = last_one + thesum / (thelen + 1)
, , , .. ( ) b. tee O (x), x - ; 1, O (1).
izip "" -, enumerate, , , .. , ( +1 , enumerate 0! -). +=, (sum , ! -).
last_one = a.next(), , a - izip iterables , a b ! -). , Python - j - , , b , izip ( , thelen , enumerate). last_one , j , .
, , !) - , , . - "Impara l'Arte, e mettila da parte!"... " , " - , , : , - , , - , , , ! -)