-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbisect_lab.py
More file actions
62 lines (47 loc) · 1.04 KB
/
Copy pathbisect_lab.py
File metadata and controls
62 lines (47 loc) · 1.04 KB
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
__author__ = 'admin'
#-*- coding:utf8 -*-
import bisect
import random
#正向插入排序
l = []
for i in range(1, 15):
r = random.randint(1, 1000)
position = bisect.bisect(l, r)
bisect.insort(l,r)
print '%3d %3d' %(r,position),l
def index(a, x):
'Locate the leftmost value exactly equal to x'
i = bisect.bisect_left(a, x)
if i != len(a) and a[i] == x:
return i
raise ValueError
def find_lt(a, x):
'Find rightmost value less than x'
i = bisect.bisect_left(a, x)
if i:
return a[i-1]
raise ValueError
def find_le(a, x):
'Find rightmost value less than or equal to x'
i = bisect.bisect_right(a, x)
if i:
return a[i-1]
raise ValueError
def find_gt(a, x):
'Find leftmost value greater than x'
i = bisect.bisect_right(a, x)
if i != len(a):
return a[i]
raise ValueError
def find_ge(a, x):
'Find leftmost item greater than or equal to x'
i = bisect.bisect_left(a, x)
if i != len(a):
return a[i]
raise ValueError
l = [2 ,3 ,4 ,5, 6, 7]
print index(l,3)
print find_lt(l,3)
print find_le(l,3)
print find_gt(l,3)
print find_ge(l,3)