File: defanging-an-ip-address/solution.py

Date: 2026-06-06

Time: 16:13

Purpose

This file solves LeetCode 1108 — Defanging an IP Address. It owns exactly one responsibility: transforming a valid IPv4 address string by wrapping each . in brackets ([.]), producing a "defanged" representation safe for display in contexts where dots might be parsed as separators (e.g., logs, chat messages, security reports).

Key Components

Solution.defangIPaddr(self, address: str) -> str

Patterns

Dependencies

Imports: None. Pure standard library, no external packages.

Imported by: The "Imported By" list in the prompt is misleading — those are test files across the entire repo that import their *own* solution.py, not this one. The only real consumer is defanging-an-ip-address/test_solution.py, which imports this Solution class to run test cases against it.

Flow

1. Caller passes a string like "255.100.50.0".

2. str.replace(".", "[.]") scans left to right, building a new string with each . expanded to [.].

3. The new string ("255[.]100[.]50[.]0") is returned.

There is no branching, no state, no iteration visible at the Python level — str.replace handles it internally in C (CPython).

Invariants

Error Handling

None. There is no validation, no try/except, no edge-case guarding. This is appropriate — the problem guarantees valid input, and str.replace on any string with no dots simply returns the original string unchanged (a safe no-op).

Topics to Explore

Beliefs