Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>import heapq import itertools import operator def increasing(fn, left, right): """ Given two never decreasing iterators produce another iterator resulting from passing the value from left and right to fn. This iterator should also be never decreasing. """ # Imagine an infinite 2D-grid. # Each column corresponds to an entry from right # Each row corresponds to an entry from left # Each cell correspond to apply fn to those two values # If the number of columns were finite, then we could easily solve # this problem by keeping track of our current position in each column # in each iteration, we'd take the smallest value report it, and then # move down in that column. This works because the values must increase # as we move down the column. That means the current set of values # under consideration must include the lowest value not yet reported # To extend this to infinite columns, at any point we always track a finite # number of columns. The last column current tracked is always in the top row # if it moves down from the top row, we add a new column which starts at the top row # because the values are increasing as we move to the right, we know that # this last column is always lower then any columns that come after it # Due to infinities, we need to keep track of all # items we've ever seen. So we put them in this list # The list contains the first part of the incoming iterators that # we have explored left_items = [next(left)] right_items = [next(right)] # we use a heap data structure, it allows us to efficiently # find the lowest of all value under consideration heap = [] def add_value(left_index, right_index): """ Add the value result from combining the indexed attributes from the two iterators. Assumes that the values have already been copied into the lists """ value = fn( left_items[left_index], right_items[right_index] ) # the value on the heap has the index and value. # since the value is first, low values will be "first" on the heap heapq.heappush( heap, (value, left_index, right_index) ) # we know that every other value must be larger then # this one. add_value(0,0) # I assume the incoming iterators are infinite while True: # fetch the lowest of all values under consideration value, left_index, right_index = heapq.heappop(heap) # produce it yield value # add moving down the column if left_index + 1 == len(left_items): left_items.append(next(left)) add_value(left_index+1, right_index) # if this was the first row in this column, add another column if left_index == 0: right_items.append( next(right) ) add_value(0, right_index+1) def fib(): a = 1 b = 1 while True: yield a a,b = b,a+b r = increasing(operator.add, fib(), itertools.count() ) for x in range(100): print next(r) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload