#!/usr/bin/env python
#
# $Id: bisect_example.py 601 2007-05-06 13:04:31Z dhellmann $
#
"""Example use of the bisect module.
"""

import bisect
import random

# Use a constant seed to ensure that we see
# the same pseudo-random numbers each time
# we run the loop.
random.seed(1)

# Generate 20 random numbers and
# insert them into a list in sorted
# order.
l = []
for i in range(1, 20):
	r = random.randint(1, 100)
	position = bisect.bisect(l, r)
	bisect.insort(l, r)
	print '%2d %2d' % (r, position), l

# Reset the seed
random.seed(1)

# Use bisect_left and insort_left.
l = []
for i in range(1, 20):
	r = random.randint(1, 100)
	position = bisect.bisect_left(l, r)
	bisect.insort_left(l, r)
	print '%2d %2d' % (r, position), l

