IPv4 CIDR Utilities

medium
Unit 20 · Timing0/3 to milestone

Implement IPv4 network helpers without the ipaddress module:

  • ip_to_int(ip) and int_to_ip(n): convert between dotted quads and 32-bit integers, raising ValueError for malformed addresses (wrong part count, non-numeric parts, values over 255)
  • parse_cidr(cidr): "10.1.0.0/16" → (network_int, prefix). Raise ValueError if host bits are set (like "10.1.2.3/16") or the prefix is outside 0–32.
  • contains(cidr, ip)
  • subnets(cidr, new_prefix): split into the list of smaller CIDR strings
  • collapse(cidrs): the minimal sorted list of CIDRs covering exactly the union, by removing covered networks and repeatedly merging adjacent sibling pairs (two /25s that make up a /24)
Pro problem

Unlock this and every premium problem across all tracks — plus hidden-test grading, advanced AI engineering modules and new content every week.

Start 7-day free trial
solution.py · Python 3.13
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
IPv4 CIDR Utilities · TechGradify