forked from adityabisoi/ds-algo-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.py
320 lines (272 loc) · 7.73 KB
/
solution.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import sys
import cProfile
class output_result:
ROOT_UPDATE = 0
LEFT_TREE_UPDATE = 1
RIGHT_TREE_UPDATE = 2
def __init__(self):
self.root = []
self.ltree = []
self.rtree = []
self.bt_cnt = 0
self.obuf = ""
#for inserting the node
def __insert_node(self, node, tree_update):
if (tree_update == self.LEFT_TREE_UPDATE):
nl = []
nl.insert(0, node)
if (len(self.ltree) > 0):
t = self.ltree.pop(0)
nl.insert(1, t)
nl.insert(2, [])
else:
nl.insert(1, [])
nl.insert(2, [])
self.ltree.insert(0, nl)
elif (tree_update == self.RIGHT_TREE_UPDATE):
nl = []
nl.insert(0, node)
if (len(self.rtree) > 0):
t = self.rtree.pop(0)
nl.insert(1, [])
nl.insert(2, t)
else:
nl.insert(1, [])
nl.insert(2, [])
self.rtree.insert(0, nl)
else:
self.root.insert(0, node)
if (len(self.ltree) > 0):
self.root.insert(1, self.ltree[0])
else:
self.root.insert(1, [])
if (len(self.rtree) > 0):
self.root.insert(2, self.rtree[0])
else:
self.root.insert(2, [])
if len(node[1]) > 0 and node[1][0] != 0:
self.bt_cnt += 1
return
#for tree traversal
def __inorder_traversal(self, node):
if (len(node[1]) > 0):
self.__inorder_traversal(node[1])
if len(node[0][1]) > 0:
outputstr = ''.join(map(str, node[0][1]))
if (outputstr != "0"):
self.obuf += str(node[0][0]) + " " + outputstr + "\n"
if (len(node[2]) > 0):
self.__inorder_traversal(node[2])
return
def get_root(self):
return self.root
def get_tree_cnt(self):
return self.bt_cnt
def left_tree_add(self, node):
self.__insert_node(node, self.LEFT_TREE_UPDATE)
return
def right_tree_add(self, node):
self.__insert_node(node, self.RIGHT_TREE_UPDATE)
return
# add the root node
def root_add(self, node):
self.__insert_node(node, self.ROOT_UPDATE)
return
# print the output result
def print_output(self):
sys.stdout.write(str(self.bt_cnt) + "\n")
self.__inorder_traversal(self.root)
sys.stdout.write(self.obuf)
return
# each entry is stored as (asl, max-entries, mav-value)
ores = []
l = 0
r = 0
clv = 0
def create_ten_power_x_list(k):
if (k <= 1):
return [1, 0], 1
rl = [1]
r2p = 0
if (k > 1):
r2p = pow(2, k - 1)
rl = rl + [0]*r2p
return rl, r2p
# algo for a + b
def add_two_int_list(x, y):
# look for largest possible level
al = len(x)
bl = len(y)
if (bl == 0):
a = x[:]
elif (al == 0):
a = y[:]
else:
if (al < bl):
a = y[:]
minl = al
b = x
maxl = bl
elif (bl < al):
a = x[:]
maxl = al
b = y
minl = bl
else:
a = x[:]
b = y
maxl = al
minl = bl
carry = 0
i = 1
entries = 0
# a >= b as per this logic and result is also stored in a
while entries < minl:
sum_t = b[minl - i] + a[maxl - i] + carry
if (sum_t >= 10):
carry = 1
a[maxl - i] = sum_t - 10
else:
carry = 0
a[maxl - i] = sum_t
i += 1
entries += 1
# if carry > 0, then we need to do some more work
i = maxl - minl - 1
while carry > 0 and i >= 0:
sum_t = a[i] + carry
if (sum_t >= 10):
carry = 1
a[i] = sum_t - 10
else:
a[i] = sum_t
carry = 0
i -= 1
if (carry == 1):
a = [1] + a
if (a[0] == 0):
a.pop(0)
return a
# algo for a - b
def del_two_int_list(x, y):
al = len(x)
bl = len(y)
if al < bl:
return []
a = x[:]
b = y
if al == bl and a[0] < b[0]:
return []
a_i = al - 1
b_i = bl - 1
while (b_i >= 0):
# subtraction borrow logic
if (a[a_i] < b[b_i]):
a[a_i] = 10 + a[a_i]
i = 1
while a[a_i - i] == 0:
a[a_i - i] = 9
i += 1
a[a_i - i] -= 1
# subtract logic
a[a_i] = a[a_i] - b[b_i]
a_i -= 1
b_i -= 1
i = 0
while i < al:
if a[i] != 0:
break
i += 1
if i > 0:
a = a[i:]
# print "a: " + str(a)
return a
def compare_two_list_int(a, b):
a1 = len(a)
b1 = len(b)
if a1 > b1:
return 1
elif a1 < b1:
return -1
else:
i = 0
while (i < a1):
if (a[i] != b[i]):
if (a[i] > b[i]):
return 1
else:
return -1
i += 1
return 0
# a % b where b is the number of zeroes in 10
def divmod_10_list_int(x, b, mod_reqd):
c = []
a = x[:]
al = len(a)
if b >= len(a):
return a, [0]
if mod_reqd is True:
c = a[al - b:]
i = 0
while i < len(c):
if c[i] != 0:
break
i += 1
c = c[i:]
a = a[:al - b]
return c, a
def range_traverse():
global clv, l, r, ores
clv_me, clv_10s = create_ten_power_x_list(clv)
range_diff = del_two_int_list(r, l)
if compare_two_list_int(range_diff, clv_me) >= 0:
modl, divl = divmod_10_list_int(l, clv_10s, True)
del divl
lent = del_two_int_list(clv_me, modl)
if compare_two_list_int(lent, clv_me) == 0:
lent = [0]
rent, rentr = divmod_10_list_int(r, clv_10s, True)
tmpl = add_two_int_list(l, lent)
modl, l = divmod_10_list_int(tmpl, clv_10s, False)
del tmpl
del modl
modr, r = divmod_10_list_int(del_two_int_list(r, rent), clv_10s, False)
del modr
ores.left_tree_add((clv, lent))
ores.right_tree_add((clv, rent))
clv += 1
del range_diff
range_traverse()
else:
ent = range_diff
modent, divent = divmod_10_list_int(ent, clv_10s, True)
ores.root_add((clv, modent))
del range_diff
return
# @profile
def main():
global ores, l, r
ores = output_result()
l = list(map(int, (sys.stdin.readline()).strip()))
r = list(map(int, (sys.stdin.readline()).strip()))
l = del_two_int_list(l, [1])
range_traverse()
ores.print_output()
return
enable_profiling = 0
if __name__ == "__main__":
if (len(sys.argv) == 2):
if (sys.argv[1] == "profile"):
enable_profiling = 1
else:
print("usage: %s profile <optional>" % sys.argv[0])
exit(-1)
if (len(sys.argv) > 2):
print("usage: %s profile <optional>" % sys.argv[0])
exit(-1)
if (enable_profiling == 1):
cProfile.run('main()')
else:
main()
sys.exit(0)
# p.s. the problem is way too complex and intricated. Skip this problem if you have not much knowledge about the topic.