MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / range_counting

Function range_counting

data_structures/binary_tree/wavelet_tree.py:161–204  ·  view source on GitHub ↗

Returns the number of elements in range [start_num, end_num] in interval [start, end] in the list >>> root = build_tree(test_array) >>> range_counting(root, 1, 10, 3, 7) 3 >>> range_counting(root, 2, 2, 1, 4) 1 >>> range_counting(root, 0, 19, 0, 100) 20 >>>

(
    node: Node | None, start: int, end: int, start_num: int, end_num: int
)

Source from the content-addressed store, hash-verified

159
160
161def range_counting(
162 node: Node | None, start: int, end: int, start_num: int, end_num: int
163) -> int:
164 """
165 Returns the number of elements in range [start_num, end_num]
166 in interval [start, end] in the list
167
168 >>> root = build_tree(test_array)
169 >>> range_counting(root, 1, 10, 3, 7)
170 3
171 >>> range_counting(root, 2, 2, 1, 4)
172 1
173 >>> range_counting(root, 0, 19, 0, 100)
174 20
175 >>> range_counting(root, 1, 0, 1, 100)
176 0
177 >>> range_counting(root, 0, 17, 100, 1)
178 0
179 """
180 if (
181 start > end
182 or node is None
183 or start_num > end_num
184 or node.minn > end_num
185 or node.maxx < start_num
186 ):
187 return 0
188 if start_num <= node.minn and node.maxx <= end_num:
189 return end - start + 1
190 left = range_counting(
191 node.left,
192 (node.map_left[start - 1] if start else 0),
193 node.map_left[end] - 1,
194 start_num,
195 end_num,
196 )
197 right = range_counting(
198 node.right,
199 start - (node.map_left[start - 1] if start else 0),
200 end - node.map_left[end],
201 start_num,
202 end_num,
203 )
204 return left + right
205
206
207if __name__ == "__main__":

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected