Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

bisect.py 2.5 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
  1. """Bisection algorithms."""
  2. def insort_right(a, x, lo=0, hi=None):
  3. """Insert item x in list a, and keep it sorted assuming a is sorted.
  4. If x is already in a, insert it to the right of the rightmost x.
  5. Optional args lo (default 0) and hi (default len(a)) bound the
  6. slice of a to be searched.
  7. """
  8. if lo < 0:
  9. raise ValueError('lo must be non-negative')
  10. if hi is None:
  11. hi = len(a)
  12. while lo < hi:
  13. mid = (lo+hi)//2
  14. if x < a[mid]: hi = mid
  15. else: lo = mid+1
  16. a.insert(lo, x)
  17. def bisect_right(a, x, lo=0, hi=None):
  18. """Return the index where to insert item x in list a, assuming a is sorted.
  19. The return value i is such that all e in a[:i] have e <= x, and all e in
  20. a[i:] have e > x. So if x already appears in the list, a.insert(x) will
  21. insert just after the rightmost x already there.
  22. Optional args lo (default 0) and hi (default len(a)) bound the
  23. slice of a to be searched.
  24. """
  25. if lo < 0:
  26. raise ValueError('lo must be non-negative')
  27. if hi is None:
  28. hi = len(a)
  29. while lo < hi:
  30. mid = (lo+hi)//2
  31. if x < a[mid]: hi = mid
  32. else: lo = mid+1
  33. return lo
  34. def insort_left(a, x, lo=0, hi=None):
  35. """Insert item x in list a, and keep it sorted assuming a is sorted.
  36. If x is already in a, insert it to the left of the leftmost x.
  37. Optional args lo (default 0) and hi (default len(a)) bound the
  38. slice of a to be searched.
  39. """
  40. if lo < 0:
  41. raise ValueError('lo must be non-negative')
  42. if hi is None:
  43. hi = len(a)
  44. while lo < hi:
  45. mid = (lo+hi)//2
  46. if a[mid] < x: lo = mid+1
  47. else: hi = mid
  48. a.insert(lo, x)
  49. def bisect_left(a, x, lo=0, hi=None):
  50. """Return the index where to insert item x in list a, assuming a is sorted.
  51. The return value i is such that all e in a[:i] have e < x, and all e in
  52. a[i:] have e >= x. So if x already appears in the list, a.insert(x) will
  53. insert just before the leftmost x already there.
  54. Optional args lo (default 0) and hi (default len(a)) bound the
  55. slice of a to be searched.
  56. """
  57. if lo < 0:
  58. raise ValueError('lo must be non-negative')
  59. if hi is None:
  60. hi = len(a)
  61. while lo < hi:
  62. mid = (lo+hi)//2
  63. if a[mid] < x: lo = mid+1
  64. else: hi = mid
  65. return lo
  66. # Overwrite above definitions with a fast C implementation
  67. try:
  68. from _bisect import *
  69. except ImportError:
  70. pass
  71. # Create aliases
  72. bisect = bisect_right
  73. insort = insort_right
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...