IPv4 Address Iterator

Asked at OpenAIPhone screen

Reported by the problem's creator, not verified

IPv4 addresses are written as four dotted decimal octets a.b.c.d, each from 0 to 255, such as 10.0.0.254. Read as a single 32-bit number, an address is a * 2^24 + b * 2^16 + c * 2^8 + d, and addresses are ordered by that number. The smallest address is 0.0.0.0 and the largest is 255.255.255.255.

Build the class IPv4Iterator. Each test drives one instance through a sequence of calls and checks every return value. When next() has no address left, it raises StopIteration.

Examples

  1. Example 1

    Input: init = ["10.0.0.254"], operations = [["has_next",[]],["next",[]],["next",[]],["next",[]],["has_next",[]],["next",[]]]

    Output: [true,"10.0.0.254","10.0.0.255","10.0.1.0",true,"10.0.1.1"]

    Explanation: The last octet 254 becomes 255, then the next call carries into the third octet. After three returns has_next() is still true and the following next() returns 10.0.1.1.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.