1
0

701_bisect.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. from bisect import bisect_left, bisect_right, insort_left, insort_right
  2. a = [1, 1, 2, 6, 7, 8, 16, 22]
  3. assert bisect_left(a, 23) == 8 # 23 does not exist in the list
  4. assert bisect_left(a, 22) == 7 # 22 exists in the list
  5. assert bisect_left(a, 0) == 0 # 0 does not exist in the list
  6. assert bisect_left(a, 1) == 0 # 1 exists in the list
  7. assert bisect_left(a, 5) == 3 # 5 does not exist in the list
  8. assert bisect_left(a, 6) == 3 # 6 does exist in the list
  9. # test bisect_right
  10. assert bisect_right(a, 23) == 8 # 23 does not exist in the list
  11. assert bisect_right(a, 22) == 8 # 22 exists in the list
  12. assert bisect_right(a, 0) == 0 # 0 does not exist in the list
  13. assert bisect_right(a, 1) == 2 # 1 exists in the list
  14. assert bisect_right(a, 5) == 3 # 5 does not exist in the list
  15. assert bisect_right(a, 6) == 4 # 6 does exist in the list
  16. # test insort_left
  17. insort_left(a, 23)
  18. assert a == [1, 1, 2, 6, 7, 8, 16, 22, 23]
  19. insort_left(a, 0)
  20. assert a == [0, 1, 1, 2, 6, 7, 8, 16, 22, 23]
  21. insort_left(a, 5)
  22. assert a == [0, 1, 1, 2, 5, 6, 7, 8, 16, 22, 23]
  23. insort_left(a, 1)
  24. assert a == [0, 1, 1, 1, 2, 5, 6, 7, 8, 16, 22, 23]
  25. # test insort_right
  26. insort_right(a, 23)
  27. assert a == [0, 1, 1, 1, 2, 5, 6, 7, 8, 16, 22, 23, 23]
  28. insort_right(a, 0)
  29. assert a == [0, 0, 1, 1, 1, 2, 5, 6, 7, 8, 16, 22, 23, 23]
  30. insort_right(a, 5)
  31. assert a == [0, 0, 1, 1, 1, 2, 5, 5, 6, 7, 8, 16, 22, 23, 23]
  32. insort_right(a, 1)
  33. assert a == [0, 0, 1, 1, 1, 1, 2, 5, 5, 6, 7, 8, 16, 22, 23, 23]