-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisjintv.py
179 lines (123 loc) · 3.73 KB
/
disjintv.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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import disjintv1
import random
##########################################################
class DisjIntvs():
def __init__(self, intvs=[]):
# the intervals are kept in the format of [a, b]
# add artificial negative infinity and positive infinity to simplify the code
self.intvs = [[-float("inf"), -float("inf")], [float("inf"), float("inf")]]
if intvs:
for a, b in intvs:
self.add(a, b)
def __str__(self):
n = len(self.intvs)
return str(self.intvs[1:n-1])
def _find(self, i, t): # i=0:left end, i=1:right end; t: traget val
# binary search
lo, hi = 0, len(self.intvs)-1
while lo <= hi:
mid = (lo+hi) // 2
if self.intvs[mid][i] == t:
return mid
elif t < self.intvs[mid][i]:
hi = mid - 1
else:
lo = mid + 1
return lo
def add(self, a, b):
print("+", a, b)
if a == b: # invalid interval, do nothing
return
if len(self.intvs) == 2: # no intvs stored
self.intvs = [self.intvs[0]] + [[a, b]] + [self.intvs[1]]
print(self.__str__())
return
# find the lower and upper index
i, j, n = self._find(0, a), self._find(1, b), len(self.intvs)
if i > 0 and a <= self.intvs[i-1][1]:
i -= 1
if 0 < j < n and b < self.intvs[j][0]:
j -= 1
# new lower and upp bdds
a1, b1 = min(self.intvs[i][0], a), max(self.intvs[j][1], b)
self.intvs[i:j+1] = [[a1, b1]]
print(self.__str__())
return
def remove(self, a, b):
print("-", a, b)
if a == b: # invalid interval, do nothing
return
if len(self.intvs) == 2: # no intv stored
print(self.__str__())
return # do nothing
# find the lower and upper index
i, j, n = self._find(1, a), self._find(0, b), len(self.intvs)
if i > 0 and a <= self.intvs[i-1][1]:
i -= 1
if 0 < j < n and b < self.intvs[j][0]:
j -= 1
# possible new lower and upper bounds
b1, a1 = min(self.intvs[i][1], a), max(self.intvs[j][0], b)
# deal with removal in cases
if self.intvs[i][0] < b1 and a1 < self.intvs[j][1]:
self.intvs[i:j+1] = [[self.intvs[i][0], b1]] + [[a1, self.intvs[j][1]]]
elif self.intvs[i][0] < b1:
self.intvs[i:j+1] = [[self.intvs[i][0], b1]]
elif a1 < self.intvs[j][1]:
self.intvs[i:j+1] = [[a1, self.intvs[j][1]]]
else:
self.intvs[i:j+1] = []
print(self.__str__())
return
#----------------------------------------------------------
class Operator():
def __init__(self, intvs=[], acts=[]):
self.intvs = DisjIntvs(intvs) # maintian a set of intervals
self.acts = acts # maintian a set of actions
# for future expansion
#def load(self): # load commands from a file
# return
def gen_randtest(self):
TESTCMDS = 5 # number of test cases
UPPBDD = 20 # upper bound of the interval
temp = []
N = random.randint(1, TESTCMDS)
for _ in range(N):
c = random.randint(0, 1)
a, b = random.randint(0, UPPBDD), random.randint(0, UPPBDD)
if a > b:
a, b = b, a
temp.append([c, a, b])
self.acts = temp
def run(self):
for com, a, b in self.acts:
if com: # com=1: add
self.intvs.add(a, b)
else: # com=0: remove
self.intvs.remove(a, b)
def result(self):
#print(self.intvs)
return self.intvs.__str__()
##########################################################
# main section
# random test
ROUNDS = 10 # test rounds
c = 0 # count
for _ in range(ROUNDS):
# first operator
opt = Operator()
opt.gen_randtest()
opt.run()
#opt.result()
print("----")
# second operator
A = opt.acts # export the commands for operator1 to test
opt1 = disjintv1.Operator(acts=A) # second approach for justify the result
opt1.run()
c += opt.result()==opt1.result()
print("-------------")
print("different cases: %d\n" % (ROUNDS-c)) # print out number of different cases
# test 1
A = [[1,1,5],[0,2,3],[1,6,8],[0,4,7],[1,2,7]]
opt = Operator(acts=A)
opt.run()