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)
In [21]:
bisect_fn(HAYSTACK, 3)
Out[21]:
In [22]:
HAYSTACK
Out[22]:
In [ ]:
In [ ]:
In [ ]: