speed up remove_ips_with_subnets function

This commit is contained in:
Павел
2025-01-09 14:19:00 +03:00
parent 99da7ea726
commit 113b651167
+8 -10
View File
@@ -41,14 +41,13 @@ def get_mask(binary: str) -> str:
def remove_ips_with_subnets(binaries: Set[str]) -> Set[str]: def remove_ips_with_subnets(binaries: Set[str]) -> Set[str]:
sorted_binaries = sorted(binaries) sorted_binaries = sorted(binaries)
i = 0 result = set()
while i < len(sorted_binaries) - 1: for x, y in zip(sorted_binaries, sorted_binaries[1:]):
mask = get_mask(sorted_binaries[i]) mask = get_mask(x)
if sorted_binaries[i + 1].startswith(mask): if not y.startswith(mask):
del sorted_binaries[i] result.add(x)
else: result.add(sorted_binaries[-1])
i += 1 return result
return set(sorted_binaries)
def rough_merge_binaries(binaries: List[str], req_len: int) -> List[str]: def rough_merge_binaries(binaries: List[str], req_len: int) -> List[str]:
@@ -104,7 +103,7 @@ def smooth_merge_binaries(binaries: List[str], req_len: int) -> List[str]:
def main(): def main():
file = "cidr4.txt" file = "cidr4.txt"
required_len = 20 required_len = 15
data = get_data(file) data = get_data(file)
bin_ips = list(map(cidr4_to_binary, data)) bin_ips = list(map(cidr4_to_binary, data))
@@ -172,7 +171,6 @@ if __name__ == "__main__":
"1111000", "1111000",
"0101000", "0101000",
} }
assert remove_ips_with_subnets(set()) == set()
# main() # main()
cProfile.run("main()") cProfile.run("main()")