Design a class that iterates through IPv4 addresses.
An IPv4 address is a string in the form a.b.c.d, where each octet is an integer from 0 to 255.
Implement an iterator with the following behavior:
-
The constructor receives:
-
start_ip
: the first IPv4 address in the sequence
-
end_ip
: the last IPv4 address that may be returned
-
step
: a positive integer increment
-
The increment is fixed for the lifetime of the object and is provided in the constructor, not passed into
next()
.
-
next()
returns the current IP address and then advances by
step
addresses.
-
has_next()
returns whether another valid address can still be returned without passing
end_ip
.
You should correctly handle carry between octets. For example:
-
incrementing
10.0.0.255
by 1 gives
10.0.1.0
-
incrementing
192.168.1.254
by 2 gives
192.168.2.0
Assume all input IP strings are valid and start_ip <= end_ip.
Example:
-
start_ip = "192.168.0.254"
-
end_ip = "192.168.1.4"
-
step = 2
The iterator should return:
-
192.168.0.254
-
192.168.1.0
-
192.168.1.2
-
192.168.1.4
Follow-up: What internal representation would you choose to simplify the implementation and avoid repeated string parsing?