Count an address pool without listing it
Calculate inclusive pool capacity with overlapping exclusions and duplicate lease records.
Count the policy's address range
This exercise accepts an explicit IPv4 start and end address, both inclusive. It does not infer a subnet's usable hosts: the pool owner has already selected the intended endpoints. Convert addresses to integers so the total is end minus start plus one.
Exclusions remove inclusive ranges from that pool. Overlapping or adjacent exclusions should be merged before counting, otherwise the same addresses could be removed more than once. Reject exclusions outside the pool rather than silently clipping an erroneous policy.
Leases are observations, not extra capacity
The supplied lease list represents active leases. Deduplicate exact addresses, count only those inside the pool and outside exclusions, and ignore valid out-of-pool addresses. Invalid lease addresses still raise ValueError, because their meaning cannot be established.
Return usable, used and free counts plus an unrounded utilization percentage. If exclusions cover the entire pool, utilization is None rather than a division error or a misleading healthy percentage. Used and free are then both zero.
Do not build a set containing every address between the endpoints. Interval arithmetic handles large pools with memory proportional to the supplied exclusions and leases. This is an export analysis exercise: it does not contact a DHCP server or prove that a lease is still active on a live network.
from ipaddress import IPv4Address
start, end = IPv4Address("192.0.2.10"), IPv4Address("192.0.2.20")
print(int(end) - int(start) + 1)
Put it into practice.
Define scope_capacity(start, end, leases, exclusions) for an inclusive IPv4 range. Require string IPv4 addresses; reversed endpoints, malformed addresses or exclusions outside/reversed within the pool raise ValueError. Each exclusion has start/end; merge overlapping/adjacent ranges. Leases are active address strings: deduplicate them and count only in-pool, nonexcluded addresses. Return usable, used, free, utilization_percent (None when usable=0). Do not enumerate the pool or change inputs.
See an explained solution
from ipaddress import IPv4Address
def scope_capacity(start, end, leases, exclusions):
def address(value):
if not isinstance(value,str):
raise ValueError("Use an IPv4 string")
return int(IPv4Address(value.strip()))
first, last = address(start), address(end)
if first > last:
raise ValueError("Pool endpoints are reversed")
intervals = []
for row in exclusions:
low, high = address(row["start"]), address(row["end"])
if low > high or low < first or high > last:
raise ValueError("Exclusion is outside the pool")
intervals.append((low,high))
merged = []
for low, high in sorted(intervals):
if merged and low <= merged[-1][1]+1:
merged[-1] = (merged[-1][0],max(merged[-1][1],high))
else:
merged.append((low,high))
usable = last-first+1-sum(high-low+1 for low,high in merged)
active = {address(value) for value in leases}
used = sum(first <= value <= last and not any(low <= value <= high for low,high in merged) for value in active)
return {"usable":usable,"used":used,"free":usable-used,"utilization_percent":used/usable*100 if usable else None}
if __name__ == "__main__":
print(scope_capacity("192.0.2.10","192.0.2.20",["192.0.2.10"],[]))
Interval merging prevents double subtraction and avoids allocating huge address sets. The result distinguishes an exhausted or empty pool from malformed policy.