forked from sofeien/Python3-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSortedList.py
67 lines (56 loc) · 1.92 KB
/
SortedList.py
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
_identity=lambda x:x
class SortedList:
def __init__(self,sequence=None,key=None):
self.__key=key or _identity
assert hasattr(self.__key,"__call__")
if sequence is None:
self.__list=[]
elif (isinstance(sequence,SortedList) and sequence.key==self.__key):
self.__list=sequence.__list[:]
else:
self.__list=sorted(list(sequence),key=self.key)
@property
def key(self):
return self.__key
def add(self,value):
index=self.__bisect_left(value)
if index==len(self.__list):
self.__list.append(value)
else:
self.__list.insert(index,value)
def __bisect_left(self,value):
key=self.__key(value)
left,right=0,len(self.__list)
count=0
while left<right:
count+=1
print('已迭代次数{}'.format(count))
middle=(left+right)//2
print("左边界:{} 中值:{} 右边界{}".format(left,middle,right))
if self.__key(self.__list[middle])<key:
left=middle+1
else:
right=middle
#print('已迭代次数{}'.format(count))
return left
def remove(self,value):
index=self.__bisect_left(value)
if index<len(self.__list) and self.__list[index]==value:
del self.__list[index]
else:
raise ValueError("{}.remove(x):x not in list".format(self.__class__.__name__))
def remove_every(self,value):
count=0
index=self.__bisect_left(value)
print('begin:{}'.format(index))
while(index<len(self.__list) and self.__list[index]==value):
del self.__list[index]
count +=1
print(count)
return count
def __str__(self):
return str(list(self.__list))
if __name__=="__main__":
x=SortedList([1]*2+[2,3,4]+[1]*2,abs)
x.remove_every(1)
print(x)