← 返回 openai 的题目列表IP Address Iterator
类型:online_judge
Part 1: Forward Iteration
Implement an IPV4Iterator class. Given a starting IP string, such as 192.168.0.1, implement an iterator that starts from this point and returns subsequent IP addresses until reaching the IPv4 limit 255.255.255.255. Implement the standard Python iterator protocol, including __init__, __iter__, and __next__.
Part 2: Reverse Iteration
Given an end IP string, such as 192.168.0.255, implement a reverse iterator that starts from this IP and iterates backward until 0.0.0.0.
Part 3: CIDR Parsing
The input is a CIDR format string, such as 192.168.1.0/24. Parse this string, use bitwise operations to calculate the start IP and end IP of the subnet, and iteratively return all IP addresses within this range.
CIDR format: An IP address followed by /n denotes the network prefix length, indicating that the first n bits of the 32-bit IP are fixed and the remaining 32-n bits (host part) are variable.
Part 4: Optimization
Discuss how to optimize time and space complexity.
Example
Input
192.168.0.1