In [1]:
import bisect
import sys

In [17]:
HAYSTACK = [1, 4, 5, 6, 8 , 12, 15, 20, 21, 23, 23, 26, 29, 30]
NEEDLES = [0, 1, 2, 5, 8, 10, 22, 23, 29, 30, 31]

In [4]:
ROW_FMT = '{0:2d} @{1:2d}    {2}{0:<2d}'

In [5]:
def demo(bisect_fn):
    for needle in reversed(NEEDLES):
        position = bisect_fn(HAYSTACK, needle)
        offset = position * '  |'
        print(ROW_FMT.format(needle, position, offset))

In [13]:
bisect_fn = bisect.bisect

In [20]:
print('DEMO:', bisect_fn.__name__)
print('haystack ->', ' '.join('%2d' % n for n in HAYSTACK))
demo(bisect_fn)


DEMO: bisect
haystack ->  1  4  5  6  8 12 15 20 21 23 23 26 29 30
31 @14      |  |  |  |  |  |  |  |  |  |  |  |  |  |31
30 @14      |  |  |  |  |  |  |  |  |  |  |  |  |  |30
29 @13      |  |  |  |  |  |  |  |  |  |  |  |  |29
23 @11      |  |  |  |  |  |  |  |  |  |  |23
22 @ 9      |  |  |  |  |  |  |  |  |22
10 @ 5      |  |  |  |  |10
 8 @ 5      |  |  |  |  |8 
 5 @ 3      |  |  |5 
 2 @ 1      |2 
 1 @ 1      |1 
 0 @ 0    0 

In [21]:
bisect_fn(HAYSTACK, 3)


Out[21]:
1

In [22]:
HAYSTACK


Out[22]:
[1, 4, 5, 6, 8, 12, 15, 20, 21, 23, 23, 26, 29, 30]

In [ ]:


In [ ]:


In [ ]: