1 | initial version |
Probably this can only be done by defining some auxiliary function(s), or importing such functions.
My one-line solution using the (standard) itertools
library:
sage: import itertools
sage: reduce(lambda z, w: w, itertools.takewhile(lambda x: x[1] <= 0.4, itertools.accumulate(reversed(D), lambda a, b: [a[0] + b[0], a[1] + b[1]])))
[16800.0000000000, 0.333200000000000]
Here,
reversed(D)
allows iterating over the list in reverse,itertools.accumulate
allows accumulating a running total,itertools.takewhile
allows stopping when the accumulated percentage reaches some value,reduce
is used with lambda z, w: w
as a trick to get the last element.