Advertisement
rfmonk

bisect_example.py

Jan 13th, 2014
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.45 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3.  
  4. import bisect
  5. import random
  6.  
  7. """ Use a constant seed to ensure that the
  8. same pseudo-random numbers are used each
  9. time the loop is run. """
  10. random.seed(1)
  11.  
  12. print 'New pos Contents'
  13. print '--- --- --------'
  14.  
  15. # Generate random numbers and
  16. # insert them into a list in
  17. # sorted order.
  18. l = []
  19. for i in range(1, 15):
  20.     r = random.randint(1, 100)
  21.     position = bisect.bisect(l, r)
  22.     bisect.insort(l, r)
  23.     print '%3d %3d' % (r, position), l
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement