fix calc_dip

This commit is contained in:
Pavel Patsey
2025-01-23 10:20:36 +03:00
parent 474b6779d8
commit adc569af21
2 changed files with 11 additions and 3 deletions
+7
View File
@@ -88,6 +88,13 @@ def test_find_parent__with_exception():
def test_calc_dip(): def test_calc_dip():
assert calc_dip(26, 26) == 0
assert calc_dip(26, 27) == 67108864 assert calc_dip(26, 27) == 67108864
assert calc_dip(27, 26) == 67108864
assert calc_dip(26, 28) == 201326592 assert calc_dip(26, 28) == 201326592
assert calc_dip(28, 26) == 201326592
assert calc_dip(26, 29) == 469762048 assert calc_dip(26, 29) == 469762048
assert calc_dip(29, 26) == 469762048
+4 -3
View File
@@ -44,9 +44,10 @@ def find_parent(a: Node, b: Node) -> Node:
def calc_dip(mask_len_a: int, mask_len_b: int) -> int: def calc_dip(mask_len_a: int, mask_len_b: int) -> int:
dip = 0 dip = 0
while mask_len_a < mask_len_b: len_min, len_max = sorted((mask_len_a, mask_len_b))
dip += 1 << mask_len_a while len_min < len_max:
mask_len_a += 1 dip += 1 << len_min
len_min += 1
return dip return dip