From 9377d2313430252ac574e1df7e0727e5e99d64a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=B0=D0=B2=D0=B5=D0=BB?= Date: Sat, 11 Jan 2025 08:08:08 +0300 Subject: [PATCH] add convert data to nodes --- cidr4_merger.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/cidr4_merger.py b/cidr4_merger.py index e39134e..dfe9a62 100644 --- a/cidr4_merger.py +++ b/cidr4_merger.py @@ -1,8 +1,6 @@ import cProfile -from collections import defaultdict -from copy import deepcopy -from ipaddress import IPv4Address -from typing import List, Set, Tuple + +Node = tuple[int, int, int] def get_data(input_file): @@ -11,28 +9,32 @@ def get_data(input_file): return data -def cidr4_to_node(cidr4: str) -> Tuple[int, int, int]: +def cidr4_to_node(cidr4: str) -> Node: ip, mask_len = cidr4.strip().split("/") mask_len = int(mask_len) added_ips_number = 0 - a, b, c, d = ip.split(".") - a, b, c, d = int(a), int(b), int(c), int(d) + a, b, c, d = list(map(int, ip.split("."))) ip_value = a * 256**3 + b * 256**2 + c * 256**1 + d * 256**0 return ip_value, mask_len, added_ips_number +def data_to_nodes(data: list[str]) -> list[Node]: + return sorted(map(cidr4_to_node, data)) + + def main(): file = "cidr4.txt" required_len = 15 data = get_data(file) - nodes = sorted(map(cidr4_to_node, data)) + nodes = data_to_nodes(data) for n in nodes: print(n) if __name__ == "__main__": assert cidr4_to_node("4.78.139.0/24") == (72256256, 24, 0) + assert cidr4_to_node("0.0.0.0/32") == (0, 32, 0) main() # cProfile.run("main()")